简体   繁体   English

Mandrill电子邮件附件文件路径

[英]Mandrill email attachments file path

I am trying to add some attachments to an email that is being sent using the mandrill api via a php wrapper. 我正在尝试将一些附件添加到通过php包装器使用mandrill api发送的电子邮件中。 I have tried a number of different things to try to successfully attach the file but to no avail. 我尝试了多种尝试来成功附加文件,但无济于事。 I am using cakephp 2.x but I don't think that has any particular significance in this instance (maybe it does?!). 我正在使用cakephp 2.x,但在这种情况下我认为这没有任何特别的意义(也许呢?!)。 I am using the php wrapper maintained by mandrill at https://bitbucket.org/mailchimp/mandrill-api-php 我正在使用由mandrill维护的php包装器, 网址https://bitbucket.org/mailchimp/mandrill-api-php

Here is the code: 这是代码:

$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);

}

This shows that an attachment has been added to the email and is a pdf but the actual pdf has not been attached. 这表明附件已添加到电子邮件中,并且是pdf,但实际的pdf尚未附加。 I also tried by adding the path directly onto the file as in: 我还尝试通过将路径直接添加到文件中来进行尝试,如下所示:

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

I have googled and read every article I can find but cannot find any specific reference as to how I should specify the path for mandrill to correctly attach my attachment. 我已经在Google上搜索并阅读了我能找到的每篇文章,但是找不到关于如何指定山d正确附着附件的路径的任何特定参考。

Any help will be greatly appreciated. 任何帮助将不胜感激。

OK. 好。 So thanks to Kaitlin for her input. 因此,感谢Kaitlin的投入。 The PHP way to deal with this is to get the file and then base64_encode it: PHP的解决方法是先获取文件,然后对其进行base64_encode:

$attachment = file_get_contents(WWW_ROOT.'files/downloads/file.pdf');
$attachment_encoded = base64_encode($attachment); 

and then in the attachments part of the mandrill array you pass the : 然后在mandrill数组的附件部分中传递:

"attachments" => array(
        array(
            'content' => $attachment_encoded,
            'type' => "application/pdf",
            'name' => 'file.pdf',
        )

So easy! 太简单! Thanks again Kaitlin! 再次感谢Kaitlin!

It looks like you're passing a parameter called path , but the Mandrill API doesn't take the path of a file for attachments. 看起来您正在传递一个名为path的参数,但是Mandrill API不会采用附件的文件路径。 If you're using the send or send-template call, attachments should be an associative array (hash) with three keys: type, name, and content. 如果您使用send或send-template调用,则附件应为具有三个键的关联数组(哈希):类型,名称和内容。

The content parameter should be the contents of the file as a Base64 encoded string, so instead of path, you'll want to get the file contents, Base64 encode them, and then pass them in a parameter called content instead of path . content参数应该是以Base64编码的字符串的形式作为文件的内容,因此,您需要获取文件内容,而不是path,对Base64进行编码,然后将它们传递给名为content的参数,而不是path

You can see the full details of the parameters, including for attachments, in the Mandrill API docs here: https://mandrillapp.com/api/docs/messages.html#method=send 您可以在此处的Mandrill API文档中查看参数的完整详细信息,包括附件的详细信息: https : //mandrillapp.com/api/docs/messages.html#method=send

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM