簡體   English   中英

Mandrill電子郵件附件文件路徑

[英]Mandrill email attachments file path

我正在嘗試將一些附件添加到通過php包裝器使用mandrill api發送的電子郵件中。 我嘗試了多種嘗試來成功附加文件,但無濟於事。 我正在使用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);

}

這表明附件已添加到電子郵件中,並且是pdf,但實際的pdf尚未附加。 我還嘗試通過將路徑直接添加到文件中來進行嘗試,如下所示:

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

我已經在Google上搜索並閱讀了我能找到的每篇文章,但是找不到關於如何指定山d正確附着附件的路徑的任何特定參考。

任何幫助將不勝感激。

好。 因此,感謝Kaitlin的投入。 PHP的解決方法是先獲取文件,然后對其進行base64_encode:

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

然后在mandrill數組的附件部分中傳遞:

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

太簡單! 再次感謝Kaitlin!

看起來您正在傳遞一個名為path的參數,但是Mandrill API不會采用附件的文件路徑。 如果您使用send或send-template調用,則附件應為具有三個鍵的關聯數組(哈希):類型,名稱和內容。

content參數應該是以Base64編碼的字符串的形式作為文件的內容,因此,您需要獲取文件內容,而不是path,對Base64進行編碼,然后將它們傳遞給名為content的參數,而不是path

您可以在此處的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