2

I am trying to add an excel file (.xlsx) as an attachment to the email I am sending out through the Mandrill API. I am using CURL in a php file to send the email. The excel file is named Report.xlsx.

Here is a link to Mandrill API to use CURL. Here is a link an other question going over adding file paths.


I receive the following error message:

PHP Parse error: syntax error, unexpected 'path' (T_STRING) in /var/www/html/newFolder/EmailAlertSystem/mailchimp-mandrill-api-php/UsageReport.php

(****Which is my directory of my code)


This is my php code to send out the email via Mandrill:

$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$api_key = 'APIKEY';
$content = 'all the content I want to add';
$content_text = strip_tags($content);
$from = 'FROM';
$fromName = 'FROMNAME';
$to = 'TO';
$toName = 'TONAME';
$subject = 'SUBJECT';
$fullName = "FIRSTNAME LASTNAME";
$attachment = file_get_contents('Report.xlsx');
$attachment_encoded = base64_encode($attachment); 


$postString = '{
"key": "' . $api_key . '",
"message": {
 "html": "' . $content . '",
 "text": "' . $content_text . '",
 "subject": "' . $subject . '",
 "from_email": "' . $from . '",
 "from_name": "' . $fromName . '",
 "to": [
 {
 "email": "' . $to . '",
 "name": "' . $fullName . '"
 }
 ],
 "track_opens": true,
 "track_clicks": true,
 "auto_text": true,
 "url_strip_qs": true,
 "preserve_recipients": true,
 "attachments" : array(
        array(
            'path' => $attachment_encoded,
            'type' => "application/xlsx",
            'name' => 'Report.xlsx',
        )
    )




},
"async": false
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);

//this is the executable curl statement that will actually send the email
$result = curl_exec($ch);

Any help would be greatly appreciated!!! Please let me know if I was unclear and what I am doing wrong. Thank you in advance!

Community
  • 1
  • 1
MadsterMaddness
  • 725
  • 3
  • 12
  • 35
  • 1
    You open `$postString` with a single quote. Using a single quote for the array keys tells PHP you are ending the string, which is not your intent. (Notice the syntax highlighting in the code you've posted). You'll need to escape the single quotes or use double quotes like you have for objects. For example: `"name" => "Report.xlsx",` or `\'name\' => \'Report.xlsx\',`. – showdev Jan 14 '15 at 19:06
  • Attachments must be part of the message: https://stackoverflow.com/questions/26309297/mandrill-attachments-not-sending-attachment – Fede Oct 24 '17 at 16:24

1 Answers1

4

The error seems to refer to this part

 "attachments" : array(
        array(
            'path' => $attachment_encoded,
            'type' => "application/xlsx",
            'name' => 'Report.xlsx',
        )
    )

Right there, the string is terminated before path and then restarted, its a syntax error.

Something like,

 "attachments" : array(
        array(
            \'path\' => $attachment_encoded,
            \'type\' => "application/xlsx",
            \'name\' => \'Report.xlsx\',
        )
    )

i.e. having the quotes escaped should fix it, but... the rest of that string looks like JSON. Is the attachments portion supposed to be in PHP-like array format? Might want to double-check.