This is an answer to the question when using Mailgun's class. I'm seeking to find an answer that applies to using CURL inside of PHP.
这是使用Mailgun课程时的问题的答案。我正在寻找一个适用于在PHP中使用CURL的答案。
Using PHPMailer's class, I can send multiple attachments in the following manner:
使用PHPMailer的类,我可以通过以下方式发送多个附件:
$mail->AddStringAttachment($attachment1, $title1);
$mail->AddStringAttachment($attachment2, $title2);
Because I am not fetching a file from the server, and am instead composing in a string, I need to specify the title for each enclosure.
因为我没有从服务器获取文件,而是在字符串中编写,所以我需要为每个机箱指定标题。
Now, I'd like to accomplish that using Mailgun via PHP and CURL. So far, I am utilizing the following technique for sending mail without attachments:
现在,我想通过PHP和CURL使用Mailgun实现这一目标。到目前为止,我正在使用以下技术发送没有附件的邮件:
$api_key="[my api key]";
$domain ="[my domain]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"from" => "[sender]",
"to" => $to,
"subject" => $subject,
"html" => $content
));
Following this same convention of specifying the fields in an array, what's the equivalent of sending string attachments and specifying the titles using PHP and CURL with Mailgun?
遵循这个在数组中指定字段的相同约定,相当于发送字符串附件并使用PHP和CURL与Mailgun指定标题?
1 个解决方案
#1
1
I gave up on using string attachments and instead created two temporary files (based on content previously composed by another function) inside of a temporary directory (with the directory name based on the user's unique ID). (Thanks to drew010 for starting me down the correct path.)
我放弃了使用字符串附件,而是在临时目录(目录名称基于用户的唯一ID)内创建了两个临时文件(基于以前由另一个函数组成的内容)。 (感谢drew010让我开始正确的道路。)
I doubt the following function will be useful to others as is, but perhaps various portions will be helpful to others desiring similar functionality.
我怀疑以下功能对其他功能是否有用,但也许各个部分对希望具有类似功能的其他功能有所帮助。
function sendFormattedEmail ($coverNote, $attachment1, $title1, $attachment2, $title2) {
global $userID, $account;
if (!file_exists("temp_{$userID}")) {
mkdir("temp_{$userID}");
}
file_put_contents("temp_{$userID}/{$title1}", $attachment1);
file_put_contents("temp_{$userID}/{$title2}", $attachment2);
$api_key="[api_key]";
$domain ="[my_domain]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"from" => "[my_return_address]",
"to" => $account,
"subject" => "your requested files",
"text" => $coverNote,
"attachment[1]" => new CurlFile("temp_{$userID}/{$title1}"),
"attachment[2]" => new CurlFile("temp_{$userID}/{$title2})"
));
$response = curl_exec($ch);
$response = strtolower(str_replace("\n", "", trim($response)));
$result= json_decode($response, true);
$status = explode(".", $result["message"]);
if ($status[0] == "queued") {
echo json_encode(array ("result" => "success"));
}
else {
echo json_encode(array ("result" => "failure"));
}
curl_close($ch);
unlink ("temp_{$userID}/{$title1}");
unlink ("temp_{$userID}/{$title2}");
rmdir ("temp_{$userID}");
}
As shown above, the function strips the newline characters from Mailgun's response in order to enable the use of json_encode. The trim and lowercase conversion are just my preference.
如上所示,该函数从Mailgun的响应中删除换行符,以便能够使用json_encode。修剪和小写转换只是我的偏好。
After reporting the result back to the calling function, it removes the two temporary files and then the temporary directory.
在将结果报告回调用函数后,它会删除两个临时文件,然后删除临时目录。
#1
1
I gave up on using string attachments and instead created two temporary files (based on content previously composed by another function) inside of a temporary directory (with the directory name based on the user's unique ID). (Thanks to drew010 for starting me down the correct path.)
我放弃了使用字符串附件,而是在临时目录(目录名称基于用户的唯一ID)内创建了两个临时文件(基于以前由另一个函数组成的内容)。 (感谢drew010让我开始正确的道路。)
I doubt the following function will be useful to others as is, but perhaps various portions will be helpful to others desiring similar functionality.
我怀疑以下功能对其他功能是否有用,但也许各个部分对希望具有类似功能的其他功能有所帮助。
function sendFormattedEmail ($coverNote, $attachment1, $title1, $attachment2, $title2) {
global $userID, $account;
if (!file_exists("temp_{$userID}")) {
mkdir("temp_{$userID}");
}
file_put_contents("temp_{$userID}/{$title1}", $attachment1);
file_put_contents("temp_{$userID}/{$title2}", $attachment2);
$api_key="[api_key]";
$domain ="[my_domain]";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:'.$api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/'.$domain.'/messages');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"from" => "[my_return_address]",
"to" => $account,
"subject" => "your requested files",
"text" => $coverNote,
"attachment[1]" => new CurlFile("temp_{$userID}/{$title1}"),
"attachment[2]" => new CurlFile("temp_{$userID}/{$title2})"
));
$response = curl_exec($ch);
$response = strtolower(str_replace("\n", "", trim($response)));
$result= json_decode($response, true);
$status = explode(".", $result["message"]);
if ($status[0] == "queued") {
echo json_encode(array ("result" => "success"));
}
else {
echo json_encode(array ("result" => "failure"));
}
curl_close($ch);
unlink ("temp_{$userID}/{$title1}");
unlink ("temp_{$userID}/{$title2}");
rmdir ("temp_{$userID}");
}
As shown above, the function strips the newline characters from Mailgun's response in order to enable the use of json_encode. The trim and lowercase conversion are just my preference.
如上所示,该函数从Mailgun的响应中删除换行符,以便能够使用json_encode。修剪和小写转换只是我的偏好。
After reporting the result back to the calling function, it removes the two temporary files and then the temporary directory.
在将结果报告回调用函数后,它会删除两个临时文件,然后删除临时目录。