Often, when you have a dedicated server for your needs, it becomes necessary to send emails from the server, for example, to notify user about the status of programs working there.
In such cases Postfix email transfer agent can help.
I will consider only setup which I use personally on a permanent basis is to use Gmail relay to send emails through. But it can be adapted to other relays if your hosting provides some.
¶Configuration
First of all, you need to create a file for simple authentication and security layer (SASL), or simply speaking for authentication.
/etc/postfix/sasl/sasl_passwd
:
[smtp.gmail.com]:587 <account_name>@gmail.com:<password>
If you use some other relay replace Gmail server and port by yours, note that []
must be preserved.
Internally, Postfix uses lookup tables to store / receive data thus we should prepare our authentication info by executing the following command:
postmap /etc/postfix/sasl/sasl_passwd
This will generate lookup table in format according to default_database_type
setting (may be specified explicitly in configuration file main.cf
).
To inspect its current value you can execute the following command:
postconf -d | grep default_database_type
Despite what format you chose or used default one, you need to set up proper rights so only root account could access authentication info:
chmod 600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.*
Gmail uses Transport Layer Security to encrypt the emails during transit so you also need to install a collection of trusted certificates.
I omit its installation procedure cause it depends on your distro but the package itself called ca-certificates
and information about its installation can be easily found on the web.
You can check it exists with ls
:
ls /etc/ssl/certs/ca-certificates.crt
Finally, you need to add the following lines to the main configuration file /etc/postix/main.cf
:
relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = lmdb:/etc/postfix/sasl/sasl_passwd smtp_sasl_security_options = noanonymous smtp_use_tls = yes smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
¶Testing
Now, it is time to check the setup.
To compose the email and send it you should use mailx
command line utility.
mailx is part of POSIX standard so available across different Unix-like OS / distros.
echo "Body of the message" | mailx -s "Subject" <recipients_email>
Then check recipient’s email for new messages.
¶Queue management
To check email queue in postfix type the following command:
mailq
For more information please refer to man pages but essentially it just shows message queue.
To erase the queue you can type the following:
postsuper -d ALL
To delete a specific message from the queue:
postsuper -d <message_id>
To hold all messages in the queue:
postsuper -h ALL
To releases all held messages back into the queue:
postsuper -r ALL
Perform structure checks and repairs on the queue:
postsuper -s
That’s all about the basics!