Sending mail with Mandrill & PHP (MailChimp)

Hi there

Short post about sending mail through the Mandrill-system.

“Mandrill is a scalable and affordable email infrastructure service, with all the marketing-friendly analytics tools you’ve come to expect from MailChimp.”

Download the latest release (working 2014-16-04). Grab the /src.

Create an API-key @ https://mandrillapp.com/settings/index

Put for example mail.php (code below) in the root folder (with /src).

Full API docs.

<?php

require_once 'src/Mandrill.php'; 

try {
    $mandrill = new Mandrill('YOUR_API_KEY');
    $message = array(
        'html' => '<p>Example HTML content</p>',
        'text' => 'Example text content',
        'subject' => 'example subject',
        'from_email' => 'sender.email@example.com',
        'from_name' => 'Example Name',
        'to' => array(
            array(
                'email' => 'recipient.email@example.com',
                'name' => 'Recipient Name',
                'type' => 'to'
            )
        ),
        'headers' => array('Reply-To' => 'message.reply@example.com'),
        
       
        
    );
    $async = false;
    $result = $mandrill->messages->send($message, $async);
    print_r($result);
    /*
    Array
    (
        [0] => Array
            (
                [email] => recipient.email@example.com
                [status] => sent
                [reject_reason] => hard-bounce
                [_id] => abc123abc123abc123abc123abc123
            )
    
    )
    */
} catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}
?>