php Mandrill 电子邮件附件文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15213151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:47:39  来源:igfitidea点击:

Mandrill email attachments file path

phpemailcakephpemail-attachmentsmandrill

提问by Manu

I am trying to add some attachments to an email that is being sent using the mandrill api via a php wrapper. 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?!). I am using the php wrapper maintained by mandrill at https://bitbucket.org/mailchimp/mandrill-api-php

我正在尝试将一些附件添加到通过 php 包装器使用 mandrill api 发送的电子邮件中。我尝试了许多不同的方法来尝试成功附加文件,但无济于事。我正在使用 cakephp 2.x,但我认为在这种情况下没有任何特别的意义(也许确实如此?!)。我在https://bitbucket.org/mailchimp/mandrill-api-php使用 mandrill 维护的 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. I also tried by adding the path directly onto the file as in:

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

"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.

我已经用谷歌搜索并阅读了我能找到的每一篇文章,但找不到任何关于我应该如何指定山魈正确附加我的附件的路径的具体参考。

Any help will be greatly appreciated.

任何帮助将不胜感激。

回答by Manu

OK. So thanks to Kaitlin for her input. The PHP way to deal with this is to get the file and then base64_encode it:

好的。所以感谢 Kaitlin 的投入。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!

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

回答by 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. If you're using the send or send-template call, attachments should be an associative array (hash) with three keys: type, name, and content.

看起来您正在传递一个名为 的参数path,但 Mandrill API 不采用附件文件的路径。如果您使用 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 contentinstead of path.

content 参数应该是作为 Base64 编码字符串的文件内容,因此您需要获取文件内容,而不是路径,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