Sendgrid API - invalid username / password error

I am trying to send my first email using Sendgrid:

$sendgrid = new SendGrid('username', 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx'); $email = new SendGrid\Email(); $email->addTo(" email1@gmail.com ") ->setFrom(" email@domain.com ") ->setSubject("Sending with SendGrid is Fun") ->setHtml("and easy to do anywhere, even with PHP"); 

Here is the error I encountered:

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


Instead of "username" and "xx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" I, of course, use the real API key information from the Sendgrid settings: enter image description here

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

Where can I get authorization information so that I don't get a Bad username / password error?

+5
source share
2 answers

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

 $sendgrid = new SendGrid('xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx'); $email = new SendGrid\Email(); $email->addTo(" email1@gmail.com ") ->setFrom(" email@domain.com ") ->setSubject("Sending with SendGrid is Fun") ->setHtml("and easy to do anywhere, even with PHP"); 
+8
source

Instead of using the username, which is the name of the API key, try using the SendGrid username (the username that you use to access the control panel, etc.).

EDIT: for it to work, also use the Sendgrid password instead of the API key. It worked for me.

EDIT 2: while this solves the problem, you only need to use the api key, as indicated in the selected answer.

+3
source

Source: https://habr.com/ru/post/1239880/


All Articles