Php 如何通过Mailgun发送Twilio短信附件?
背景:我使用自己的web应用程序通过Twilio发送和接收短信。我将图像附件插入邮件正文,但希望将其他附件类型(例如PDF)发送到我的邮件帐户 答:见下文 请注意那些认为我不应该问和回答我自己问题的下层选民: 请花点时间阅读我在写我的文章之前所做的。截至今天,它的内容是 我能回答我自己的问题吗 对!!堆栈交换一直明确鼓励用户回答问题 他们自己的问题。如果你有一个你已经知道答案的问题 回答,你想公开记录这些知识吗 包括你自己在内的其他人可以在以后找到它,这是完美的 可以在Stack Exchange站点上提问并回答您自己的问题 为了鼓励人们这样做,在页面底部有一个复选框 每次你问问题的时候都会翻页。如果你有超过15个 如果您已经知道答案,请单击下面的复选框 在提问页面底部回答您自己的问题。 输入答案,然后同时提交问题和答案Php 如何通过Mailgun发送Twilio短信附件?,php,curl,twilio,mailgun,Php,Curl,Twilio,Mailgun,背景:我使用自己的web应用程序通过Twilio发送和接收短信。我将图像附件插入邮件正文,但希望将其他附件类型(例如PDF)发送到我的邮件帐户 答:见下文 请注意那些认为我不应该问和回答我自己问题的下层选民: 请花点时间阅读我在写我的文章之前所做的。截至今天,它的内容是 我能回答我自己的问题吗 对!!堆栈交换一直明确鼓励用户回答问题 他们自己的问题。如果你有一个你已经知道答案的问题 回答,你想公开记录这些知识吗 包括你自己在内的其他人可以在以后找到它,这是完美的 可以在Stack Exchang
function emailAttachments() {
// Get global variables for Twilio and Mailgun
global $twilio_source, $twilio_sid, $twilio_token;
global $mailTo, $mailFrom, $mailgun_api_key, $mailgun_url, $mailgun_domain;
// Step through all attachments
$numMedia = $_REQUEST["NumMedia"];
if ($numMedia > 0) {
// Note the recipient's number (in case we need to notify SMS sender of a problem)
$to = $_REQUEST["To"];
// Use the sender's number and the time for forming a filename
$from = $_REQUEST["From"];
$numericFrom = preg_replace("/[^\d]/", "", $from);
$timestamp = round(microtime(true));
// Format the sender's phone number for use in the Subject and Body
$prefix = substr($from, 1, 1);
$areaCode = substr($from, 2, 3);
$exchange = substr($from, 5, 3);
$line = substr($from, 8);
$formattedFrom = "{$prefix} ({$areaCode}) {$exchange}-{$line}";
// Include the attachment count in the Subject (plural if appropriate)
$description = $numMedia . " " . ($numMedia == 1 ? "attachment" : "attachments");
$subject = "Forwarding {$description} from {$formattedFrom}.";
// Include any SMS Body text in the email's Body
$body = $_REQUEST["Body"];
$message = "<p>SMS from {$formattedFrom}</p><p style='margin-left:20px; margin-right:20px; font-family:monospace; color:navy;'>{$body}</p>";
// Specify the Mailgun parameters (not including the attachments)
$mailgunParams = array(
"from" => $mailFrom,
"to" => $mailTo,
"subject" => $subject,
"html" => $message
);
// Set a directory and start fetching from Twilio
$directory = "downloads/";
for ($i = 0; $i < $numMedia; $i++) {
// Get the content type that Twilio sent and combine it with the sender's number, the timestamp, file number, and proper extension
$contentType = $_REQUEST["MediaContentType" . $i];
$filename = $numericFrom . "." . $timestamp . "." . ($i + 1) . "." . substr($contentType, strrpos($contentType, "/") + 1);
// Open a file in the download directory
$file = fopen($directory . $filename, "w+");
// Fetch the file content with cURL
$ch = curl_init();
$options = array(
CURLOPT_HTTPGET => true,
CURLOPT_URL => $_REQUEST["MediaUrl" . $i],
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "$twilio_sid:$twilio_token",
CURLOPT_FILE => $file
);
curl_setopt_array($ch, $options);
curl_exec($ch);
// Close connection
curl_close($ch);
// Add the file information to the Mailgun array
$mailgunParams["attachment[$i]"] = curl_file_create($directory . $filename);
}
// Establish cURL connection to Mailgun
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "api:" . $mailgun_api_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_URL, "https://api.mailgun.net/v3/" . $mailgun_domain . "/messages");
curl_setopt($ch, CURLOPT_POSTFIELDS, $mailgunParams);
// Parse the result
$response = curl_exec($ch);
$response = strtolower(str_replace("\n", "", trim($response)));
$result= json_decode($response, true);
$status = explode(".", $result["message"]);
// If the message was not queued, trigger function to notify the sender and post error to log
if ($status[0] !== "queued") {
notifySender($from, $to);
error_log("Message not sent because of " . print_r($status, true));
}
// Close connection
curl_close($ch);
// Step through the attachment list one more time, and delete the files from the server
for ($i = 0; $i < $numMedia; $i++) {
$filename = $numericFrom . "." . $timestamp . "." . ($i + 1) . "." . substr($contentType, strrpos($contentType, "/") + 1);
unlink($directory . $filename);
}
}
}
有道理,对吧?在这种情况下,这篇文章提供了关于两种非常流行的服务的信息,即Twilio和Mailgun。如果一个沮丧的开发人员撞到了墙,为什么他们要问一个问题并等待答案,而不是轻易找到答案?99%的情况下,我在StackExchange上找到答案而不必发帖。在浏览媒体附件时,我会查找任何不是.jpg、.gif或.png的内容
// Look for attachments
$numMedia = $_REQUEST["NumMedia"];
if ($numMedia > 0) {
$attachments = "\n\n";
// Step through list
for ($i = 0; $i < $numMedia; $i++) {
// Get the attachment's URL on Twilio
$attachment = $_REQUEST["MediaUrl" . $i];
// If not .jpg, .gif, or .png
if (!preg_match('~(image/jpeg|image/gif|image/png)~', $_REQUEST["MediaContentType" . $i])) {
// Inform user in message body
$attachments .= "[A non-image file was sent.]\n\n";
// Note the presence of a non-image attachment
$nonImageAttachment = true;
}
// If image, add a link and image tag to the message body
else {
$attachments .= "<a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>\n\n";
}
}
// If there are any non-image attachments, trigger the emailAttachment function
if ($nonImageAttachment) {
emailAttachments();
}
}
如果Mailgun未将邮件排队,请通知发件人通过电子邮件发送附件
函数notifySender$respondTo,$respondingFrom{
//获取通过Twilio发送的参数以及回调URL
全局$twilio_sid,$twilio_令牌;
全球$website\uURL;
//获取您希望发件人用于通过电子邮件而不是SMS发送附件的电子邮件地址
全球$mailToAlternative;
//设置希望Twilio返回的任何参数,例如,监视性能的起始时间
$timestamp=roundmicrotimetrue;
$callback=$website_URL。?origined=.$timestamp;
//创建Twilio阵列
$params=数组
From=>$respondingFrom,
Body=>如果这里有问题的话,那就太好了。你所做的只是发布你拥有的东西,而没有给任何人一个发布解决方案的机会。