Send mail through the console: In order to be aware of everything that happens on the server, in addition to monitoring, it is useful to follow standard emails that I send to various services. I will tell you how to configure the server to send emails through a standard postfix with authorization to a third-party SMTP server. Also, this setting is useful for sending mail through the Linux console with SMTP authentication.
By default, in a minimal installation of CentOS 7 already comes complete mail server postfix. I’m going to use it. Standard server settings do not give proper opportunities to send mail. Will do some additional actions.
To begin with, to be able to test send mail via console, install the mailx. Without it when you try to send an email to the console will receive an error:
# mail bash: mail: command not found
In order to fix this, install mailx.
# yum install mailx
After that, you can send from your server messages to the outside world, for example.
# df -h | mail-s "Disk usage" admin@mymail.com
The mail will leave the output of the command df.
But with the standard settings, your mail will either fall into spam or even not accepted by the receiving server because your server does not have the correct settings for sending mail (dns, spf, dkim, etc.). To mail normally went it is necessary to use some external mail server. Configure postfix to send local mail through an external server with SMTP authentication.
I took the default configuration of postfix, I cleaned it from comments and spaces added to the end of your settings.
# cat /etc/postfix/main.cf
## DEFAULT CONFIG BEGIN ###################### queue_directory = /var/spool/postfix command_directory = /usr/sbin daemon_directory = /usr/libexec/postfix data_directory = /var/lib/postfix mail_owner = postfix inet_interfaces = localhost inet_protocols = all unknown_local_recipient_reject_code = 550 alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases
Create a file with information about username and password for authorization.
# mcedit /etc/postfix/sasl_passwd
mailsrv.mymail.EN:25 admin@mymail.com:password
Create a db file.
# postmap /etc/postfix/sasl_passwd
You can now restart the postfix and check the work.
# systemctl restart postfix
Sent a test email via the console. Check the mail log.
# tail-n 10 /var/log/maillog
If you have there is a similar error:
postfix/smtp[5420]: warning: SASL authentication failure: No worthy mechs found postfix/smtp[5420]: 24762774C6: to=<admin@mymail.com>, relay=mailsrv.mymail.com[10.10.30.3]:25, delay=450, delays=450/0.03/0.02/0, dsn=4.7.0, status=deferred (SASL authentication failed; cannot authenticate to server mailsrv.mymail.com[10.10.30.3]: no mechanism available)
You then need to install some more packages:
# yum install cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain
After that, restart the postfix and check the dispatch console. If everything is in order, to the standard alias for root in /etc/aliases, add an external address, which will be duplicated mail to the root account. This editable for the specified file, changing the last line.
Was:
#root: marc
Became
root: root,admin@mymail.com
Updated database of certificates:
# newaliases
That’s about it. Now all the mail addressed to local root, for example, reports from corn, will be duplicated to an external mailbox, and sending via the normal external mail server. So that mails will normally be delivered without being spam (although not necessarily, there are still heuristic filters). Now it is convenient to use a local sending in scripts without specifying additional options. Everything is already configured, you can use a simple local delivery.