我一直在寻找有关如何使用Mailgun API和标准PHP cURL在我的电子邮件中发送内嵌图像的线索 - 而不是使用Mailgun SDK并且没有运气,所以我使用了发布问题。
到目前为止这是我的代码:
$url = 'https://api:**my-key**@api.eu.mailgun.net/v3/**my-domain.com**/messages';
$from = 'Admin < [email protected] >';
$to = 'Excite User < [email protected] >';
$subject= 'Excite Stuff In Here';
$body = '<html>
<img src="cid:logo_sml.png">
<p>Testing Mailgun API with inline images</p>
</html>';
$text = strip_tags( nl2br($body) );
$tag = 'Test';
$inline = ['inline' => realpath('../includes/images/logo_sml.png')];
// parameters of message
$params = [
'from' => $from,
'to' => $to,
'subject' => $subject,
'text' => $text,
'html' => $body,
'inline' => json_encode($inline),
'o:tag' => $tag
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params );
$data_results = curl_exec($curl);
$response = json_decode($data_results);
curl_close($curl);
电子邮件正在发送时没有任何错误消息,但收到时没有图像 - 内联或附件。我哪里错了?
我用Mailgun支持提出了一张票,Marco回复了我认为我会分享的解决方案:
$inline = curl_file_create(realpath('../includes/images/logo_sml.png'));
...
'inline' => $inline,
curl_file_create - 创建一个CURL文件对象以附加图像。其余代码没有变化。