Как я могу отправить SMS-сообщения Twilio через Mailgun? - PullRequest
0 голосов
/ 30 марта 2020

Справочная информация: Я использую свое собственное веб-приложение для отправки и получения SMS-сообщений через Twilio. Я вставляю вложения изображений в тело сообщения, но хочу, чтобы другие типы вложений (например, PDF-файлы) отправлялись на мой почтовый аккаунт.

Ответ: (см. Ниже.)


Примечание для Downvoters (которые считают, что я не должен задавать и отвечать на свой вопрос:

Пожалуйста, найдите время, чтобы прочитать официальную политику StackOverflow (которую я делал до написания своего поста). Начиная с этой даты, она гласит ...

Могу ли я ответить на свой вопрос?

Да! Stack Exchange всегда явно побуждал пользователей отвечать на свои вопросы. Если у вас есть вопрос, ответ на который вы уже знаете , и вы хотели бы задокументировать эти знания в publi c, чтобы другие (включая вас) могли найти его позже, вполне нормально задавать и отвечать на свои вопросы на сайте Stack Exchange.

Для поощрения Чтобы сделать это, каждый раз, когда вы задаете вопрос, в нижней части страницы есть флажок. Если у вас более 15 репутаций и вы уже знаете ответ, установите флажок «Ответить на свой вопрос» внизу страницы. Задать вопрос на странице. Введите свой ответ, затем отправьте оба вопроса и ответ вместе.

Имеет смысл, верно? В этом случае пост предоставляет информацию о двух очень популярных сервисах (т. Е. Twilio и Mailgun). Если разочарованный разработчик ударил стену, почему они должны задавать вопрос и ждать ответа, а не легко найти его? 99% времени я нахожу ответы на StackExchange, не задавая вопрос.

1 Ответ

0 голосов
/ 30 марта 2020

Проходя по полкам с носителями, я ищу те, которые не являются .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();
        }
    }

Если найдены какие-либо вложения, не связанные с изображениями, функция emailAttachments срабатывает. Функция отправляет все вложения (даже изображения) по электронной почте, так что все вложения из одного SMS-сообщения хранятся вместе.

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);
        }           
    }
}   

Если Mailgun не ставит сообщение в очередь, уведомите отправителя отправить по электронной почте вложения.

function notifySender($respondTo, $respondingFrom) {
    // Get parameters for sending through Twilio, as well as a callback URL
    global $twilio_sid, $twilio_token;
    global $website_URL;

    // Get the email address that you want the Sender to use for sending attachments via email instead of SMS
    global $mailToAlternative;

    // Set any parameters you want Twilio to return (e.g., origination time to monitor performance)
    $timestamp = round(microtime(true));
    $callback = $website_URL . "?originated=" . $timestamp;

    // Create Twilio array
    $params = array (
        "From" => $respondingFrom,
        "Body" => "?  SERVER AUTO-REPLY:\n\nPlease re-send non-image file(s) to…\n\n{$mailToAlternative}",
        "StatusCallback" => $callback
    );

    // Send to Twilio
    $client = new Client($twilio_sid, $twilio_token);
    $message = $client->messages->create($respondTo, $params);

    // Optionally do something with the result (e.g., write to log file)
    $sid = $message->sid;
    $status = $message->status;
}
...