Do you want to send emails from your Linux server but you don’t have an email server?
This feature is specially useful to provide feedback or results from your custom made scripts or any other piece of software that needs an SMTP to send emails.
You can use a Gmail account and Postfix as a Relay email server.
In this post we are going to set Postfix as an SMTP Relay server using Gmail.
This example is going to use a Debian derivated OS, but it should be almost literal in most of the cases.
There are three main steps:
- Install Postfix and its dependencies.
- Configure Postfix.
- Provide Gmail credentials.
Install Postfix and its dependences
Use your OS package manager, in this case apt. Access as “root” or add “sudo” at the beginning of the command.
apt-get install postfix mailutils libsasl2-2 libsasl2-modules
During the installation, answer this way (without the quotes):
- General type of mail configuration:
Satellite system
- System mail name: This is the name of your system. If you want/have a FQDN, you can use it here, for instance “mail.totaladministrator.net”. If not, or if you just want to send emails from localhost, use the name of your computer (if you don’t know it, you can type “hostname” in the shell).
- SMTP Relay host, type the Gmail SMTP:
[smtp.gmail.com]:587
Configure Postfix
Open “/etc/postfix/main.cf” and search for “relayhost” to make sure that “[smtp.gmail.com]:587” is set.
At the end of the config file, add the following lines:
smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous
Provide Gmail credentials
Open or create “/etc/postfix/sasl/sasl_passwd”. In this file add your credentials this way:
[smtp.gmail.com]:587 User@gmail.com:Password
If you have a professional Gmail account with a custom domain (GSuite), just configure “/etc/postfix/sasl/sasl_passwd” the same way, only replace “@gmail.com” with your “@example.com”.
Create Postfix Credentials DB File
Execute the following sentence:
postmap /etc/postfix/sasl/sasl_passwd
At this point, you shoud have two files in “/etc/postfix/sasl“, which are:
- sasl_passwd
- sasl_passwd.db
This files need to be secured.
chmod 400 /etc/postfix/sasl/sasl_passwd*
And finally restart Postfix:
service postfix restart
Test email
Test your Postfix configuration from bash:
echo "Hello World" | mail -s "Test message" youremail@example.com
Gmail account configuration
If no email is being sent, check the following in the Gmail account:
- Make sure you don’t have a 2-Step Verification. If you have it, you need to set an “App Password”.
- If the problem remains, check “Less secure Apps” config.