Mailgun is a third-party email service that offers Compute Engine users up to 10,000 free emails each month. MailGun also offers a programmatic API, log retention, email personalization, analytics, email validation, and more.
Follow these instructions to sign up for and use Mailgun.
Before you begin
Create a new Mailgun account on Mailgun's Google partner page*. As a Compute Engine user, your first 30,000 messages are free every month. Check out the Mailgun monthly pricing calculator for pricing on additional messages and volume discounts.
Get your credentials. The instructions will require that you know your Mailgun SMTP username, password, and hostname. Get your username and password from the Mailgun control panel, under the Domains section.
The Mailgun SMTP hostname is smtp.mailgun.org.
Configure your firewall rules to allow outgoing traffic on TCP port 2525.
* Google will be compensated for customers who sign up for a non-free account.
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.
Connect to your instance using SSH.
gcloud compute ssh [INSTANCE_NAME]where
[INSTANCE_NAME]is the name of the VM instance where you want to send emails from.Become a superuser and set a safe umask.
user@test-instance:~$ sudo su -root@test-instance:~# umask 077Install the Postfix Mail Transport Agent. When prompted, accept the default choices for domain names but select the
Local Onlyconfiguration.Debian
root@test-instance:~# apt-get update && apt-get install postfix libsasl2-modules -yCentOS
root@test-instance:~# yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -yModify the Postfix configuration options. Postfix configuration options are set in the
main.cffile. Open the file with the text editor of your choice.root@test-instance:~# vi /etc/postfix/main.cfIf they exist, comment out the following lines.
# default_transport = error # relay_transport = errorAdd the Mailgun SMTP service by adding the following line to the end of the file.
relayhost = [smtp.mailgun.org]:2525Next, add the following lines to enforce SSL/TLS support and to configure STMP authentication for these requests. A simple access and security layer (SASL) module handles authentication in the Postfix configuration. 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 = noanonymousSave your changes and close the file.
Generate the SASL password map.
Create a new password file that is ready for standard input.
root@test-instance:~# cat > /etc/postfix/sasl_passwd << EOFAt the prompt, enter the service details, replacing
YOUR_SMTP_LOGINandYOUR_SMTP_PASSWORDwith your credentials.> [smtp.mailgun.org]:2525 YOUR_SMTP_LOGIN:YOUR_SMTP_PASSWORDClose and save the file by typing the delimiter,
EOF.> EOF
Use the
postmaputility to generate a.dbfile.root@test-instance:~# postmap /etc/postfix/sasl_passwdroot@test-instance:~# ls -l /etc/postfix/sasl_passwd* -rw------- 1 root root 68 Jun 1 10:50 /etc/postfix/sasl_passwd -rw------- 1 root root 12288 Jun 1 10:51 /etc/postfix/sasl_passwd.db
Next, remove the file containing your credentials as it is no longer needed.
root@test-instance:~# rm /etc/postfix/sasl_passwdSet the permissions on your
.dbfile.root@test-instance:~# chmod 600 /etc/postfix/sasl_passwd.dbroot@test-instance:~# ls -la /etc/postfix/sasl_passwd.db -rw------- 1 root root 12288 Aug 31 18:51 /etc/postfix/sasl_passwd.db
Finally, reload your configuration to load the modified parameters.
Debian
root@test-wheezy:~# /etc/init.d/postfix restartCentOS
[root@test-centos ~]# postfix reloadTest your configuration. Install the
mailxormailutilspackage and test your configuration.Debian
root@test-wheezy:~# apt-get install mailutils -yCentOS
[root@test-centos ~]# yum install mailx -ySend a test message.
root@test-instance:~# echo 'Test passed.' | mail -s 'Test-Email' EMAIL@EXAMPLE.COMLook in your systems logs for a status line containing
statusand the successful server response code(250).Debian
root@test-wheezy:~# tail -n 5 /var/log/syslogCentOS
[root@test-centos ~]# tail -n 5 /var/log/maillog
For more detailed examples and information about many other topics including tracking and routing messages, read the Mailgun documentation.
Mailgun SMTP settings
Here is a quick reference to Mailgun-specific SMTP settings that are used to configure clients:
- Host: smtp.mailgun.org
- Port: 2525
Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.


