I am trying Mandrill for sending email from WordPress. It’s a nice service from Mailchimp for sending transactional mail. The best part is they allow 12,000 free mail/month 😀
The problem is, every email in WordPress is plain text. As I am using WooCommerce, the order emails of WooCommerce is HTML and Mandrill also sends email as HTML.
Although mandrill has this settings, they contradicts with HTML and Plain text emails at the same time. So the trick is to treat WooCommerce order emails specially.
[php]
function wd_mandrill_woo_order( $message ) {
if ( in_array( ‘wp_WC_Email->send’, $message[‘tags’][‘automatic’] ) ) {
$message[‘html’] = $message[‘html’];
} else {
$message[‘html’] = nl2br( $message[‘html’] );
}
return $message;
}
add_filter( ‘mandrill_payload’, ‘wd_mandrill_woo_order’ );
[/php]
This code will pass order emails as HTML and normal plaintext emails new line will be converted as line break and they can live happily ever after 😀
So out-of-the-box wpMandrill will mess up my receipt emails that WooCommerce sends?
Out-of-the-box, it won’t. But it’ll mess up the standard WordPress mails, cause they will be sent as HTML emails. So the line breaks won’t be there for standard mails. This trick is to both work the standard emails and also the HTML mails for WooCommerce order.
Okay thanks. Does mandrill allow you to edit the woocommerce templates using mandrill’s system, or does it only act as a mail server / analytics?
Mandrill acts as a mail server and analytics. Also it has a templating system, but not sure how it works.
You can edit add a custom html template which will over ride the header and footer with your custom code.
The body of the email remains the same.
Great snippet! I’m also using WC and Mandrill – thanks for sharing 🙂
Thanks, that was very helpful!
Actually, maybe I spoke too soon… Although unchecking that setting makes WooCommerce emails look much better, I don’t think the snippet worked quite right because now regular plain text emails don’t have line breaks anymore…
Where do I put this code?
In your themes functions.php perhaps.
Tareq – thanks for sharing, this was really useful.
To clarify, the code needs to go in your themes functions.php file, and you will also need to uncheck the “Replace all line feeds (“\n”) by in the message body?” option within wpMandrill config settings (Settings -> Mandrill).
If you’re still not seeing the changes, make sure you’ve cleared your cache (and WP’s object cache and anything else, e.g. Cloudflare cache).
Best wishes,
Tim Durden
Twitter: @tjdurden
Thanks a lot Tareq and Tim
Do mailchimp really allow 12000 free mails monthly?
Mandrill changed their plan from last month and merged with Mailchimp as a paid add-on. Yes, it was free before that and was an excellent service.
Hello, if, like me, you’re using the mandrill templates and are confused about how to let some plugins like woocommerce use their template system, you can use this. Update it to add the right tags. Pay attention some plugins don’t add them well.
// Hook into the mandrill_payload filter.
add_filter(‘mandrill_payload’, ‘kainjoo_force_native_mail_for_woocommerce’);
/**
* Forces WooCommerce emails to use the native wp_mail() function.
*
* @param array $message The Mandrill payload.
* @return array Modified Mandrill payload.
*/
function kainjoo_force_native_mail_for_woocommerce($message) {
// Check if the email is from WooCommerce.
if (isset($message[‘tags’]) && is_array($message[‘tags’]) && in_array(‘woocommerce’, $message[‘tags’])) {
$message[‘force_native’] = true;
}
return $message;
}
?>