kelvin dev kelvin dev - 1 year ago 91
PHP Question

Dynamic attachments mandrill

I am new to mandrill and just wondering can i use mandrill to create templates that i can send to recipients as attachments. For instance if i wanted to send a downloadable event ticket with dynamic content. I was thinking of designing the event ticket as a template in mandrill

My code

$template_name = 'eventTicket';
$template_content = array(
'name' => 'example name',
'content' => 'example content'

Answer Source

You can return HTML results using render then use a library like tcpdf to convert the html to pdf which you can then send as an attachment

try {
$mandrill = new Mandrill('YOUR_API_KEY');
$template_name = 'Example Template';
$template_content = array(
        'name' => 'editable',
        'content' => '<div>content to inject *|MERGE1|*</div>'
$merge_vars = array(
        'name' => 'merge1',
        'content' => 'merge1 content'
$result = $mandrill->templates->render($template_name, $template_content, $merge_vars);

// create new PDF document
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        // set font
        $pdf->SetFont('helvetica', '', 10);
        // set image scale factor

        // add a page
        // output the HTML content
        $pdf->writeHTML($result['html'], true, false, true, false, '');
        $file_name = 'filename.pdf';
        $path = $your_upload_path.$file_name;

    [html] => content to inject merge1 content
} 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_Invalid_Key - Invalid API key
 throw $e;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download