Sometimes, we need to send email via command lines, specifically for cron jobs or bash scripts. Message and results can be sent automatically when tasks are finished. One of most popular mailing tools is gnu mailutils. Below are the brief steps to install and use mailutils.
1. install the packages:
sudo apt-get install mailutils
sudo apt-get install sharutils (if you’d like to send attachments)
In the progress, one exim4 configuration screen will be prompted out.
2. usual way to write email
mail firstname.lastname@example.org, edit CC, subject, body, finish with ‘Ctrl-D’
3. file content as body text
mail -s test email@example.com < test.txt
4. mail with attachments
uuencode attachment_name attachment_display_name | mail -s subject address
5. check whether the email has been sent out
if “Mail queue is empty” is shown, then the mail has been successfully sent.
clear mail queue:
flush all: /usr/lib/sendmail -v -q [v shows the progress]
flush specific domain: /usr/lib/sendmail -v -qR[firstname.lastname@example.org]