SMTP - send an email from terminal

From Ever changing code

Send email using Telnet

You can send an email using native SMTP commands while connecting to a mail server using Telnet connection. In the script below text in bold are the commands followed a space then free form string appropriate to the preceding command.

telnet 25
220 Microsoft ESMTP MAIL Service, Version: 7.5.7601.17514 ready at  Thu, 31 Mar 2016 13:53:42 +0100
250 Hello []
mail from:   <- this need to be valid address, otherwise you'll get:501 5.5.4 Invalid Address
250 2.1.0 OK 
rcpt to:
250 2.1.5
354 Start mail input; end with <CRLF>.<CRLF>
from: Piotr
to: me
Subject: Test via telenet
              <- [enter] may also require a space
test body
              <- [enter] may also require a space
.  <-- the '.' dot is important as indicates EndOfMessage
250 2.6.0 <> Queued mail for delivery

Depends on your mail server the commands need to be in upper cases like HELO etc, the above was tested using Microsoft implementation.

Send email with curl to gmail

Curl can be used to as an email cliet interacting with SMTP protocol. To enable Gmail to support curl we need to allow less secure apps on your email account. Login details are stored in ~/.netrc remote FTP servers credentials file that curl also supports it with flag -n.

curl -n --ssl-reqd --mail-from "" --mail-rcpt "touser@server.tld" --url smtps:// -T mail.txt

# Create the email data/body
cat >mail.txt <<EOF
From: "FromUser" <>
To: "ToUser" <touser@server.tld>
Subject: [TEST] This is a test
I am sending this mail with curl thrugh my gmail account.

# Create credential file for `-n` option
cat >~/ <<EOF
# netrc, usernames and passwords for various applications
# chmod 600 ~/.netrc
  password password1
  password password1

Send email using Mutt

We are going to use mutt command line tool to email attached syslog on regular basis.

Install mutt
 sudo apt-get install mutt
Verify you can send attachment test-email.txt within the email message
mutt -a test-email.txt -- < /dev/null
< /dev/null sends nothing to email body
-- is required as per man page
Attach a file to your message using MIME. When attaching single or multiple files, separating filenames and recipient addresses with "--" is mandatory, e.g. mutt -a image.jpg -- addr1 or mutt -a img.jpg *.png -- addr1 addr2. The -a option must be placed at the end of command line options.
  • Compress all logs to temporary file /tmp/dmk1-cradlepoint.tgz
tar -zcvf /tmp/dmk1-cradlepoint.tgz /var/log/dmk1_cradlepoint.log*
  • Email using mutt
echo | mutt -s "Your_WHID Syslog" -a /tmp/dmk1-cradlepoint.tgz --
  • Ver1 Create script
tar -zcvf /tmp/dmk1-cradlepoint.tgz /var/log/dmk1_cradlepoint.log*
echo | mutt -s "DMK1 Syslog" -a /tmp/dmk1-cradlepoint.tgz --
  • Ver2 single command email attachment (working)

Install sharutils to

sudo apt-get install sharutils
gzip -c syslog.tar.gz /var/log/syslog | uuencode syslog.tar.gz | mail -s "Compressed syslog attachment"

Probably need adding /tmp and then removing

  • add to daily cron jobs

work in progress

  • ready script

Script is being tested

Script name will be Then the router syslog logs are zipped from /var/log/netgear.log* to single file ~/syslog/netgear-$DATE.tar.gz

DATE=$(date +"%m-%d-%Y")
tar -czf ~/syslog/netgear.$DATE.tar.gz /var/log/netgear.log* 
echo | mutt -s "DSS1 $NOW Netgear syslog" -a ~/syslog/$FILE --

Add to cron.daily, user's scripts are run from /home/username/

sudo apt-get install gnome-schedule

List a user cron jobs

crontab -u username -l

Example output, script will be run every day at 11:10am and any STDOUT will be suppressed to /dev/null

10 11 * * * ~/ >/dev/null 2>&1 # JOB_ID_1

Edit user cron tab

crontab -e
GNU nano 2.2.6                                  File: /tmp/crontab.nBbRuz/crontab
45 17 * * * ~/ >/dev/null 2>&1 # JOB_ID_1