By linxsolaire
I have started using Sendgrid on my website to allow emails to sent to my users when they sign up for my site.
I copied the api call for Sendgrid into my site code and tried to send an email. I received a permission denied error. I contacted the Sendgrid support before making this post and have been working with them to try and fix this issue but we are stuck because it does not seem like PHP is picking up the environment variable that I have set to store my Sendgrid api key.
When I hard code in the key, I am able to send emails through Sendgrid with one issues, it is only when I attempt to have the api key called through an environment variable. Like this: ```$apiKey = getenv(‘SENDGRID_API_KEY’);``
I am certain it is not being set because when I try to echo $apiKey in php, I do not get anything back.
First, I tried sourcing the environment variable though an env file like it states on the Sendgrid website.
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env```
When that didn't work, I followed [this](https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps#setting-shell-and-environmental-variables) article on how to set environment variables on my droplet, but still does not seem to work. I still get a permission denied error from Sendgrid's api when making a call with the environment variable.
When I do printenv from my droplet's terminal, I can see the variable so I imagine it is set on my server but how to do I make it be seen by PHP?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I know this is old - but if anyone else stumbles across this, do also check out this comment on the PHP wiki.
There’s a PHP configuration setting (variables_order) that needs to include the character ‘E’ to access environment variables.
Looks like this may be a security feature (stopping php from reading environment variables).
There may be a setting you have to turn on (or off) in php.ini but what I’ve found if you put the enviroment variable in /etc/enviroment php will read it with getenv().
This link may shed some light on it: https://stackoverflow.com/questions/13568191/how-to-get-system-environment-variables-into-php-while-running-cli-apache2hand
You need to ensure the environment variable is accessible to the web server and, by extension, PHP.
sudo nano /etc/apache2/sites-available/000-default.conf
Add the following line to pass the environment variable to PHP:
SetEnv SENDGRID_API_KEY "your_sendgrid_api_key_here"
Restart Apache to apply the changes.
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
env[SENDGRID_API_KEY] = "your_sendgrid_api_key_here"
sudo systemctl restart php7.4-fpm sudo systemctl restart nginx
If PHP is running under a different user (like www-data), it won’t inherit your shell’s environment variables. Ensure that you add the environment variable to the web server’s configuration rather than relying on shell variables.
Once the above changes are made, test if PHP can now access the environment variable.
Create a simple PHP file to test:
<?php
$apiKey = getenv('SENDGRID_API_KEY');
echo $apiKey ? "API Key: $apiKey" : "No API Key found.";
?>
Access this file via your browser to verify if the API key is being printed.
.env Files with PHP (Optional)Alternatively, you can use a .env file and load it into PHP with a library like vlucas/phpdotenv to manage environment variables in your application.
composer require vlucas/phpdotenv
.env file in your project root:SENDGRID_API_KEY=your_sendgrid_api_key_here
.env file:require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$apiKey = getenv('SENDGRID_API_KEY');
echo $apiKey;
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.