Unable to access SENGRID_API_KEY environment variable.
Issue Summary
I have been trying to send my first email. I followed all steps in the installation guide. The only piece of code that is breaking the script is getenv('SENGRID_API_KEY'). That ends up returning nothing. It's definitely set in my root directory (I verified using export -p and SENDGRID_API_KEY is set to my sendgrid api key. I also verified that is the only error because if I manually set my api key to $apiKey and just run the code that way, then mail does send.
The readme.md documentation doesn't detail how to get the environment variable from the shell to the php environment.
Appreciate your help!
Technical details:
- sendgrid-php Version: master (latest commit: [commit number])
- PHP Version: 5.6
Hello @govindrai,
After you do this:
echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
What happens when you do echo $SENDGRID_API_KEY?
With Best Regards,
Elmer
@thinkingserious
Here's what I get in my error log:
[11-Nov-2016 13:30:29 UTC] PHP Notice: Undefined variable: SENDGRID_API_KEY in /home/raigovind93/public_html/testforelmer.php on line 3
Basically, those three lines of code make the environment variable SENDGRID_API_KEY available in my shell (root) environment, but how to get the key to become available in the PHP environment?
I feel that is the piece of code missing from the installation docs for sendgrid-php.
Hello @govindrai,
Here is the function you need: http://php.net/manual/en/function.getenv.php
As described here: https://github.com/sendgrid/sendgrid-php#hello-email
What happens when you do echo $SENDGRID_API_KEY from the command line?
@thinkingserious Hi Elmer,
Here's what I get back:
[12-Nov-2016 16:30:30 UTC] PHP Notice: Undefined variable: SENDGRID_API_KEY in /home/raigovind93/public_html/testforelmer.php on line 3
Basically, the variable is available in the root environment, but the variable is not available to the webserver.
Hello @govindrai,
You will need to discover the best practice for storing environment variables in your web server. Is this a locally hosted server, or are you testing on a web host? If it's the latter, you may want to reach out the web hosts admins for assistance.
Thank You Sir for helping. i am windows user, you said in your given git to update the development environment: https://github.com/sendgrid/sendgrid-php
would you please guide me in updating the development environment? please, your help would be appreciated. waiting for reply.
@smkazim,
Please check this out: https://github.com/sendgrid/sendgrid-csharp#setup-environment-variables
Thanks!