Wondering how to Configure Mailgun in Google Cloud? We can help you!
Google Cloud works with Mailgun to provide an email service that has a programmatic API, log retention, email personalization, analytics, and email validation.
Here at Bobcares, we often handle requests from our customers to configure Mailgun 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 Mailgun in Google Cloud
Now we will see the steps that our Support Engineers follow for this setup.
Prerequisites for configuring Mailgun:
Following are the prerequisites for this setup:
1. First we need to create a new Mailgun account.
2. Then get the credentials such as username and password from the Mailgun control panel, under the Domains section.
3. And configure the firewall rules to allow outgoing traffic on TCP port 2525.
Configuring Mailgun as a mail relay with Postfix
Configuring Mailgun as a mail relay allows the Postfix mail transfer agent to forward emails destined for remote delivery.
- First, we must instance using SSH.
gcloud compute ssh [INSTANCE_NAME]
- To become a superuser we can use the following command:
$ sudo su -
- Then set a safe umask using the following command:
# umask 077
- After that, we can install the Postfix Mail Transport Agent.
# apt update && apt -y install postfix libsasl2-modules
- When prompted, select the Local Only configuration and accept the default choices for domain names.
- To Modify the Postfix configuration options we must open the main.cf file:
# vi /etc/postfix/main.cf
- And comment out the following lines if they exist:
# default_transport = error # relay_transport = error
- Then we can add the Mailgun SMTP service by inserting the following line to the end of the file.
relayhost = [smtp.mailgun.org]:2525
Note: We must use port 2525 here since port 25 is not allowed on Compute Engine.
- To enforce SSL/TLS support and configure SMTP authentication for these requests, we must add the following lines to the end of the file:
smtp_tls_security_level = encrypt smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous
- After making the changes we can save the changes and close the file.
- For generating the SASL password map we can do the following:
- Create a new password file that is ready for standard input.
# cat > /etc/postfix/sasl_passwd << EOF
- And enter the service details, replacing YOUR_SMTP_LOGIN and YOUR_SMTP_PASSWORD with our credentials.
> [smtp.mailgun.org]:2525 YOUR_SMTP_LOGIN:YOUR_SMTP_PASSWORD
- Now, close and save the file by typing the delimiter, EOF.
> EOF
- Create a new password file that is ready for standard input.
- 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 12288 Aug 31 18:51 /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 Mailgun in Google Cloud for our customers.
0 Comments