Mail с консоли

Материал из Webko Wiki
Версия от 19:56, 20 апреля 2015; Sol (обсуждение | вклад) (Новая страница: «Отправка електронных писем с консоли Полезно для отправки автоматических отчетов. Жля…»)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к навигации Перейти к поиску

Отправка електронных писем с консоли

Полезно для отправки автоматических отчетов.

Жля начала нам нужен mailx

yum install mailx


Синтаксис

Отправить произвольное сообщение:

# echo “test-test-test” | mail -s “Subject” [email protected]

Отправить файл в теле письма:

# mail -s “Subject” [email protected] < /var/log/messages

Отправить письмо в интерактивном режиме:

# mail -s “Subject” [email protected]
test-test-test
.
Cc:
# 

(В конце тела письма обязательно строчка с единственной точной)

echo "Message Body" | mail -s "Message Subject" [email protected]

./crypto_php_check.py /var/www/ | grep "CRYPTOPHP DETECTED" | mail -s "Crypto php" [email protected]

And if you want mail to read the content from a file:

mail -s “Hello world” [email protected] < /home/calvin/application.log

Some other useful options in the mail command are: -s subject (The subject of the mail) -c email-address (Mark a copy to this “email-address”, or CC) -b email-address (Mark a blind carbon copy to this “email-address”, or BCC) Here’s how you might use these options:

echo “Welcome to the world of Calvin n Hobbes” | mail -s “Hello world” [email protected] -c [email protected] -b [email protected]

MUTT

One of major drawbacks of using the mail command is that it does not support the sending of attachments. mutt, on the other hand, does support it. I’ve found this feature particularly useful for scripts that generate non-textual reports or backups which are relatively small in size which I’d like to backup elsewhere. Of course, mutt allows you to do a lot more than just send attachments. It is a much more complete command line mail client than the “mail” command. Right now we’ll just explore the basic stuff we might need often. Here’s how you would attach a file to a mail:

echo “Sending an attachment.” | mutt -a backup.zip -s “attachment” [email protected]

This command will send a mail to [email protected] with the subject (-s) “attachment”, the body text “Sending an attachment.”, containing the attachment (-a) backup.zip. Like with the mail command you can use the “-c” option to mark a copy to another mail id.

SENDING MAIL FROM A SHELL SCRIPT

Now, with the basics covered you can send mails from your shell scripts. Here’s a simple shell script that gives you a reading of the usage of space on your partitions and mails the data to you.

#!/bin/bash
df -h | mail -s “disk space report” [email protected]

Save these lines in a file on your Linux server and run it. You should receive a mail containing the results of the command. If, however, you need to send more data than just this you will need to write the data to a text file and enter it into the mail body while composing the mail. Here’s and example of a shell script that gets the disk usage as well as the memory usage, writes the data into a temporary file, and then enters it all into the body of the mail being sent out:

#!/bin/bash
df -h > /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s “disk and RAM report” [email protected] < /tmp/mail_report.log

Now here’s a more complicated problem. You have to take a backup of a few files and mail then out. First the directory to be mailed out is archived. Then it is sent as an email attachment using mutt. Here’s a script to do just that:

#!/bin/bash
tar -zcf /tmp/backup.tar.gz /home/calvin/files
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” [email protected]

The echo at the start of the last line adds a blank into the body of the mail being set out.