Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
Mandrill Logo

Mandrill Logo

Mailchimp Logo

Mailchimp Logo

I recently wanted to use Mandrill and Mailchimp API to send emails from one of my websites instead of using default mail() method in PHP. There are many reasons for not using default PHP mail() function. But main reason is that if you’re using a VM to host your site instead of shared web hosting provided by web hosting provider you may have to install your own mail server in the VM to get this working. Setting up mail server is hardest thing that you could find to do on a server and even if you do there is a high chance of mails originating from your server blocked by main mail systems like gmail, yahoo, msn etc. In addition if you use third party email service (free for very good starter package and more than enough for normal web site) there are lot of tools you can utilize regarding the emails you sent, logs, security and many more. However there are few things you should know when configuring to use these APIs. One common error I found myself is “API call to messages/send failed: SSL certificate problem: unable to get local issuer certificate“. So I thought writing this post showing you how to fix it.

There are two ways to fix this as below.

By Configuring php.ini

If you have access to php.ini file in your system this will be the easiest method. You can find the location of active php.ini file by looking at phpinfo(); output. (I hope you know how to use phpinfo by adding dummy php page into the web root). However note that if you follow this method it will be applied to all sites hosted in the server which use PHP.

First you need to visit this url and download the CA (certificate authority) bundle file. Remember this is only to use with email service which uses curl. Mandril and Mailchimp use curl to write its email API clients so we can use the CA bundle file provided by curl in this location http://curl.haxx.se/docs/caextract.html. Download the  cacert.pem file and save to your server location.

Open the php.ini and look for following key, if you don’t find it add it.

If you want to use php-fpm you may need to restart it. Otherwise and always better to restart the web server (ex: Apache or Nginx) after this change.

Now once again try sending the email using Mandrill or Mailchimp API and you should see no error and successful delivery.

By Editing API Code

This is the method I used because I can control where to use it without applying it to entire hosting system. I hope you’re using the Mandrill or Mailchimp API client located here (https://bitbucket.org/mailchimp/mandrill-api-php).  As in previous method download the cacert.pem and keep it near where you have mandrill/mailchimp api client library files.

Then open the Mandrill.php or Mailchimp.php ( I will use Mandrill.php as an example but it is same for Mailchimp.php only the actual location is different). Locate where it has following lines of code. (https://bitbucket.org/mailchimp/mandrill-api-php/src/9f336b08ea148c80db3b694f3dc737cd4f414e19/src/Mandrill.php?at=master#cl-59)

Just after the first line add following set of lines as shown below.

Note that I have given the absolute path to the cacert.pem file. Once you have done this you can call the mail API and it should work without any errors.

 

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone