Sending Email from the Command Line with mutt
If you prefer using mutt as your command line email
tool, then here are a couple of examples of using mutt to
send emails which can be automated quite easily to use in scripts.
Sending a file attachment (-a) and an empty message body:
$ mutt -s "System Logs" -a /var/log/somelog.log -- [email protected] < /dev/null
Sending a message body using redirection:
$ mutt -s "Email Subject" [email protected] < email.html
Sending a file attachment along with a message body:
$ echo $message | mutt -s "Email Subject" -a attachment.txt -- [email protected]
Sending Email from the Command Line with cURL
Let’s assume that you had a RFC 5822 formatted file named
mail.txt as follows:
From: Some Sender <[email protected]>
To: Some Recipient <[email protected]>
Subject: Saying Hello
Date: Tue, 6 Jun 2023 04:15:06 -0100
Message-ID: <[email protected]>
This is a message just to say hello.
So, "Hello".
You can forward this to a email recipient using Google’s SMTP with
the following curl command provided you have a Google app
password:
$ curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \
--mail-from '[email protected]' \
--mail-rcpt '[email protected]' \
--upload-file mail.txt \
--user '[email protected]:your-gmail-app-password'
Output should be something like this:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12 0 0 100 12 0 10 0:00:01 0:00:01 --:--:-- 10
$
Sending Email from the Command Line
mutt Examples
Sending just a file attachment (-a) and an empty message body:
$ mutt -s "System logs" -a /opt/backup.sql -- [email protected] < /dev/null
Sending a message body using redirection:
$ mutt -s "Email subject" [email protected] < email.html
Sending a file attachment along with a message body:
$ echo $message | mutt -s "Subject" -a attachment.txt -- [email protected]
mailx Example:
$ echo "message body" | mail -s "subject" [email protected]