Sending email with Mailgun

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

  1. Sign up for and create a new Mailgun account on theGoogle Cloud Marketplace.

  2. 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 iseithersmtp.mailgun.org orsmtp.eu.mailgun.org.

  3. Configure yourfirewall rulesto allow outgoing traffic on TCP port2525.

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.

  1. 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.

  2. Become a superuser and set a safe umask.

    user@test-instance:~$sudo su -
    root@test-instance:~#umask 077
  3. Install the Postfix Mail Transport Agent.

    Debian

    root@test-instance:~#apt update && apt -y install postfix libsasl2-modules

    CentOS

    root@test-instance:~#yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y

  4. When prompted, select theLocal Only configuration and accept the defaultchoices for domain names.

  5. Modify the Postfix configuration options. Postfix configuration options areset in themain.cf file. Open the file with the text editor of your choice.

    root@test-instance:~#vi /etc/postfix/main.cf
  6. If they exist, comment out the following lines.

    # default_transport = error# relay_transport = error
  7. Add the Mailgun SMTP service by adding the following line to the end of the file.

    relayhost = [smtp.mailgun.org]:2525
    Note: You must use port2525 because port25 isn't allowed on Compute Engine.
  8. 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 = noanonymous
  9. Save your changes and close the file.

  10. Generate the SASL password map.

    1. Create a new password file that is ready for standard input.

      root@test-instance:~#cat > /etc/postfix/sasl_passwd<< EOF
    2. At the prompt, enter the service details, replacingYOUR_SMTP_LOGIN andYOUR_SMTP_PASSWORD with 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_PASSWORD
    3. Close and save the file by typing the delimiter,EOF.

      >EOF
  11. Use thepostmap utility to generate a.db file.

    root@test-instance:~#postmap /etc/postfix/sasl_passwd
    root@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
  12. Next, remove the file that contains your credentials because it is no longerneeded.

    root@test-instance:~#rm /etc/postfix/sasl_passwd
  13. Set the permissions on your.db file.

    root@test-instance:~#chmod 600 /etc/postfix/sasl_passwd.db
    root@test-instance:~#ls -la /etc/postfix/sasl_passwd.db-rw------- 1 root root 12288 Aug 31 18:51 /etc/postfix/sasl_passwd.db
  14. Finally, reload your configuration to load the modified parameters.

    Debian

    root@test-wheezy:~#/etc/init.d/postfix restart

    CentOS

    [root@test-centos ~]#postfix reload

  15. Test your configuration. Install themailx ormailutils package and testyour configuration.

    Debian

    root@test-wheezy:~#apt -y install mailutils

    CentOS

    [root@test-centos ~]#yum install mailx -y

    Send a test message.

    root@test-instance:~#echo 'Test passed.' | mail -s 'Test-Email' EMAIL@EXAMPLE.COM

    Look in your systems logs for a status line containingstatus and thesuccessful server response code(250).

    Debian

    root@test-wheezy:~#tail -n 5 /var/log/syslog

    CentOS

    [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.