使用 Web API PHP 通过 SendGrid 发送电子邮件

问题描述 投票:0回答:1

我对这里的

$pass
$api_key
感到困惑; 他们一样吗?因为一开始
$pass
被分配了 SendGrid 用户名的密码,但随后
api_key
被分配了
$pass
。 如果它们相同,我们将在哪里使用我们在 SendGrid 上生成的
api_key
? 请帮忙!

<?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);

?>
php sendgrid
1个回答
0
投票

您尝试执行的操作存在一些问题:

  • 您永远不应该向
    sendgrid.com
    发送 API 调用,而只能向
    api.sendgrid.com
  • 发送 API 调用
  • 您似乎正在尝试将 v2 API 与参数一起使用。由于您正在设置新脚本,因此您应该使用 v3 API,它使用正确的正文 JSON 参数,并且是 RESTful。
  • 您是否没有查看过 SendGrid PHP 库,而不是设置自己的脚本?
  • 对于脚本中的身份验证,您应始终使用 API 密钥,而不是您的主用户名和密码。

看起来您有一个非常旧的示例脚本用于测试。你从哪里得到的?

© www.soinside.com 2019 - 2024. All rights reserved.