【问题标题】:Why is my form data not being sent via SendGrid为什么我的表单数据没有通过 SendGrid 发送
【发布时间】:2021-03-02 16:45:05
【问题描述】:

发送付款页面以通过 sendgrid 发送带有表单数据的电子邮件。之前的开发者在不久前就做了这个,直到一个月左右之前都可以正常工作。

我认为问题出在身份验证上,因为他们使用电子邮件 + 密码作为 $user 和 `$pass.更新了密码以使用 sendgrid 生成的 API 密钥。付款提交很好,所以我知道问题一定出在下面的代码中。

    $url = 'https://api.sendgrid.com/';
    $user = 'apikey';
    $pass = 'apikeyhere';


    $categories = 'Bill Payment';

    $json_string = array(
    'to' => array(
        'test1@email.com', 'test@email.com'
        ),
      'category' => $categories,
      'filters' => array('templates' => array('settings' => array('enable' => 1, 'template_id' => '7c4ecc93-8575-a46a3d-9864-97sabedf54119')))
    );

    $full_name = $_POST['billing-address-first-name'].' '.$_POST['billing-address-last-name'];
    $address1 = $_POST['billing-address-address1'];
    $city = $_POST['billing-address-city'];
    $state = $_POST['billing-address-state'];
    $postal = $_POST['billing-address-zip'];
    $phone = $_POST['billing-address-phone'];
    $amount = $_POST['billing-address-bill-total'];
    $email = $_POST['billing-address-email'];
    $discountField = $_POST['discount-code'];
    if($discountField != ''){
        $discCode = $discountField;
    }else{
        $discCode = 'N/A';
    }

    $body = "Thank you for paying online.<br/>This email can serve as your receipt.<br/><br/>";
    $body = $body. "Name: $full_name<br/>";
    $body = $body. "Address: <a href='#' style='text-decoration:none;color: #005044;'>$address1 $city, $state $postal</a><br/>";
    $body = $body. "Phone: $phone<br/>";
    $body = $body. "Amount: $amount<br/><br/>";
    $body = $body. "Discount Code Applied: $discCode<br/><br/>";
    $body = $body. "As always, if there are any questions or concerns please email us at <a href='mailto:email@email.com</a>.";

    // Send email to person submitting hte payment.
    $params = array(
                'api_user'  => $user,
                'api_key'   => $pass,
                'x-smtpapi' => json_encode($json_string),
                'to'        => $email,
                'subject'   => "Online Payment Receipt",
                'html'      => $body,
                'text'      => $body,
                'from'      => 'info@email.com'
     );

    $request =  $url.'api/mail.send.json';

    // Generate curl request
    $session = curl_init($request);

    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true);

    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    // obtain response
    $response = curl_exec($session);
    curl_close($session);

    // Send email to Nolan Painting as well.
    $params = array(
                'api_user'  => $user,
                'api_key'   => $pass,
                'x-smtpapi' => json_encode($json_string),
                'to'        => 'steve@email.com',
                'subject'   => "Nolan Painting - Online Payment Receipt",
                'html'      => $body,
                'text'      => $body,
                'from'      => 'info@email.com'
     );

    $request =  $url.'api/mail.send.json';

    // Generate curl request
    $session = curl_init($request);

    // Tell curl to use HTTP POST
    curl_setopt ($session, CURLOPT_POST, true);

    // Tell curl that this is the body of the POST
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);

    // Tell curl not to return headers, but do return the response
    curl_setopt($session, CURLOPT_HEADER, false);
    // Tell PHP not to use SSLv3 (instead opting for TLS)
    curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

    // obtain response
    $response = curl_exec($session);
    curl_close($session);

【问题讨论】:

    标签: php email sendgrid


    【解决方案1】:

    您怀疑身份验证是一个问题是正确的。从 2020 年第四季度开始,SendGrid 不再使用用户名/密码身份验证 (source)。

    您需要传递一个授权标头并在您的请求中省略api_userapi_key 参数。将 API 密钥作为不记名令牌传递:"Authorization: Bearer &lt;Your API Key&gt;"。 (docs)

    类似:

    $authorization = 'Authorization: Bearer <Your API Key>';
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
    

    请务必将新的 curl_setopt 行添加到您声明 curl 请求的两个位置。

    编辑:

    在进一步浏览此doc 后,您的初始方法似乎确实受到支持(或在某些时候得到支持)。如果上述方法没有帮助,请尝试按照here 列出的步骤进行故障排除。检查错误以及您对 API 密钥的设置和权限是否正确。

    我还发现了您的开发人员可能使用的this documentation。这里还有更多关于如何使用不记名令牌方法的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多