1

我正在尝试使用 Sendgrid 发送我的第一封电子邮件:

<?php 
$url = 'https://api.sendgrid.com/'; 
$user = 'bio'; //bio
$pass = 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');';

// grabs HTML form's post data; if you customize the form.html parameters then you will need to reference their new new names here
$name = "a";
$email = "gd@gmail.com";
$subject = "fdfdfd gdfg";
$message = "dfgd fhg fghfd ";
// note the above parameters now referenced in the 'subject', 'html', and 'text' sections
// make the to email be your own address or where ever you would like the contact form info sent
$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => "developer@gmail.com", // set TO address to have the contact form's email content sent to
    'subject'   => "Contact Form Submission", // Either give a subject for each submission, or set to $subject
    'html'      => "<html><head><title> Contact Form</title><body>",
    'from'      => "contact@gmail.com", // set from address here, it can really be anything
  );
//curl_setopt($url, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$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);
?>

这是我遇到的错误:

{"errors":["Bad username / password"],"message":"error"}

我应该从哪里获取授权信息,以免出现错误的用户名/密码错误?

4

2 回答 2

0
$request =  $url.'api/mail.send.json';
$headr = array();
// set authorization header
$headr[] = 'Authorization: Bearer '.$key; //key not a password

// Generate curl request
$session = curl_init($request);
if($session)
{
      curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
      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);

     // add authorization header
      curl_setopt($session, CURLOPT_HTTPHEADER,$headr);
    
     // obtain response
       $response = curl_exec($session);
       curl_close($session);
}     
于 2021-02-10T10:21:25.850 回答
0

删除“api_user”和“api_key”字段并添加 HTTP 标头"Authorization: Bearer " . $pass(其中 $pass 设置为您的 API 密钥)。

恕我直言, SendGrid 的 Web API v2 文档中存在错误,或者至少具有误导性。

于 2020-04-03T02:15:23.183 回答