Click anywhere to close this dialog

Farewell

Great is the art of beginning, but greater is the art of ending
Henry Wadsworth Longfellow

I announce that I cease all development and activity in the programming universe indefinitely. My career has reached the turning point I was not expecting for at least another year, leaving me highly off guard and without laid-out plans for this hobby's continuity. I have begun a 5-year residency program in Neurosurgery which is clearly not compatible, time-wise, with programming.

I gave in all my passion for developing, and you gave me back your loyalty and trust, even when I did not deserve that much. Now it is the time for payback. I release all my present and past work as Open Source software, in the hope some talented developer will continue maintaining and expanding my vision of a modern, sleek forum software. The intrinsic flexibility of MyBB is the true hidden gem of an otherwise outdated codebase; I do hope the project can continue and be updated complying to the latest coding standards.

I hereby thank Euan, kawaii, andrewjs18, Ben, Matt, Omar G., effone, Eric J., Devilshakerz, Wildcard, JordanMussi and all the other team members I have had the opportunity to work with when I was a MyBB team member. I thank Tomm M, my mentor, who inspired me to pick up coding with his piece-of-art plugins. And finally, I thank all of you MyBBoost subscribers who have helped me getting through my toughest university years economically.

Yours sincerely, Filippo

Use SendGrid to send emails through MyBB

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 806
Eldenroot and phpkiller like this post
abid khan 11 Jun 2020
#11
Hello again,

Key to success is keep trying.

After trying so many times with SMTP settings and keep failing, I thought to try your hack one more time and guess what, this time the email lands directly into my inbox, really happy to see magic works.

Thank You so much for your time for the Mybb community.

May you get more success and fame.

Love,
Abid Khan