【问题标题】:Dynamic attachments mandrill动态附件山魈
【发布时间】:2016-10-25 08:55:09
【问题描述】:

我是 mandrill 的新手,我想知道我是否可以使用 mandrill 创建可以作为附件发送给收件人的模板。例如,如果我想发送带有动态内容的可下载活动门票。我正在考虑将活动门票设计为 mandrill 中的模板。

我的代码:

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

【问题讨论】:

    标签: php mailchimp mandrill


    【解决方案1】:

    您可以使用 render 返回 HTML 结果,然后使用 tcpdf 之类的库将 html 转换为 pdf,然后您可以将其作为附件发送

    <?php
    try {
    $mandrill = new Mandrill('YOUR_API_KEY');
    $template_name = 'Example Template';
    $template_content = array(
        array(
            'name' => 'editable',
            'content' => '<div>content to inject *|MERGE1|*</div>'
        )
    );
    $merge_vars = array(
        array(
            'name' => 'merge1',
            'content' => 'merge1 content'
        )
    );
    $result = $mandrill->templates->render($template_name, $template_content, $merge_vars);
    print_r($result);
    
    // 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
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
            // add a page
            $pdf->AddPage();
            // output the HTML content
            $pdf->writeHTML($result['html'], true, false, true, false, '');
            $file_name = 'filename.pdf';
            $path = $your_upload_path.$file_name;
            $pdf->Output($path,'F');
    
    
    /*
    Array
    (
        [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;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 2013-10-14
      • 2014-06-24
      • 2016-11-16
      • 2016-12-30
      • 2015-12-11
      • 1970-01-01
      • 2016-05-26
      • 2015-10-10
      相关资源
      最近更新 更多