Sending emails from PHP is so straight forward and easy. PHP has an inbuilt function – mail(), to do this. But to send bulk emails you need a specialized infrastructure or you should rely on an API exposed by a bulk email service provider. Sendgrid, Mailchimp and mandrill are popular among such bulk email providers. Sending bulk emails using PHP with mandrill API is explained in this article.
What is Mandrill?
Mandrill is for transactional emails as how MailChimp is for marketing emails. Unlike marketing emails, transactional emails are sent to communicate some necessary information to the recipients. It may include registration confirmation or welcome emails, password reset emails, payment reminders, transaction success/failure notice, expiry notice, etc. In other words transactional emails are sent as a result of the recipient initiating an action (say registering on your site). And so these type of emails always have higher priority.
Sending Emails using Mandrill JSON API call
Of all the approaches, I found JSON the simplest way to send an email via Mandrill, especially using PHP. I have created this function as follows.
Sign Up for Mandrill to get the API key, if not already done.
You can include the above code in your script and call the sendBulkMandillEmail() function with required parameters to send bulk emails or single email at a time.
- $to_josn – JSON encoded array of email IDs of recipients. Ex:
"name": "Recipient Name",
- $subject – Subject line of the email.
- $html_content – HTML content of your email.
- $reply_to_email_id – Specify an email id to which replies are expected. Defaults to from email ID.
- $from – This will the shown as the sender email ID to recipients.
- $from_name – This will the shown as the sender name to recipients.
There are many other parameters apart from what I have used in the function definition. The parameters track_opens, track_clicks when set to true will help you track your outgoing emails. These stats can be found in Mandrill control panel. Also can be retrieved via reports API. The parameter auto_text can be set to true to auto generate text version of your HTML email. The url_strip_qs parameter can either be true or false and specifies whether to strip query strings from the URLs specified in the HTML content of your email. I advise you to set preserve_recipients to false when sending bulk emails. When set to true, the list of all recipients’ email ids will be displayed to every recipient in the list.
Mandrill provides so many other features in this API. I have covered only the basics. Check Mandrill Message call API for complete list of available features.
Why do I recommend Mandrill API for Sending Bulk emails with PHP?
- From the MailChimp group.
- Unlike other providers, they offer 12,000 free email credits per month to try out.
- Economic pricing.
- Wide range of APIs. Neatly categorized.
- APIs for retrieving reports.
- Easy to use dashboard.
- Precise documentation on the APIs, etc.