27

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?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Jeremy
  • 925
  • 2
  • 11
  • 22

5 Answers5

65

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' => 'you@yourdomain.example',
    'html' => '<p>this is a test message with Mandrill\'s PHP wrapper!.</p>',
    'to' => array(array('email' => 'recipient1@domain.example', 'name' => 'Recipient 1')),
    'merge_vars' => array(array(
        'rcpt' => 'recipient1@domain.example',
        '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));

?>
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Kaitlin
  • 6,167
  • 32
  • 30
  • 8
    As a note for future users, you will need to create a template in Mandrill found under the outbound -> template menu with the minimum html as follows:
    the template slug must match Stationary. Further information on templates can be found here http://help.mandrill.com/entries/21694286-How-do-I-add-dynamic-content-using-editable-regions-in-my-template-
    – Treemonkey Dec 06 '13 at 16:14
22

Mandrill take HTTP POST requests for all of their API methods, and they take your input as a JSON string. Here's a basic example of sending an email. It uses cURL to do the HTTP request:

$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';

$postString = '{
"key": "YOUR KEY HERE",
"message": {
    "html": "this is the emails html content",
    "text": "this is the emails text content",
    "subject": "this is the subject",
    "from_email": "someone@example.com",
    "from_name": "John",
    "to": [
        {
            "email": "blah@example.com",
            "name": "Bob"
        }
    ],
    "headers": {

    },
    "track_opens": true,
    "track_clicks": true,
    "auto_text": true,
    "url_strip_qs": true,
    "preserve_recipients": true,

    "merge": true,
    "global_merge_vars": [

    ],
    "merge_vars": [

    ],
    "tags": [

    ],
    "google_analytics_domains": [

    ],
    "google_analytics_campaign": "...",
    "metadata": [

    ],
    "recipient_metadata": [

    ],
    "attachments": [

    ]
},
"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);

echo $result;
MrCode
  • 63,975
  • 10
  • 90
  • 112
10
// Simply Send Email Via Mandrill...

require_once 'Mandrill.php';
$mandrill = new Mandrill($apikey);

$message = new stdClass();
$message->html = "html message";
$message->text = "text body";
$message->subject = "email subject";
$message->from_email = "address@example.com";
$message->from_name  = "From Name";
$message->to = array(array("email" => "recipient@example.com"));
$message->track_opens = true;

$response = $mandrill->messages->send($message);
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Muddasir Abbas
  • 1,699
  • 1
  • 20
  • 37
4

This is the most basic piece of code I could give you, I just craft it seconds ago for a client and it's working smooth.

require_once 'path/to/your/mandrill/file/Mandrill.php';
 try {
    $mandrill = new Mandrill('your-API-key');
    $message = array(
        'html' => $htmlMessage,
        'subject' => $subject,
        'from_email' => $fromEmail,
        'from_name' => $fromName,
        'to' => array(
            array(
                'email' => $toEmail,
                'name' =>  $toName,
                'type' => 'to'
            )
        )
    );
    $result = $mandrill->messages->send($message);
    print_r($result);
} catch(Mandrill_Error $e) {
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    throw $e;
}

Also check their send method for more options like headers, meta-data, attachments etc. https://mandrillapp.com/api/docs/messages.php.html#method-send

0x1ad2
  • 8,014
  • 9
  • 35
  • 48
2

Include the PHP API: https://bitbucket.org/mailchimp/mandrill-api-php

Code: https://mandrillapp.com/api/docs/messages.php.html#method-send

You can use ZF's autoloading for including the wrapper class or Composer: https://mandrillapp.com/api/docs/index.php.html

octavian
  • 321
  • 2
  • 11