Send Email With Google Compute Engine
Most of the people say that Google Compute Engine cant send mail. Actually it is only blocking for non google product, and common port 25 to stop spam email.
Once you integrate Google Computer Engine With Gsuite, you can send email through 25,465,587 as well.
Let see what we need to do and what we need to configure in the server.
- Login into your Google Admin Console
- Go to Apps > G Suite > Gmail > Advanced settings. (Click On the Gmail icon, so you can saw the Advanced Settings.)
- Scroll Down to the SMTP Relay Service > Configure
Give it a name (Testing Google mail)
Allowed Senders : Only registered Apps users in my domains (Thats means all the email account that i register with google gsuite will be able to be the sender in the mail server, IE: firstname.lastname@example.org)
Tick : Only accept mail from the specified IP addresses
ADD IP RANGE: give it a name and type in your GCE server ip address. IE: 192.168.20.10
Tick: Require SMTP Authentication
Tick: Require TLS encrytion
- Click Save
Now we will proceed to ubuntu server and install related software for mail send.
- apt-get upgrade & apt install mailutils (Ubuntu) yum install -y postfix cyrus-sasl-plain mailx (Centos)
- hostname linuxscriptshub.com ( Configure your Server hostname to your domainname which is register in google gsuite IE: linuxscriptshub.com )
- vim /etc/postfix/main.cf add the following to end of this file.
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = secure
smtp_tls_mandatory_protocols = TLSv1
smtp_tls_mandatory_ciphers = high
smtp_tls_secure_cert_match = nexthop
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
relayhost = [smtp-relay.gmail.com]:587
For Centos User, just simply replace smtp_tls_CAfile to /etc/pki/tls/certs/ca-bundle.crt
- Create sasl_passwd file.
quit and save the file.
- postmap /etc/postfix/sasl_passwd
- Service postfix restart
- Create related gmail user in the server IE: johnson
sudo su johnson
- Run command to send email.
mail -s ” MailFROMGCE ” email@example.com
Thats all for the GCE with Gsuite mail sending, please do not hesitate to contact us if you found any problem during the setup.
Note: Remember to enable Less Secure apps on your gmail account that you use for sending email in server. If you are using dual authentication, please generate app password and inside into sasl_passwd again, and run through the process again.