15

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
  • 559
  • 2
  • 8
  • 20
  • why is this tagged cakephp then? it would probably be wise to at least link what you are using. nobody of us has a crystal ball ;) – mark Mar 04 '13 at 23:46
  • 1
    I am using cakephp 2.x as my framework but I didn't think that was important. I thought it might be useful for future cakephp people attempting to use mandrill to find this reference. I will edit my post and mention that I am using cakephp. If you have experience using mandrill with cakephp I would appreciate the input. – Manu Mar 05 '13 at 00:26
  • I mean how can you ask a question about a class (Mandrill) that no-one has ever seen so far? its like me asking how I can fix my superAwesomeFooBar() method of my AdvancedSomething class without also mentioning the location of the source code.. You want to give it a shot? :) – mark Mar 05 '13 at 09:54
  • oh ok, the wrapper class is pretty generic I think. I was actually hoping to get an answer from the guys at mandrill. I have edited the question again with a link to the php wrapper: https://bitbucket.org/mailchimp/mandrill-api-php – Manu Mar 05 '13 at 12:10

2 Answers2

31

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!

Manu
  • 559
  • 2
  • 8
  • 20
  • Hi @Manu , what about decoding it ? How do I save the decoded base64? – CodeGuru Jun 12 '14 at 23:15
  • 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. – Manu Jun 14 '14 at 05:24
  • You mean Mandrill will provide a link for us to download it? – CodeGuru Jun 14 '14 at 06:25
  • 5
    No. You get the attachment in your email like any attachment. Mandrill is provides a transport facility not a storage facility. – Manu Jun 15 '14 at 07:49
28

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

Kaitlin
  • 5,921
  • 29
  • 29
  • 2
    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! – Manu Mar 06 '13 at 11:02
  • 2
    Yeah, thanks for jumping into stackoverflow with us : ) This is super helpful. – Costa Jun 05 '14 at 18:51