Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendgrid API - Bad username / password error

Tags:

php

sendgrid

I'm trying to send my first email with Sendgrid:

$sendgrid = new SendGrid('username', 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');
$email = new SendGrid\Email();

$email->addTo("[email protected]")
      ->setFrom("[email protected]")
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");

Here's the error I run into:

PHP Fatal error: Uncaught exception 'SendGrid\Exception' with message '{"errors":["Bad username / password"],"message":"error"}'


Instead of 'username' and 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx' I of course use real API key info from Sendgrid settings: enter image description here

I used the long string key provided after creating the API key. But there still seems to be something wrong with this line: $sendgrid = new SendGrid('username', 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');

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

like image 366
Sergey Avatar asked Jan 05 '16 11:01

Sergey


People also ask

What is SendGrid username?

SendGrid supports Basic Authentication using an API key as your password value for some services. When using Basic Authentication, your username will always be "apikey," and your password will be your API key.

What is SendGrid API key?

Your application, mail client, or website can all use API (Application Programming Interface) keys to authenticate access to SendGrid services. They are the preferred alternative to using a username and password because you can revoke an API key at any time without having to change your username and password.


1 Answers

Another possible answer is to pass just the api key to the SendGrid() constructor;

$sendgrid = new SendGrid('xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');
$email = new SendGrid\Email();

$email->addTo("[email protected]")
      ->setFrom("[email protected]")
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");
like image 76
Justin Steele Avatar answered Sep 20 '22 07:09

Justin Steele