Bobcares

Configure Mailgun in Google Cloud – How to do

by | Jul 4, 2021

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.

  1. First, we must instance using SSH.
    gcloud compute ssh [INSTANCE_NAME]
  2. To become a superuser we can use the following command:
    $ sudo su -
  3. Then set a safe umask using the following command:
    # umask 077
  4. After that, we can install the Postfix Mail Transport Agent.
    # apt update && apt -y install postfix libsasl2-modules
  5. When prompted, select the Local Only configuration and accept the default choices for domain names.
  6. To Modify the Postfix configuration options we must open the main.cf file:
    # vi /etc/postfix/main.cf
  7. And comment out the following lines if they exist:
    # default_transport = error
    # relay_transport = error
  8. 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.

  9. 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
    
  10. After making the changes we can save the changes and close the file.
  11. For generating the SASL password map we can do the following:
    1. Create a new password file that is ready for standard input.
      # cat > /etc/postfix/sasl_passwd << EOF
    2. 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
    3. Now, close and save the file by typing the delimiter, EOF.
       > EOF
  12. Use the postmap utility to generate a .db file:
    postmap /etc/postfix/sasl_passwd
  13. 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
  14. Remove the file containing the credentials because it is no longer needed:
    rm /etc/postfix/sasl_passwd
  15. 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
    
  16. After that we can reload our configuration to load the modified parameters:
    /etc/init.d/postfix restart
  17. Install the mailutils or mailx package:
    apt -y install mailutils
  18. Send a test email:
    echo 'message' | mail -s subject email@example.com
  19. 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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.