📜 ⬆️ ⬇️

Mutt, ssmtp and sending reports

At one time, when I was not yet on Habré, this article was published in a sandbox. Unfortunately, nobody liked it and was eventually removed. Long thought what to do with it - to score, post it yet on Habré or on your blog Notes programmer . I decided, since it was written for Habr, let it be published on it. So, under the cut - an article on the practice of sending reports by e-mail. Now, rereading the text, I understand that the article is not a fountain, but maybe someone will come in handy.

Often there is a task like this: it is necessary to process such logs monthly (or weekly / daily), build a report on them and send it to the specified E-Mail. In this article I just would like to tell about the second part of such a task, namely about sending reports. It would seem such a routine operation, but difficulties arise with it more often than it might seem at first glance.

I prefer to use ready-made, time-tested programs, rather than writing my own scripts each time. In most cases, the necessary program was once written by someone, why reinvent the wheel?
')
I use mutt to send reports. I am attracted to the simplicity of the interface, the age (read stability) of the program and the fact that mutt is ported to almost all unix-like systems if not all. In my favorite FreeBSD, look for it in / usr / ports / mail / mutt.

To send a letter with an attach with the help of mutt, the command is enough

mutt -s Subject -a File User@Host.com

where Subject is the subject of the message, File is the name of the attached file, and User@Host.com is the recipient of the letter. You can attach several files to the letter or specify several recipients. The program reads the text of the letter from stdin.

By default, mutt uses sendmail as the MTA. Usually there is no difficulty with this - after installing the mutt, we can immediately start writing scripts and everything will work fine. But I had to deal with the situation when sending letters directly from the server is simply prohibited by the firewall rules. In this case, use a third-party SMTP server.

The problem is that mutt does not have MTA functions, that is, working with SMTP is not his task. However, you can teach him to send mail through a given smtp-server and the ssmtp program will help him with this.

Strictly speaking, ssmtp is not an MTA, but simply an smtp client that emulates the work of sendmail. If you, like me, are using FreeBSD, you can install ssmtp from ports - / usr / ports / mail / ssmtp.

Configuring ssmtp is done by editing the file /usr/local/etc/ssmtp/ssmtp.conf (the path to the file may be different on your OS). Here is how it looks like:

root=reports@example.ru #
mailhub=smtp.example.ru:465 # smtp-
hostname=localhost # ,
UseTLS=Yes #
AuthUser=reports@example.ru #
AuthPass=qwerty #
FromLineOverride=Yes # From: mutt


If you want to send reports via Gmail, the config will look a little different:

root=reports@gmail.com
mailhub=smtp.gmail.com:587
hostname=localhost
UseSTARTTLS=YES
AuthUser=reports@gmail.com
AuthPass=qwerty


We are trying to send a test letter using ssmtp:

echo "" | ssmtp -v -s Hello admin@example.ru

If the letter is successfully reached, then everything was done correctly. If ssmtp says something like:

. . .
[->] MAIL FROM:<root@localhost>
[<-] 573 root@localhost failed to route the address
ssmtp: 573 root@localhost failed to route the address


also not a problem, just ssmtp indicated the wrong address of the sender, and the smtp-server noticed this. When we send mail through mutt, everything will be set correctly.

To mutt send mail via ssmtp, in the home directory of the user, on behalf of which we are going to send reports, you need to correct the file .muttrc

set sendmail="/usr/local/sbin/ssmtp" # ssmtp
set realname="Example Report System" #
set from="reports@example.ru" # e-mail


Checking the work of mutt:

echo "" | mutt -s Hello admin@example.ru

The letter must successfully reach the recipient. If you want to use mutt in cron scripts, you should keep in mind that mutt is in the / usr / local / bin directory, which by default is not set in the PATH environment variable in your crontab. Decisions two - either correct the PATH environment variable in the crontab, or use the full name mutt in the scripts.

It also happens that on the server from which you have to send reports, we do not have root privileges. In this case, you either have to persuade the admin to install and configure mutt, or still write your own script to send email. But that's another story.

Source: https://habr.com/ru/post/82919/


All Articles