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 -- username@example.com < /dev/null
Sending a message body using redirection:
$ mutt -s "Email Subject" username@example.com < email.html
Sending a file attachment along with a message body:
$ echo $message | mutt -s "Email Subject" -a attachment.txt -- username@example.com
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 <some.sender@gmail.com>
To: Some Recipient <some.recipient@example.com>
Subject: Saying Hello
Date: Tue, 6 Jun 2023 04:15:06 -0100
Message-ID: <1234@local.machine.example>
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 'some.sender@gmail.com' \
--mail-rcpt 'some.recipient@example.com' \
--upload-file mail.txt \
--user 'some.sender@gmail.com: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 -- user@example.com < /dev/null
Sending a message body using redirection:
$ mutt -s "Email subject" test@example.com < email.html
Sending a file attachment along with a message body:
$ echo $message | mutt -s "Subject" -a attachment.txt -- me@myemail.com
mailx Example:
$ echo "message body" | mail -s "subject" test@example.com