Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple php function to send an email with Mandrill

What is the easiest way to send an email via Mailchimp's Mandrill service (using the API).

Here's the send method: https://mandrillapp.com/api/docs/messages.html#method=send

Here's the API wrapper: https://bitbucket.org/mailchimp/mandrill-api-php/src/fe07e22a703314a51f1ab0804018ed32286a9504/src?at=master

But I can't figure out how to make an PHP function that will send and email via Mandrill.

Can anyone help?

like image 289
Jeremy Avatar asked Jan 23 '13 06:01

Jeremy


People also ask

How do I send an email using Mandrill API?

Click on “Launch Mandrill”. Click on “Setup your sending domain” button and add your domain name. It will ask you the email associated with the domain name in order to verify it. It will send you an email, click on the link to verify your account.

What is mandrill SMTP?

Mandrill by MailChimp is one of such options. It is a transactional mail distribution service that allows for sending up to 12,000 free e-mails per month. All you need is change SMTP credentials in your application or e-mail client.

What is mandrill mailer?

Mandrill is a transactional email API for MailChimp users. It's reliable, powerful, and ideal for sending data driven emails, including targeted e-commerce and personalized one-to-one messages.

How do I use mandrill in MailChimp?

Purchase a paid MailChimp account and integrate your existing paid Mandrill account into that. If you already have a paid MailChimp account, then login into that and integrate your paid Mandrill account with the MailChimp account. You can find the Mandrill option under Profile > Transactional menu.


1 Answers

We also have an official API wrapper for PHP, which is available on Bitbucket or via Packagist, which wraps the Mandrill API for you.

If your Mandrill API key is stored as an environment variable, here's a simple example of sending using a template, with some merge variables and metadata:

<?php require 'Mandrill.php';  $mandrill = new Mandrill();   // If are not using environment variables to specific your API key, use: // $mandrill = new Mandrill("YOUR_API_KEY")  $message = array(     'subject' => 'Test message',     'from_email' => '[email protected]',     'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',     'to' => array(array('email' => '[email protected]', 'name' => 'Recipient 1')),     'merge_vars' => array(array(         'rcpt' => '[email protected]',         'vars' =>         array(             array(                 'name' => 'FIRSTNAME',                 'content' => 'Recipient 1 first name'),             array(                 'name' => 'LASTNAME',                 'content' => 'Last name')     ))));  $template_name = 'Stationary';  $template_content = array(     array(         'name' => 'main',         'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, thanks for signing up.'),     array(         'name' => 'footer',         'content' => 'Copyright 2012.')  );  print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));  ?> 
like image 72
Kaitlin Avatar answered Sep 20 '22 16:09

Kaitlin