Wondering how to Configure SendGrid in Google Cloud? We can help you.
Here at Bobcares, we often handle requests from our customers to configure SendGrid on their GCP as a part of our Server Management Services.
Today we will see how our Support Engineers do this for our customers.
How to configure SendGrid in Google Cloud
Now we will see the steps that our Support Engineers follow for this setup.
Prerequisites for configuring SendGrid
Following are the prerequisites for this setup:
1. Firstly, in the Cloud Console, go to the VM instances page and click Create instance.
2. Then we can set Name to sendgrid-tutorial.
3. After that in the Boot disk section click Change.
4. Now, on the Public images tab, we can select a Debian or CentOS image version.
5. And click Select.
6. Finally, we can click Create to create the instance.
7. We can use the Google Cloud Marketplace to sign up for the SendGrid email service. The SMTP username and password are the same as what we use to sign up for the service.The SendGrid hostname is smtp.sendgrid.net
.
8. Next, we will create an API key using the following steps:
a) Sign in to SendGrid and go to Settings, then take API Keys.
b) And create an API key.
c) After that, we have to select the permissions for the key. At least, the key should have Mail send permissions to send an email.
b) Finally, click Save to create the key.
Since this is the only copy of the key, we have to ensure that we copy the key and save it for later.
Sending mail from the instance with Postfix
Complete the following steps to connect to the sendgrid-tutorial instance and run SendGrid with Postfix.
Connecting to the instance using SSH
For connecting the sendgrid-tutorial we created earlier we can use the following steps:
1. From the Cloud Console, go to the VM instances page.
2. In the list of virtual machine instances, we need to click SSH in the row of the instance that we want to connect.
Configuring SendGrid as an SMTP relay with Postfix
We can run the following commands in our SSH terminal to use SendGrid as an SMTP relay with Postfix.
- First, to become a superuser we can use the following command:
sudo su -
- Then set a safe umask using the following command:
umask 077
- Now we can Install the Postfix Mail Transport Agent:
apt update && apt -y install postfix libsasl2-modules
If prompted, we must select the Local Only configuration and accept the default domain name.
- After that, we can modify the Postfix configuration options by opening
/etc/postfix/main.cf
for editing.nano /etc/postfix/main.cf
- We must update the file as given below:
- Comment out the following lines:
# default_transport = error # relay_transport = error
- Add the following lines to the end of the file:
relayhost = [smtp.sendgrid.net]:2525 smtp_tls_security_level = encrypt smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd header_size_limit = 4096000 smtp_sasl_security_options = noanonymous
- Comment out the following lines:
- After making the changes in configuration, we can Save and close the file.
- Now, generate the SASL password map using the API key we generated in the section.
echo [smtp.sendgrid.net]:2525 apikey:your-api-key >> /etc/postfix/sasl_passwd
- Use the
postmap
utility to generate a.db
file:postmap /etc/postfix/sasl_passwd
- Verify that there is a
.db
file using the following command:ls -l /etc/postfix/sasl_passwd* -rw------- 1 root root ... /etc/postfix/sasl_passwd -rw------- 1 root root ... /etc/postfix/sasl_passwd.db
- Remove the file containing the credentials because it is no longer needed:
rm /etc/postfix/sasl_passwd
- Set the permissions on the
.db
file and verify that the other file was removed:chmod 600 /etc/postfix/sasl_passwd.db ls -la /etc/postfix/sasl_passwd.db -rw------- 1 root root ... /etc/postfix/sasl_passwd.db
- After that we can reload our configuration to load the modified parameters:
/etc/init.d/postfix restart
- Install the
mailutils
ormailx
package:apt -y install mailutils
- Send a test email:
echo 'message' | mail -s subject email@example.com
- Finally, we can check the system logs for a status line containing
status
and the successful server response code(250)
:tail -n 5 /var/log/syslog
[Need assistance? We can help you]
Conclusion
To conclude, we saw the steps that our Support Techs follow to Configure SendGrid in Google Cloud for our customers.
I have a PHP website That is hosted on Google Cloud . website that is not sending emails.
I do’t know where is issue.
Hello,
We’ll be happy to talk to you on chat (click on the icon at right-bottom).