Movak
Movak

Reputation: 115

Mandrill- Attachments not sending attachment

I am attempting to send a small rtf attachment through Mandrill. I have created the following json and tried it using the API test page. The attachment is base 64 encoded. The API reports no error and the email comes through but with no attachment. What am I doing wrong?

{
    "attachments": [
    {
        "type": "application/rtf",
        "name": "test.rtf",
        "content": "e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcZGVmbGFuZzIwNTd7XGZvbnR0Ymx7XGYwXGZuaWxcZmNoYXJzZXQwIENhbGlicmk7fX0NCntcKlxnZW5lcmF0b3IgTXNmdGVkaXQgNS40MS4yMS4yNTEwO31cdmlld2tpbmQ0XHVjMVxwYXJkXHNhMjAwXHNsMjc2XHNsbXVsdDFcbGFuZzlcZjBcZnMyMiB0aGlzIGlzIGEgdGVzdCBzZW5kaW5nIGZpbGVccGFyDQp9DQoA"
    }
],
"message": {
    "html": "<html>\r\n<body>test data</body>\r\n</html>\r\n",
    "subject": "Cloud Demo",
    "from_email": "[email protected]",
    "preserve_recipients": true,
    "text": "",
    "to": [
        {
            "type": "to",
            "name": "",
            "email": "[email protected]"
        }
    ],
    "from_name": "",
    "headers": {
        "reply-to": "[email protected]"
    }
},
"key": #mykey#,
"async": false

}

Upvotes: 0

Views: 3569

Answers (2)

<?php
//It works for me! good luck

/*LIBS*/
include 'lib/mandrill-api-php/src/Mandrill.php';
$mandrill = new Mandrill('YOUR API KEY HERE');

/*ADMIN AND USER EMAIL*/
$admin_email = 'your_email@your_domain.com';
$client_email = '[email protected]';

/*attach PDF with base64_encode */
$attachment = file_get_contents('the_route_to_your_pdf'); // https://yourdomain/pdf_folder/mypdf.pdf
$attachment_encoded = base64_encode($attachment);

try{
    $user_message = array(
        'subject' => 'Your subject',
        'from_email' => $admin_email,
        'from_name' => 'my_domain_for_example',
        'html' =>  '<p>HTML template</p>',
        'to' => array(array('email' => $client_email, 'name' => 'Recipient 1')),
        'merge_vars' => array(array(
            'rcpt' => '[email protected]',
            'vars' =>
            array(
                array(
                    'name' => 'FIRSTNAME',
                    'content' => 'Recipient 1 first name'),
                array(
                    'name' => 'LASTNAME',
                    'content' => 'Last name')
            ))),
        'attachments' => array(
            array(
                'content' => $attachment_encoded,
                'type' => "application/pdf",
                'name' => 'the_name_of_the_attach.pdf',
            ))
    );

    $res_user_mandrill = $mandrill->messages->send($user_message, $async=false, $ip_pool=null, $send_at=null);

} catch(Mandrill_Error $e) {

}

?>

Upvotes: -1

Kaitlin
Kaitlin

Reputation: 6235

Attachments are part of the message object, so the attachments parameter should be nested under the message instead of at the same level. It should look like this instead:

{
    "message": {
        "attachments": [
            {
                "type": "application/rtf",
                "name": "test.rtf",
                "content": "e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcZGVmbGFuZzIwNTd7XGZvbnR0Ymx7XGYwXGZuaWxcZmNoYXJzZXQwIENhbGlicmk7fX0NCntcKlxnZW5lcmF0b3IgTXNmdGVkaXQgNS40MS4yMS4yNTEwO31cdmlld2tpbmQ0XHVjMVxwYXJkXHNhMjAwXHNsMjc2XHNsbXVsdDFcbGFuZzlcZjBcZnMyMiB0aGlzIGlzIGEgdGVzdCBzZW5kaW5nIGZpbGVccGFyDQp9DQoA"
            }
        ],
        "html": "<html>\r\n<body>test data</body>\r\n</html>\r\n",

....

Upvotes: 2

Related Questions