Mandrill email attachments file path

26,645

Solution 1

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.

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.

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

Solution 2

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:

$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 :

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

So easy! Thanks again Kaitlin!

Share:
26,645
Manu
Author by

Manu

Updated on August 03, 2020

Comments

  • Manu
    Manu almost 4 years

    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

    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:

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

  • Manu
    Manu about 11 years
    Thanks Kaitlin. Sorry for my ignorance. I am not familiar with using Mandrill and I have not base64 encoded anything or for that matter attached files to emails before. So far I am very impressed with Mandrill and appreciate your quick feedback!
  • Costa Michailidis
    Costa Michailidis almost 10 years
    Yeah, thanks for jumping into stackoverflow with us : ) This is super helpful.
  • CodeGuru
    CodeGuru almost 10 years
    Hi @Manu , what about decoding it ? How do I save the decoded base64?
  • Manu
    Manu almost 10 years
    I don't know what you are trying to achieve but this thread is about how to attach and send files using the mandrill api. When you receive the file you do not need to do anything to the file. It is available to download like any email attachment.
  • CodeGuru
    CodeGuru almost 10 years
    You mean Mandrill will provide a link for us to download it?
  • Manu
    Manu almost 10 years
    No. You get the attachment in your email like any attachment. Mandrill is provides a transport facility not a storage facility.