Email WordPress Posts to Subscribers with Mandrill and MailPoet

- 32 comments

Development,Snippets,WordPress,WordPress Filters

My goal for a recent project was to setup a way to send reliable, custom emails each time a new WordPress post is published. Sadly, that rules out leveraging Jetpack because its email templates cannot be customized. Enter MailPoet, arguably one of the most feature rich freemium newsletter and email plugins for WordPress.

The Problem

Email, especially WordPress email, is a tough beast to tackle, and a reliable method to email WordPress posts to subscribers can prove elusive. Many emails get flagged as spam or are rejected entirely and rendered undeliverable. Read more about how Mandrill makes quick work of increasing your WordPress email deliverability using the same engine that powers MailChimp. Best of all, the Mandrill development team created a free plugin to integrate Mandrill with WordPress: wpMandrill.

Enter Mandrill & MailPoet

Daniel Klose wrote a great tutorial on how to setup Mandrill and MailPoet to send free newsletters with WordPress. Although it doesn’t discuss why you might want to do that, it’s a great starting point if your WordPress site will involve voluminous transactional emails—new post notifications, membership confirmations and notices, invoices, shipping updates, etc. Basically any automated emails you plan to send are good candidates for deliverability issues which Mandrill aims to reduce significantly.

Shared Hosting Problems

Klose’s MailPoet and Mandrill tutorial configures MailPoet to use Mandrill via SMTP. Unfortunately, many shared hosts block the ports required for WordPress to connect with an external SMTP server, which is where I ran into difficulty. MailPoet does offer the ability to send emails through GMail, but it’s recommended to have a paid GMail account if you plan on significant email volume, which wasn’t exactly keeping in the spirit of sending free newsletters reliably for my budget-minded client.

MailPoet comes with the native ability to use PHP’s mail() or sendmail() functions, claiming the functions work on 95% and 5% of servers respectively. I think we can do better than that. So I dug around…

wpMandrill actually “intercepts” WordPress’s mail function and uses the Mandrill API instead. Unfortunately, this only works when we call WordPress’s own mail function (wp_mail()). MailPoet does not provide us with the ability to use wp_mail(), so it looks like we’re stuck. Or are we?

MailPoet Tweak

I sifted through the MailPoet code and found an interesting line:

'allow_wpmail' => false1

After some more digging, I noticed that the complete infrastructure is in place to enable MailPoet to send emails with wp_mail(), which could then magically be intercepted by wpMandrill—all without the need to pay for GMail or a VPS! A quick consult with the MailPoet support team led me to create a function which enables wp_mail() in MailPoet and remains update proof. So I added this to my functions.php:

Now when we go to the settings (MailPoet->Settings->Send With…->Your own website) we see the two default options are now three beautiful options:

mailpoet_settings

Choose WP Mail, save the settings, and send yourself a test. Now you should be off and running, ready to email WordPress posts to subscribers with Mandrill and MailPoet on your shared host, even if your outgoing ports are blocked!

Thanks to MailPoet and Rafael for helping me navigate the MailPoet codebase.

Bonus

This function makes it possible to adjust any of the parameters in the MailPoet $defaults array2. Some of them are a bit cryptic, so the MailPoet support desk was kind enough to provide more details on the MailPoet hidden options. Just be sure to test your settings on a non-production installation first!


Comments

  1. Thanks for your efforts, much appreciated. I entered your snippet which worked beautifully to bring up the use wp-mail option. My question is a little different. I currently have Mailpoet but it delivers mail with red padlocks saying the mail is not verified by the server. I installed POSTMAN SMTP which adds Gmail API. CF7 contact form and test mail is clean and verified by the API. POSTMAN SMTP actually replaces wp_mail so i thought your snippet might help. Sadly i get an error from Mailpoet when test email is attempted. Should i be talking to the people at Postman? or do you have some advice, best regards

  2. You are a wonderful example of the spirit of open source collaboration. It took you a lot of effort to discover and develop this and you give it away for free; and I bet that is partially due to the great benefit you have received from other open-source work (ie WordPress and others).

    This is great work, explained very nicely and made very simple for us to use. Thank you.

  3. You are an absolute genius!! I have been fighting with this for over a day and just happened upon this post after Googling in desperation! Thank you sooo much! :):):)

  4. I cannot for the life of me get this to work. I added the code to functions.php, and it’s still not showing the 3rd option. Also not quite sure where to add the allow_wpmail => true code as well.

    Forgive my ignorance. Please advise.

  5. Michael, your post is worth its weight in gold! It fixed the unfixable!

    I’m not extremely experienced with PHP, so to my untrained eye, what your functions.php code snippet does is just changes allow_wpmail from “false” to “true”, right? Is this something that can be achieved simply by going into the Mailpoet config.php and making the change manually? Would the only reason to put the code into functions.php to make it update-proof in case Mailpoet updates itself & reverts the change? Or is there something else going on?

    Thanks!

    1. Thanks, glad it helped. Correct, you add this snippet to functions.php so that the change will persist even if you update the plugin. I mention that in the comment just above yours and there are other comments above with some links to more info on functions.php which you may find useful as a PHP newcomer. Enjoy!

  6. You didn’t place that coding into “functions.php”. You place it under “wysija-newsletters/index.php”. I was on a wild goose chase looking for “functions.php” in the long list of Plugin Files.

    1. See this comment above. If you add the snippet directly to any of the plugin files then you will lose the code because the files will be overwritten when you update the plugin. Adding the code to functions.php ensures that the change will persist after plugin updates.

  7. Oh my goodness! You are a lifesaver! Thank you so much for this. I have bluehost and my emails stopped working. I set up mandrill and I thought all was well except my newsletters no longer worked. I thought I was going to have to change services from mailpoet even though I’d already written a well thought out newsletter. Your simple workaround saved me so much aggravation!

  8. Adding the snippet didn’t show the send method options, I saw the DOM and noticed that the element was there but hidden, if the options of mail engine won’t show to you, you can open the console and execute this:

    jQuery('.choice-sending-method-site').show();
    
    1. Hi Marco, with the latest version of WordPress (4.1.1) and MailPoet (2.6.15) I see the snippet still working. I even see MailPoet’s WP Mail send option when I remove the snippet, so it appears MailPoet may now show this option by default.

      If you’re not seeing the entire div.choice-sending-method-site then something may be wrong with your installation.

      You may need to: (1) update WordPress; (2) update MailPoet; or (3) try disabling all other plugins on your site to rule out any conflicts. A healthy installation should not experience the issue you reported.

  9. After Adding the code snippet to my themes functions.php, My site stopped loading entirely.
    i had to upload a backup of the functions.php before it will load again.
    On testing locally, it said error unexpected ‘ ‘ in php
    The code snippet was pasted after the previous codes there

  10. Hi,

    “So I added this to my functions.php ”

    Where is that “functions.php” file? do we have to create it? I do not find it.

    I really do not understand the process:

    Do we have to set “‘allow_wpmail’ => false” to “allow_wpmail’ => true” ? or leave it as it is?

    Do we have to create a file called “mailpoet-wp_mail.php” with that code?

    Sorry but I don’t get it, could yo please explain those details?

    Thanks
    Mario

    1. Hey dontforgetmario,

      Your functions.php should be inside your theme directory and is used to change default WordPress behavior. See more here: http://codex.wordpress.org/Functions_File_Explained. If it’s not there you can simply create one. Then you can just copy and paste my snippet into that file and be sure to set 'allow_wpmail' => true if you’re experiencing the issue I discussed.

  11. a sigh of relief when i saw this post with a hope that you will reply to my problem.
    I am also using mailpoet with mandrill. All is working fine, but
    the problem is that I am unable to send bulk emails. Like if i want to send bulk resend confirmation link, smtp gives an error, but if i send them individually, it works like charm.
    I am on godaddy’s shared hosting, and all my transactional mail are working fine.

    What do you suggest me to do in this case? because i think the tweak you gave is for those whose port is blocked and no mail is going out.

    I consulted with mailpoet and they reffered me to this article, but my situation is different.

    please help.

  12. Quick question:

    How does the wpMandrill plugin setting affect your MailPoet Tweak above?
    I implemented what you suggested above and it worked without installing the wpMandrill plugin, so I am kind of confused how the wpMandrill plugin and your tweak interact each other.

    Please let me know. Thank you!

    1. Hi Chloe, see my section called “The Problem.”

      If you’re just using MailPoet and you don’t care about your transactional emails’ deliverability then there is no need to use wpMandrill. If you do care, and you do use wpMandrill, and your host blocks certain outgoing ports, then allow_wpmail can help you get around that. Does that make sense?

  13. Nice digging! WP Engine’s 3rd party email provider went down on wednesday causing a client to phone in that they weren’t getting quote requests. They recommended Mandrill. Might have to start down that route soon! Thanks!

Leave a Comment