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
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
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:
- 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
- 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
postmaputility to generate a
- Verify that there is a
.dbfile 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:
- Set the permissions on the
.dbfile 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:
- Install the
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
statusand the successful server response code
tail -n 5 /var/log/syslog
[Need assistance? We can help you]
To conclude, we saw the steps that our Support Techs follow to Configure SendGrid in Google Cloud for our customers.