dryift6733 2017-07-14 15:21
浏览 125

Sendgrid 202 - 未发送电子邮件

On submitting a form I receive a status code of 202 (response received, email is queued for delivery), with "Form successfully sent" but the email never gets sent. I've tried re-creating my API key, and have not found an explanation for this behavior from the API. Is there a workaround to this scenario?

<?php

require("sendgrid-php/sendgrid-php.php");

// an email address that will be in the From field of the email.
$from = 'Demo contact form <test@example.com >';

// an email address that will receive the email with the output of the form
$to = 'Demo contact form <test@example.com>';

// subject of the email
$name = $_POST['name'];
$subject =  $_POST['subject'];
$message = $_POST['message'];

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'email' => 'Email', 'phone' => 'Phone', 'subject' => 'Subject', 'message' => 'Message'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{
    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $content = "You have a new message from your contact form
=============================
";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $content .= "$fields[$key]: $value
";
        }
    }

    /* All the necessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );
    */

    // Send email
    //mail($to, $subject, $content, implode("
", $headers));
    //recaptcha-response
    $recaptcha_secret = "6LfK7ygUAAAAAIYzE6mbqdxbmuroi4gJWqdIpmBu";
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
    $response = json_decode($response, true);

    if($response["success"] === true){
                            $from = new SendGrid\Email("Nami19", "namitajamwal19@gmail.com");
                        //  $subject = "Sending with SendGrid is Fun";
                        //  $subject = $_POST['subject'];
                            $subject= "You have got mail!";
                            $to = new SendGrid\Email("Example User", "etest549@gmail.com");
                            $content = new SendGrid\Content("text/html", "
                            Email : {$email}<br>
                            Name: {$name}<br>
                            Subject : {$subject}<br>
                            Message : {$message}
                            ");
                        //  $content = $_POST["content"];
                        //  $content = $_POST['content'];
                            $mail = new SendGrid\Mail($from, $subject, $to, $content);

                            $apiKey = ('SG.4J8IyicKTRqOcu7rOUmJXQ.mLk27ighheFPG4wDF63b5JboXvpGJFZMe');
                            $sg = new \SendGrid($apiKey);

                            $sg_response = $sg->client->mail()->send()->post($mail);
                            echo $sg_response->statusCode();
                            print_r($sg_response->headers());
                            echo $sg_response->body();
        echo "Form Submit Successfully.";
    }else{
        echo "You are a robot";
    }






   // $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

/*
 if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}
 */

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值