Send email with template (Mandrill PHP)

15,408

You can send the email and use the template by using the Mandrill PHP API wrapper.

require 'Mandrill.php';

$mandrill = new Mandrill('YOUR_API_KEY');

$message = array(
    'subject' => 'My subject',
    'from_email' => '[email protected]',
    'to' => array(array('email' => '[email protected]', 'name' => 'Marc')),
    '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 = 'YOUR-TEMPLATE-NAME';

$template_content = array(
    array(
        'name' => 'main',
        'content' => 'Hi *|FIRSTNAME|* *|LASTNAME|*, thanks for signing up.'),
    array(
        'name' => 'footer',
        'content' => 'Copyright 2013.')

);

$response = $mandrill->messages->sendTemplate($template_name, $template_content, $message);
print_r($response);

If you want to use the SMTP via SwiFtMailer, you could call the Render API method to render a template, which will give you the full HTML, which you can pass to SwiftMailer but that seems a bit of a long winded way of doing it compared to the PHP API wrapper.

Share:
15,408

Related videos on Youtube

Marckaraujo
Author by

Marckaraujo

Just me.

Updated on June 04, 2022

Comments

  • Marckaraujo
    Marckaraujo almost 2 years

    I have created a template in Mandrill but I don't know how to use to send email.

    Here is an example how to use it with a simple html:

    <?php
    
    include_once "swift_required.php";
    
    $subject = 'Hello from Mandrill, PHP!';
    $from = array('[email protected]' =>'Your Name');
    $to = array(
     '[email protected]'  => 'Recipient1 Name',
     '[email protected]' => 'Recipient2 Name'
    );
    
    $text = "Mandrill speaks plaintext";
    $html = "<em>Mandrill speaks <strong>HTML</strong></em>";
    
    $transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
    $transport->setUsername('MANDRILL_USERNAME');
    $transport->setPassword('MANDRILL_PASSWORD');
    $swift = Swift_Mailer::newInstance($transport);
    
    $message = new Swift_Message($subject);
    $message->setFrom($from);
    $message->setBody($html, 'text/html');
    $message->setTo($to);
    $message->addPart($text, 'text/plain');
    
    if ($recipients = $swift->send($message, $failures))
    {
     echo 'Message successfully sent!';
    } else {
     echo "There was an error:\n";
     print_r($failures);
    }
    
    ?>
    
  • Marckaraujo
    Marckaraujo almost 11 years
    thanks, its working, just a correction: $mandrill = new Mandrill('YOUR_API_KEY');
  • Marckaraujo
    Marckaraujo almost 11 years
    one more info, I have set |pspReference| inside html template and in merge_vars I used: 'name' => 'pspReference', 'content' => 'Hello world'), but it don't change the value, do you know why? thanks
  • Hitesh Modha
    Hitesh Modha almost 10 years
    i got this response { status: 0, message: "API call to messages/send-template failed: SSL certificate problem: unable to get local issuer certificate" }
  • Marckaraujo
    Marckaraujo over 9 years
    @HiteshModha, This issue is because your website needs to be under SLL certicate (HTTPS).