Stay organized with collections
Save and categorize content based on your preferences.
Google Cloud works with Mailgun to provide an email service that has a
programmatic API, log retention, email personalization, analytics, and email
validation.
The following instructions show you how to configure Mailgun as an email relay
with Postfix.
Get your credentials. The instructions 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.
Depending on how the domain is configured in Mailgun, the SMTP hostname is
either smtp.mailgun.org or smtp.eu.mailgun.org.
Configure your 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.
Connect to your instance using SSH.
gcloudcomputessh[INSTANCE_NAME]
where [INSTANCE_NAME] is the name of the VM instance where you want to
send email from.
When prompted, select the Local Only configuration and accept the default
choices for domain names.
Modify the Postfix configuration options. Postfix configuration options are
set in the main.cf file. Open the file with the text editor of your choice.
Add the Mailgun SMTP service by adding the following line to the end of the
file.
relayhost = [smtp.mailgun.org]:2525
To enforce SSL/TLS support and configure SMTP authentication for these
requests, add the following lines to the end of the file. A simple access and
security layer (SASL) module handles authentication in the Postfix
configuration.
At the prompt, enter the service details, replacing YOUR_SMTP_LOGIN and
YOUR_SMTP_PASSWORD with your credentials. See the Mailgun
help
for instructions on how to view or change your per-domain credentials.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-26 UTC."],[[["\u003cp\u003eGoogle Cloud utilizes Mailgun for email services, offering a programmatic API, log retention, email personalization, analytics, and email validation.\u003c/p\u003e\n"],["\u003cp\u003eThis guide provides instructions on how to configure Mailgun as an email relay using Postfix, enabling the forwarding of emails for remote delivery.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves setting up a Mailgun account, obtaining SMTP credentials, and configuring firewall rules to allow outgoing traffic on TCP port 2525.\u003c/p\u003e\n"],["\u003cp\u003ePostfix's \u003ccode\u003emain.cf\u003c/code\u003e file requires modifications to relay email through Mailgun, including setting the \u003ccode\u003erelayhost\u003c/code\u003e to \u003ccode\u003e[smtp.mailgun.org]:2525\u003c/code\u003e and configuring SMTP authentication with SSL/TLS support.\u003c/p\u003e\n"],["\u003cp\u003eUsers can test their configuration by sending a test email and verifying its successful delivery in the system logs, looking for a successful server response code (250).\u003c/p\u003e\n"]]],[],null,["*** ** * ** ***\n\nGoogle Cloud works with Mailgun to provide an email service that has a\nprogrammatic API, log retention, email personalization, analytics, and email\nvalidation.\n\nThe following instructions show you how to configure Mailgun as an email relay\nwith Postfix.\n\nBefore you begin\n\n1. Sign up for and create a new Mailgun account on the\n [Google Cloud Marketplace](https://console.cloud.google.com/marketplace/details/mailgun/emailapi).\n\n2. Get your credentials. The instructions require that you know your Mailgun\n SMTP username, password, and hostname. Get your username and password from\n the Mailgun control panel, under the **Domains** section.\n\n Depending on how the domain is configured in Mailgun, the SMTP hostname is\n either `smtp.mailgun.org` or `smtp.eu.mailgun.org`.\n3. Configure your [firewall rules](/vpc/docs/using-firewalls#creating_firewall_rules)\n to allow outgoing traffic on TCP port `2525`.\n\nConfiguring Mailgun as a mail relay with Postfix\n\nConfiguring Mailgun as a mail relay allows the\n[Postfix](http://www.postfix.org) mail transfer agent to\nforward emails destined for remote delivery.\n\n1. Connect to your instance using SSH.\n\n gcloud compute ssh [INSTANCE_NAME]\n\n where `[INSTANCE_NAME]` is the name of the VM instance where you want to\n send email from.\n2. Become a superuser and set a safe umask.\n\n ```\n user@test-instance:~$ sudo su -\n ``` \n\n ```\n root@test-instance:~# umask 077\n ```\n3. Install the Postfix Mail Transport Agent.\n\n\n Debian\n\n\n ```\n root@test-instance:~# apt update && apt -y install postfix libsasl2-modules\n ```\n\n \u003cbr /\u003e\n\n CentOS\n\n\n ```\n root@test-instance:~# yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y\n ```\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n4. When prompted, select the `Local Only` configuration and accept the default\n choices for domain names.\n\n5. Modify the Postfix configuration options. Postfix configuration options are\n set in the `main.cf` file. Open the file with the text editor of your choice.\n\n ```\n root@test-instance:~# vi /etc/postfix/main.cf\n ```\n6. If they exist, comment out the following lines.\n\n # default_transport = error\n # relay_transport = error\n\n7. Add the Mailgun SMTP service by adding the following line to the end of the\n file.\n\n relayhost = [smtp.mailgun.org]:2525\n\n | **Note:** You must use port `2525` because port `25` isn't allowed on Compute Engine.\n8. To enforce SSL/TLS support and configure SMTP authentication for these\n requests, add the following lines to the end of the file. A simple access and\n security layer (SASL) module handles authentication in the Postfix\n configuration.\n\n smtp_tls_security_level = encrypt\n smtp_sasl_auth_enable = yes\n smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd\n smtp_sasl_security_options = noanonymous\n\n9. Save your changes and close the file.\n\n10. Generate the SASL password map.\n\n 1. Create a new password file that is ready for standard input.\n\n ```\n root@test-instance:~# cat \u003e /etc/postfix/sasl_passwd \u003c\u003c EOF\n ```\n 2. At the prompt, enter the service details, replacing `YOUR_SMTP_LOGIN` and\n `YOUR_SMTP_PASSWORD` with your credentials. See the [Mailgun\n help](https://help.mailgun.com/hc/en-us/articles/203380100-Where-Can-I-Find-My-API-Key-and-SMTP-Credentials)\n for instructions on how to view or change your per-domain credentials.\n\n ```\n \u003e [smtp.mailgun.org]:2525 YOUR_SMTP_LOGIN:YOUR_SMTP_PASSWORD\n ```\n 3. Close and save the file by typing the delimiter, `EOF`.\n\n ```\n \u003e EOF\n ```\n11. Use the `postmap` utility to generate a `.db` file.\n\n ```\n root@test-instance:~# postmap /etc/postfix/sasl_passwd\n ``` \n\n ```\n root@test-instance:~# ls -l /etc/postfix/sasl_passwd*\n\n -rw------- 1 root root 68 Jun 1 10:50 /etc/postfix/sasl_passwd\n -rw------- 1 root root 12288 Jun 1 10:51 /etc/postfix/sasl_passwd.db\n ```\n12. Next, remove the file that contains your credentials because it is no longer\n needed.\n\n ```\n root@test-instance:~# rm /etc/postfix/sasl_passwd\n ```\n13. Set the permissions on your `.db` file.\n\n ```\n root@test-instance:~# chmod 600 /etc/postfix/sasl_passwd.db\n ``` \n\n ```\n root@test-instance:~# ls -la /etc/postfix/sasl_passwd.db\n\n -rw------- 1 root root 12288 Aug 31 18:51 /etc/postfix/sasl_passwd.db\n ```\n14. Finally, reload your configuration to load the modified parameters.\n\n\n Debian\n\n\n ```\n root@test-wheezy:~# /etc/init.d/postfix restart\n ```\n\n \u003cbr /\u003e\n\n CentOS\n\n\n ```\n [root@test-centos ~]# postfix reload\n ```\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n15. Test your configuration. Install the `mailx` or `mailutils` package and test\n your configuration.\n\n\n Debian\n\n\n ```\n root@test-wheezy:~# apt -y install mailutils\n ```\n\n \u003cbr /\u003e\n\n CentOS\n\n\n ```\n [root@test-centos ~]# yum install mailx -y\n ```\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n Send a test message. \n\n ```\n root@test-instance:~# echo 'Test passed.' | mail -s 'Test-Email' EMAIL@EXAMPLE.COM\n ```\n\n Look in your systems logs for a status line containing `status` and the\n successful server response code `(250)`.\n\n\n Debian\n\n\n ```\n root@test-wheezy:~# tail -n 5 /var/log/syslog\n ```\n\n \u003cbr /\u003e\n\n CentOS\n\n\n ```\n [root@test-centos ~]# tail -n 5 /var/log/maillog\n ```\n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\nFor detailed examples and information about other topics including\ntracking and routing messages, read the\n[Mailgun documentation](https://documentation.mailgun.com).\n\n\nExplore reference architectures, diagrams, and best practices about Google Cloud.\nTake a look at our\n[Cloud Architecture Center](/architecture)."]]