如何使用php curl在Mandrill中附加excel文件

我正在尝试添加一个excel文件(.xlsx)作为我通过Mandrill API发送的电子邮件的附件。 我在一个PHP文件中使用CURL来发送电子邮件。 excel文件被命名为Report.xlsx。

这里是 Mandrill API使用CURL 的链接 。 这是一个链接另一个问题,通过添加文件path。


我收到以下错误信息:

PHPparsing错误:语法错误,/var/www/html/newFolder/EmailAlertSystem/mailchimp-mandrill-api-php/UsageReport.php中意外的“path”(T_STRING)

(****这是我的代码的目录)


这是我的php代码发送电子邮件通过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); 

任何帮助将不胜感激!!! 请让我知道,如果我不清楚,我做错了什么。 先谢谢你!

错误似乎是指这个部分

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

在那里,string在path之前终止,然后重新启动,这是一个语法错误。

就像是,

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

即引号逃脱应该修复它,但是…该string的其余部分看起来像JSON。 附件部分应该是PHP类似的数组格式? 可能要仔细检查。