0

I am trying to implement the sendgrid email API.

I have followed all the instructions I can find to try and set up the sendgrid email API but I all I get is this:

Response status: 401 Response Headers - HTTP/1.1 401 Unauthorized
- Server: nginx - Date: Wed, 02 Mar 2022 21:56:45 GMT
- Content-Type: application/json
- Content-Length: 88
- Connection: keep-alive
- Access-Control-Allow-Origin: https://sendgrid.api-docs.io
- Access-Control-Allow-Methods: POST
- Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
- Access-Control-Max-Age: 600
- X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
- Strict-Transport-Security: max-age=600; includeSubDomains

I installed the SendGrid helper library, ran the Composer command which created a composer.json file in the root of my project, installed the SendGrid helper library for PHP, along with its dependencies in a new directory named vendor.

I then used the following to create sendgrid.env:

echo "export SENDGRID_API_KEY='SG.XXX....XXXX'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

The API key works fine when I tested it using curl --request POST and email comes through ok.

But I have tried every combination I can think of to integrate the sendgrid.env e.g. changing the location, removing the single quotes etc. but I just get the same error message every time.

Here is my php script to send the email:

declare(strict_types=1);

require 'vendor/autoload.php';

use \SendGrid\Mail\Mail;

$email = new Mail();
// Replace the email address and name with your verified sender
$email->setFrom(
    'paul@xxx.com',
    'Paul xxx'
);
$email->setSubject('Sending with Twilio SendGrid is Fun');
// Replace the email address and name with your recipient
$email->addTo(
    'paul@yyy.com',
    'Paul yyy'
);
$email->addContent(
    'text/html',
    '<strong>and fast with the PHP helper library.</strong>'
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    printf("Response status: %d\n\n", $response->statusCode());

    $headers = array_filter($response->headers());
    echo "Response Headers\n\n";
    foreach ($headers as $header) {
        echo '- ' . $header . "\n";
    }
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}
     

This is the structure of the files:

file structure

My domain and email have been verified on Sendgrid.

I have a feeling that there's another step involved to get the API from the sendgrid.env file?

Thanks in advance.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • When you run `source ./sendgrid.env` that should export the variable to your shell. So, in the same shell you should be able to run `echo $SENDGRID_API_KEY` in the shell and see the key. If you run your script from that same shell, then it should be available within your code using `getenv`. What happens if you try to run `echo getenv("SENDGRID_API_KEY")` in your code? – philnash Mar 02 '22 at 23:22
  • Thanks philnash If I run echo $SENDGRID_API_KEY in terminal then I get the API key but that is on my local pc. I want to get this working on my third party web server, so I upload the files via ftp to the site but then I'm not sure what I need to do next. Thanks. – Paul Anderson Mar 03 '22 at 09:32
  • You need to set that environment variable on your web server. I’d do a search for that based on what operating system you’re running on your server. – philnash Mar 03 '22 at 09:37
  • If I echo getenv("SENDGRID_API_KEY"); then the screen is blank. If I add the API key to a variable and use $sendgrid = new \SendGrid($apikey); then I get a 202 response and the email comes through ok. This reinforces my thought that the environment variable isn't available on the live web server. Sorry! just seen your latest reply. – Paul Anderson Mar 03 '22 at 10:41
  • What is your server? What operating system? – philnash Mar 03 '22 at 10:42
  • It's a Linux with cpanel access. – Paul Anderson Mar 03 '22 at 12:50
  • Is your application run with Apache? This [answer should help you set the environment variable within your Apache config](https://stackoverflow.com/a/20270501/28376). Or if you have access to the server, you can read this article on [setting environment variables on Linux](https://phoenixnap.com/kb/linux-set-environment-variable), with particular attention on setting them permanently. – philnash Mar 03 '22 at 22:28

0 Answers0