Welcome

High quality MyBB plugins at your disposal

Hi, I am a passionate developer with modernity and simplicity in mind. MyBBoost is my mansion where you can download my plugins and themes, other than receiving support for them. Several free plugins are available as well as premium ones, all compatible with the latest MyBB releases. You can purchase and download items singularly or purchase a lifetime subscription including a combination of premium plugins and:

  • Priority support
  • Future plugins access
  • Total refund if not satisfied 1
1 Terms and conditions apply. For more informations, read the EULA.

Use SendGrid to send emails through MyBB

Shade Administrator 12 Mar 2016 Edited
#1
You installed MyBB. Everything is running fine, you start writing great content and your first visitors begin to register and participate in your community. That's the precise moment in which you realize that your free web hosting limits the mail you send to your users through the forum's normal operations. No big deal, you decide to switch over SMTP only to find out that the hosting is blocking SMTP communications and you are stuck using a restricted PHP mail() function.

This was exactly what happened to me some months ago. Realizing that not only subscription emails would not be delivered properly, but also weekly digests and other personal mails would not work as they should really drove me crazy. Until I discovered SendGrid.

SendGrid provides an high quality, fully manageable and reliable mailing platform which does work and it's completely free of charge if you are running a relatively small project like I do. Not only it is so simple to set up but it is also really easy to use and it has an awesome activity panel with an incredibly precise tracker of your users' actions. You are aware when your mail are delivered, clicked, read, if any links in the email has been followed, bounces, etc.; just what mail is meant to be. Never had a problem in my first month of use. Plus, with the free plan you have up to 12.000 mails available per month which, assuming you have a small-to-medium community, you'll never even get close to it.

Despite all this beauty, there is no easy way to integrate SendGrid with MyBB rather than performing some core edits. MyBB devs should have gone nuts when building the mailing handler – just because the hook system is not even globalized in the my_mail() function. Whatever. Here's how to replace MyBB's mailing system with SendGrid. Note that we are replacing it – you must revert the core edits in order to bring the normal mail() or SMTP functions back to the game.

1. Register to SendGrid


The very first thing to do is registering to SendGrid. Go to http://www.sendgrid.com/pricing and search for the Free plan scrolling down to the bottom of the page.

[Image: dV8QW51.png]

Once you click on Try for free, you should see something like:

[Image: bkRYKVs.png]

Fill out the form and verify your account by clicking on the link which is sent to the email you used to register.

2. Create an API Key


Once you have log in into your SendGrid account, you should be at http://app.sendgrid.com. On your left you should see something like:

[Image: yGrZ4T1.png]

Click on Settings and then on API Keys. Head to your top right and push on Create API Key.

[Image: QhMA031.png]

A panel will swing in from the right of your screen. Enter a name for the API Key and set everything either to Full Access or to Read Access, just to be sure that you can use everything which is provided into the SendGrid's free account using that key. When you save the key, a one-time screenshot will appear with the unique token generated for the API Key you have just built. It is extremely important to save this piece of information to your Notepad or something similar, as you won't be able to see it again. It will be useful in the next step.

[Image: aEMKtbv.png]

3. Download the APIs


Download the file attached to this post, unzip it and put all the files in the right folders. They are already ordered in the correct folders by the way so you just really need to check out that every .php file is in inc/mailhandlers/sendgrid.

4. Do some core editing


Open your FTP/cPanel and search for the inc/functions.php file. Open it with your preferred text editor. Around line 532 there should be the my_mail() function:

function my_mail($to$subject$message$from=""$charset=""$headers=""$keep_alive=false$format="text"$message_text=""$return_email="")
{
    global 
$mybb;
    static 
$mail;

    
// Does our object not exist? Create it
    
if(!is_object($mail))
    {
        require_once 
MYBB_ROOT."inc/class_mailhandler.php";

        if(
$mybb->settings['mail_handler'] == 'smtp')
        {
            require_once 
MYBB_ROOT."inc/mailhandlers/smtp.php";
            
$mail = new SmtpMail();
        }
        else
        {
            require_once 
MYBB_ROOT."inc/mailhandlers/php.php";
            
$mail = new PhpMail();
        }
        
        
    }

    
// Using SMTP based mail
    
if($mybb->settings['mail_handler'] == 'smtp')
    {
        if(
$keep_alive == true)
        {
            
$mail->keep_alive true;
        }
    }

    
// Using PHP based mail()
    
else
    {
        if(
$mybb->settings['mail_parameters'] != '')
        {
            
$mail->additional_parameters $mybb->settings['mail_parameters'];
        }
    }

    
// Build and send
    
$mail->build_message($to$subject$message$from$charset$headers$format$message_text$return_email);
    return 
$mail->send();
    

Replace the entire function with:

function my_mail($to$subject$message$from=""$charset=""$headers=""$keep_alive=false$format="text"$message_text=""$return_email="")
{
    global 
$mybb;
    static 
$mail;

    
// Does our object not exist? Create it
    /*if(!is_object($mail))
    {
        require_once MYBB_ROOT."inc/class_mailhandler.php";

        if($mybb->settings['mail_handler'] == 'smtp')
        {
            require_once MYBB_ROOT."inc/mailhandlers/smtp.php";
            $mail = new SmtpMail();
        }
        else
        {
            require_once MYBB_ROOT."inc/mailhandlers/php.php";
            $mail = new PhpMail();
        }
        
        
    }

    // Using SMTP based mail
    if($mybb->settings['mail_handler'] == 'smtp')
    {
        if($keep_alive == true)
        {
            $mail->keep_alive = true;
        }
    }

    // Using PHP based mail()
    else
    {
        if($mybb->settings['mail_parameters'] != '')
        {
            $mail->additional_parameters = $mybb->settings['mail_parameters'];
        }
    }

    // Build and send
    $mail->build_message($to, $subject, $message, $from, $charset, $headers, $format, $message_text, $return_email);
    return $mail->send();*/
    
    
if (!$to) {
        return 
false;
    }
    
    require_once 
MYBB_ROOT "inc/mailhandlers/sendgrid/sendgrid-php.php";
    
    
$sendgrid = new SendGrid('###APIKEY###');
    
    if (!
$from) {
        
$from $mybb->settings['adminemail'];
    }
    
    
$email = new SendGrid\Email();
    
$email
        
->addTo($to)
        ->
setFrom($from)
        ->
setFromName($mybb->settings['bbname'])
        ->
setSubject($subject)
        ->
setText($message_text)
        ->
setHtml($message)
    ;
    
    
$response $sendgrid->send($email);
    
    return 
true;

Since we can manage errors in the SendGrid activity panel, we don't really care about returning true even if the $response is not 200 OK. Replace ###APIKEY### with the API Key you have saved some moments ago.

5. Enjoy


It's really that simple. You can now send mails using SendGrid. Since there are not any limitations on how many mails you send per minute, you can send your digest mails through the Mass mail option in your ACP even 500/1000 at one time. Just beware that SendGrid applies a very strict policy when it comes to spam and bounces; they calculate through an algorithm your reputation, which may lower if you are not using the mail properly. Once you get under 80% your account may be suspended permanently. Be sure that you are sending mails to existing recipients and do not begin to spam. These are two basic rules which will leave your reputation sufficiently high to be free of hassles.
Filename Size Downloads
1.02 MB 106
Eldenroot likes this post
Dr.Jeet Advanced 7 Mar 2017
#2
Is it free only for 1 month?
Shade Administrator 8 Mar 2017 Edited
#3
AFAIK, SendGrid is free when you send less than 10k mails per month, forever.
Shade Administrator 4 Sep 2017
#5
Honestly, I don't know. Ask their support board if they have one.
WallBB Advanced 16 Aug
#6
I use aws for most of my forums and it works amazing. For smaller forums, I will try sendgrid Yay
  • 4 participants