简体   繁体   中英

send email using sendgrid api - show response [ {“errors”:[“Bad username / password”],“message”:“error”} ]

I'm trying to send my first email with 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);
?>

Here's the error I run into:

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

Where should I get the info for authorization so that I don't get the Bad username / password error?

Drop the "api_user" and "api_key" fields and add HTTP header "Authorization: Bearer " . $pass "Authorization: Bearer " . $pass (where $pass is set to your API key).

There are errors in SendGrid's Web API v2 documentation IMHO, or it's misleading at least.

$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);
}     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM