SMTP - send an email from terminal

From Ever changing code
Jump to navigation Jump to search

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

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 "fromuser@gmail.com" --mail-rcpt "touser@server.tld" --url smtps://smtp.gmail.com:465 -T mail.txt

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

# Create credential file for `-n` option
cat >~/ <<EOF
# netrc, usernames and passwords for various applications
# chmod 600 ~/.netrc
machine imap.gmail.com
  login user@gmail.com
  password password1
machine smtp.gmail.com
  login user@gmail.com
  password password1
EOF

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 -- youremail@address.com < /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 -- youremail@address.com
  • Ver1 Create script emailsyslog.sh
!/bin/bash
tar -zcvf /tmp/dmk1-cradlepoint.tgz /var/log/dmk1_cradlepoint.log*
echo | mutt -s "DMK1 Syslog" -a /tmp/dmk1-cradlepoint.tgz -- youremail@address.com
  • 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" youremail@address.com

Probably need adding /tmp and then removing

  • add to daily cron jobs

work in progress

  • ready script

http://bash.cyberciti.biz/backup/backup-files-email-to-your-account/

Script is being tested

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

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


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 * * * ~/email-billion-syslog.sh >/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 * * * ~/email-syslog.sh >/dev/null 2>&1 # JOB_ID_1