【发布时间】: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 以及随附的代码,这会导致页面根本无法加载。我现在真的很难过,非常感谢任何帮助。
【问题讨论】:
-
当我按原样运行您的代码(包括虚拟用户/通行证)时,我会收到响应。
-
我使用 phpinfo() 检查过,它说 curl 支持已启用。
-
您是否尝试过从 cURL 捕获错误? php.net/manual/en/function.curl-error.php