How to integrate Mandrill into your PHP site

May 18, 2013 | PHP, Time Savers, Tutorials | 4 comments

You might have heard of Mailchimp… who hasn’t these days! Well the same guys have released a rather nice little system called Mandrill (or did earlier this year at least). It is a transactional SMTP relay service with a rather nice little API for those who care.

For those that don’t give a monkeys (get it.. monkeys?!) I have a barebones little bit of PHP code for you to use which will effect the sending of your emails. Most sites using PHP have a central ‘mail’ command. There will be a function in a file or include somewhere which will handle the sending of mail using this sort of syntax:

mail($to, $subject, $body);

Quite simple.. it’s how PHP sends emails normally. The following code is to replace that mail command. Don’t forget to enter your API key!

$to = '';
$content = '<p>this is the emails html <a href="">content</a></p>';
$subject = 'this is the subject';
$from = '';

$uri = '';
$api_key = 'YOUR KEY HERE';
$content_text = strip_tags($content);

$postString = '{
"key": "' . $api_key . '",
"message": {
 "html": "' . $content . '",
 "text": "' . $content_text . '",
 "subject": "' . $subject . '",
 "from_email": "' . $from . '",
 "from_name": "' . $from . '",
 "to": [
 "email": "' . $to . '",
 "name": "' . $to . '"
 "track_opens": true,
 "track_clicks": true,
 "auto_text": true,
 "url_strip_qs": true,
 "preserve_recipients": true
"async": false

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);

You can also echo the $result variable to debug this code.. it will return a string of JSON with either a success message or an error. Rather useful I think!

A Donate Button!


  1. Louis

    Great code, only one bit of trouble. You need to escape your double quotes in the google URL in the body of the e-mail.

    Otherwise it works great!

  2. Bala

    nice oneeeeeee

  3. Anon

    Just wanted to post to thank you so incredibly much for this PHP script. It has helped me tremendously, as there is very little in the way of documented examples for those unfamiliar with Mandrill. Have a great one!

  4. Pablo

    Nice! works for me, thanks man!


Submit a Comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

Stay in touch!

Page Builder Cloud

Page Builder Cloud

A truly universal template library for WordPress Page Builders.

Page Builder Recommendation

Elementor Banner

We are BIG fans of the Elementor page builder. Give it a look!

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist and Co-Founder of Page Builder Cloud.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!