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

How to attach excel file in Mandrill using php curl

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

Here is a link 到 Mandrill API 以使用 CURL。 Here is a link 另一个关于添加文件路径的问题。


我收到以下错误消息:

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

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


这是我的 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',
        )
    )

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

有点像,

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

即转义引号应该可以修复它,但是......该字符串的其余部分看起来像 JSON。附件部分是否应该采用类似 PHP 的数组格式?可能需要仔细检查。