Mandrill电子邮件附件文件路径

Man*_*anu 13 php email cakephp email-attachments mandrill

我正在尝试添加一些附件到使用mandrill api通过php包装器发送的电子邮件.我尝试了很多不同的东西来尝试成功附加文件,但无济于事.我正在使用cakephp 2.x,但我不认为在这种情况下有任何特别的意义(也许它确实如此?!).我正在使用由mandrill维护的php包装器,网址https://bitbucket.org/mailchimp/mandrill-api-php

这是代码:

$mandrill = new Mandrill(Configure::read('Site.mandrill_key'));
    $params = array(
        'html' => '
            <p>Hi '.$user['User']['name'].',</p>
            <p>tIt is that time of the year again.<br />
            <a href="http://my-site.com/members/renewal">Please login to the website members area and upload your renewal requirements</a>.</p>
            <p>Kind regards.</p>',
        "text" => null,
        "from_email" => Configure::read('Site.email'),
        "from_name" => Configure::read('Site.title'),
        "subject" => "Renewal Pending",
        "to" => array(array('email' => $user['User']['email'])),
        "track_opens" => true,
        "track_clicks" => true,
        "auto_text" => true,
        "attachments" => array(
            array(
                'path' => WWW_ROOT.'files/downloads/renewals',
                'type' => "application/pdf",
                'name' => 'document.pdf',
            )
        )
    );

    $mandrill->messages->send($params, true);

}
Run Code Online (Sandbox Code Playgroud)

这表明附件已添加到电子邮件中并且是pdf但实际的pdf尚未附加.我还尝试将路径直接添加到文件中,如下所示:

"attachments" => array(
            array(
                'type' => "application/pdf",
                'name' => WWW_ROOT.'files/downloads/renewals/document.pdf',
            )
Run Code Online (Sandbox Code Playgroud)

我用Google搜索并阅读了我能找到的每篇文章,但是找不到任何具体的参考资料,说明我应该如何指定mandrill正确附加我的附件的路径.

任何帮助将不胜感激.

Man*_*anu 28

好.感谢Kaitlin的投入.处理这个问题的PHP方法是获取文件,然后使用base64_encode:

$attachment = file_get_contents(WWW_ROOT.'files/downloads/file.pdf');
$attachment_encoded = base64_encode($attachment); 
Run Code Online (Sandbox Code Playgroud)

然后在mandrill数组的附件部分中传递:

"attachments" => array(
        array(
            'content' => $attachment_encoded,
            'type' => "application/pdf",
            'name' => 'file.pdf',
        )
Run Code Online (Sandbox Code Playgroud)

太简单!再次感谢凯特琳!

  • 不会。您在电子邮件中收到附件就像任何附件一样。Mandrill 提供运输设施而不是储存设施。 (3认同)

Kai*_*lin 26

看起来您正在传递一个名为的参数path,但Mandrill API不会将文件的路径作为附件.如果您正在使用send或send-template调用,则附件应该是具有三个键的关联数组(哈希):类型,名称和内容.

content参数应该是文件的内容作为Base64编码的字符串,因此您需要获取文件内容而不是路径,Base64对它们进行编码,然后在名为content而不是的参数中传递它们path.

您可以在此处的Mandrill API文档中查看参数的完整详细信息,包括附件:https://mandrillapp.com/api/docs/messages.html#method=send

  • 谢谢凯特琳.对不起我的无知.我不熟悉使用Mandrill,我之前没有base64编码任何东西或附件文件到电子邮件.到目前为止,我对Mandrill印象非常深刻,感谢您的快速反馈! (2认同)
  • 是的,感谢您与我们一起进入 stackoverflow :) 这非常有帮助。 (2认同)