使用Web Api发送带有SendGrid的电子邮件php
问题描述:
我很困惑这里的$ pass和$ api_key 它们是否相同,因为最初$ pass被赋予了SendGrid用户名的密码,但是后来api_key被赋予了$ pass。 如果它们相同,我们将使用我们在SendGrid上生成的api_key? 请帮忙!!!使用Web Api发送带有SendGrid的电子邮件php
<?php
$url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD';
$params = array(
'api_user' => $user,
'api_key' => $pass,
'to' => '[email protected]',
'subject' => 'testing from curl',
'html' => 'testing body',
'text' => 'testing body',
'from' => '[email protected]',
);
$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);
?>
答
有一些问题与您正在试图做什么:
- 你不应该发送API调用
sendgrid.com
,只有api.sendgrid.com
- 它看起来像你正在尝试使用带参数的v2 API。由于您正在设置新脚本,因此您应该使用v3 API,它使用适当的主体JSON参数,并且是RESTful。
- 而不是设置自己的脚本,你看过SendGrid PHP Library?
- 要在脚本中进行身份验证,您应该始终使用API Key,而不是您的主用户名&密码。
它看起来像你有一个非常旧的示例脚本进行测试。你从哪里得到那个的?
我不懂PHP,也没有脚本。你检查过SendGrid PHP库吗?这应该给你一个强大的开头。 – jacobmovingfwd