【问题标题】:How to send email using SendGrid如何使用 SendGrid 发送电子邮件
【发布时间】:2016-10-18 21:13:19
【问题描述】:

我一直在尝试使用 SendGrid 的 PHP API 为网页设置电子邮件,但我没有运气。我已经从 Azure 网站尝试了他们的代码以及我在尝试谷歌时发现的各种示例,每次它都没有返回任何响应,并且没有设置电子邮件。这是我正在谈论的代码:

<?php
 $url = 'https://api.sendgrid.com/';
 $user = 'USERNAME';
 $pass = 'PASSWORD'; 

 $params = array(
      'api_user' => $user,
      'api_key' => $pass,
      'to' => 'email@gmail.com',
      'subject' => 'testing from curl',
      'html' => 'testing body',
      'text' => 'testing body',
      'from' => 'anna@contoso.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);

 // print everything out
 print_r($response);
 ?>

还值得注意的是,我尝试使用 sendgrid-php 以及随附的代码,这会导致页面根本无法加载。我现在真的很难过,非常感谢任何帮助。

【问题讨论】:

标签: php azure sendgrid


【解决方案1】:

默认情况下,Azure 不会为 Azure Web 应用生成 SSL 对等证书。如果在测试代码$error = curl_error($session);中添加代码,会得到错误SSL certificate problem: self signed certificate in certificate chain

您只需添加代码curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0); 即可绕过验证。

否则,您可以关注 Verify Peer Certificate from PHP cURL for Azure Apps 在 Azure Web Apps 上添加证书。

【讨论】:

  • 感谢您的回答。我使用您提供的代码绕过验证,现在我收到了回复。它现在正在给出响应,它说它是成功的,但电子邮件没有到达。知道为什么会这样吗?
  • 没关系。我在使用的电子邮件中有错字。它正在工作。再次感谢您。
【解决方案2】:

您的 cURL 版本可能不支持开箱即用的 SSL。

您可以通过将以下内容放在代码顶部来测试这一点:

if (!curl_version()['features'] & CURL_VERSION_SSL) {
    echo "SSL is not supported with this cURL installation.";
}

如果不支持开箱即用,您必须像这样手动配置它:

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 2);
// path to CA certificate
curl_setopt($session, CURLOPT_CAINFO, '/etc/openssl/cert.pem'); 

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多