如何使用php curl在Mandrill中附加excel文件
我正在尝试将excel文件(.xlsx)作为附件添加到通过Mandrill API发送的电子邮件中。我正在使用php文件中的CURL发送电子邮件。excel文件名为Report.xlsx 要使用CURL,请使用Mandrill API。 关于添加文件路径的另一个问题如何使用php curl在Mandrill中附加excel文件,php,excel,curl,email-attachments,mandrill,Php,Excel,Curl,Email Attachments,Mandrill,我正在尝试将excel文件(.xlsx)作为附件添加到通过Mandrill API发送的电子邮件中。我正在使用php文件中的CURL发送电子邮件。excel文件名为Report.xlsx 要使用CURL,请使用Mandrill API。 关于添加文件路径的另一个问题 我收到以下错误消息: PHP分析错误:语法错误,中出现意外的“路径”(T_字符串) /var/www/html/newFolder/EmailAlertSystem/mailchimp mandrill api php/Usage
我收到以下错误消息: PHP分析错误:语法错误,中出现意外的“路径”(T_字符串) /var/www/html/newFolder/EmailAlertSystem/mailchimp mandrill api php/UsageReport.php (****这是我的代码目录)
这是我通过Mandrill发送电子邮件的php代码:
$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\',
)
)
i、 转义引号应该可以修复它,但是。。。该字符串的其余部分看起来像JSON。附件部分应该是类似PHP的数组格式吗?可能需要再次检查。您可以用一个引号打开
$postString
。使用数组键的单引号告诉PHP您正在结束字符串,这不是您的意图。(注意您发布的代码中突出显示的语法)。您需要转义单引号,或者像对对象那样使用双引号。例如:“name”=>“Report.xlsx”、
或\'name\'=>\'Report.xlsx\'、
。附件必须是消息的一部分:为什么应用程序/xlsx可以双引号?