Sending email with Mailgun Stay organized with collections Save and categorize content based on your preferences.
Google Cloud works with Mailgun to provide an email service that has aprogrammatic API, log retention, email personalization, analytics, and emailvalidation.
The following instructions show you how to configure Mailgun as an email relaywith Postfix.
Before you begin
Sign up for and create a new Mailgun account on theGoogle Cloud Marketplace.
Get your credentials. The instructions require that you know your MailgunSMTP username, password, and hostname. Get your username and password fromthe Mailgun control panel, under theDomains section.
Depending on how the domain is configured in Mailgun, the SMTP hostname iseither
smtp.mailgun.orgorsmtp.eu.mailgun.org.Configure yourfirewall rulesto allow outgoing traffic on TCP port
2525.
Configuring Mailgun as a mail relay with Postfix
Configuring Mailgun as a mail relay allows thePostfix mail transfer agent toforward emails destined for remote delivery.
Connect to your instance using SSH.
gcloudcomputessh[INSTANCE_NAME]where
[INSTANCE_NAME]is the name of the VM instance where you want tosend email from.Become a superuser and set a safe umask.
user@test-instance:~$sudo su -root@test-instance:~#umask 077Install the Postfix Mail Transport Agent.
Debian
root@test-instance:~#apt update && apt -y install postfix libsasl2-modulesCentOS
root@test-instance:~#yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -yWhen prompted, select the
Local Onlyconfiguration and accept the defaultchoices for domain names.Modify the Postfix configuration options. Postfix configuration options areset 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.
Note: You must use portrelayhost = [smtp.mailgun.org]:25252525because port25isn't allowed on Compute Engine.To enforce SSL/TLS support and configure SMTP authentication for theserequests, add the following lines to the end of the file. A simple access andsecurity layer (SASL) module handles authentication in the Postfixconfiguration.
smtp_tls_security_level = encryptsmtp_sasl_auth_enable = yessmtp_sasl_password_maps = hash:/etc/postfix/sasl_passwdsmtp_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. See theMailgunhelp for instructions on how to view or change your per-domain 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 that contains your credentials because it is no longerneeded.
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 testyour configuration.Debian
root@test-wheezy:~#apt -y install mailutilsCentOS
[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 thesuccessful server response code(250).Debian
root@test-wheezy:~#tail -n 5 /var/log/syslogCentOS
[root@test-centos ~]#tail -n 5 /var/log/maillog
For detailed examples and information about other topics includingtracking and routing messages, read theMailgun documentation.
Explore reference architectures, diagrams, and best practices about Google Cloud.Take a look at ourCloud Architecture Center.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.