简体   繁体   English

通过 Mailgun API 作为 MIME 发送,没有 Mailgun 库

[英]Sending via Mailgun API as MIME, without Mailgun library

I'm using PHPMailer to build an e-mail as a MIME string, and I want to send it via the Mailgun API, using curl to connect to the /v3/[mydomain]/messages.mime endpoint.我正在使用 PHPMailer 将电子邮件构建为 MIME 字符串,我想通过 Mailgun API 发送它,使用curl连接到/v3/[mydomain]/messages.mime端点。

Mailgun documentation says the MIME string has to be sent as the message parameter, and that you must use multipart/form-data to send it as a file upload. Mailgun 文档说 MIME 字符串必须作为message参数发送,并且您必须使用multipart/form-data将其作为文件上传发送。

libcurl documentation says that if you give an array to the CURLOPT_POSTFIELDS option, the Content-type will be set as multipart/form-data . libcurl 文档说,如果您为 CURLOPT_POSTFIELDS 选项提供一个数组,则 Content-type 将设置为multipart/form-data

I tried this:我试过这个:

curl_setopt($ch, CURLOPT_POSTFIELDS, ["message" => $mimeData]);

But the Mailgun API answers as follows:但 Mailgun API 回答如下:

{ "message": "'message' parameter is not a file" }

I managed to get it working by first creating a temporary file:我通过首先创建一个临时文件设法让它工作:

$mimeFile = tmpfile();
fwrite($mimeFile, $mimeData);

And passing it to a CURLFile:并将其传递给 CURLFile:

$mime = new \CURLFile(stream_get_meta_data($mimeFile)['uri']);

After this, I got a new error message, 'to' parameter is missing , so I finally had to get the recipient addresses from PHPMailer (they are already in the MIME string, but the API doesn't seem to realize it):在此之后,我收到一条新的错误消息, 'to' parameter is missing ,所以我最终不得不从 PHPMailer 获取收件人地址(它们已经在 MIME 字符串中,但 API 似乎没有意识到这一点):

curl_setopt($ch, CURLOPT_POSTFIELDS, [
    "to" => implode(", ", array_keys($this->getAllRecipientAddresses())),
    "message" => $mime
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM