5

Building on the previous post where the requirement was to have cron jobs send status messages via a single command line, as posted here:

Simplest way to send one-line mail out via command line using gmail?

The question came up as to how to use a single line to send an attachment file.

Scenario:

cron job generates the output file like so:

cd /home/pi/python
gnuplot plt12

the program file plt12 includes the following two lines:

set output "pl12.png"
set terminal png font "arial,11"

This is being done on a Rpi3B system connected via USB to a Nano for the ADC which does the data logging.

So, the question is, what is the easiest way to send that plot file image, using a single command line, via gmail?

2 Answers 2

6

With mutt:

SMTP_URL='smtps://user:[email protected]' mutt \
  -F /dev/null \
  -e 'set from="Me <[email protected]>"' \
  -e 'set smtp_url=$SMTP_URL' \
  -s 'pl12.png file' \
  -a pl12.png --  \
  [email protected] << EOM
Hi,

see pl12.png attached.

-- 
Me
EOM

Like every shell code, you can always put it on one line if that takes your fancy, though that doesn't help with legibility:

printf 'Hi,\n\nsee pl12.png attached\n\n-- \nMe\n' | SMTP_URL='smtps://user:[email protected]' mutt -F /dev/null -e 'set from="Me <[email protected]>"' -e 'set smtp_url=$SMTP_URL' -s 'pl12.png file' -a pl12.png  -- [email protected]
1
  • It must be -a filenames -- have a look at the mutt --help
    – Joergi
    Mar 20, 2018 at 21:32
6

Based on that previous question referenced in the OP, the easiest way to send a message with a single command line is done like so:

Simplest way to send one-line mail out via command line using gmail?


EXCERPT:

Install ssmtp with the following commands:

sudo apt-get update
sudo apt-get install ssmtp

Then go into /etc/ssmtp and edit ssmtp.conf to look like this:

root=rpi3abc@gmail
mailhub=smtp.gmail.com:465
FromLineOverride=YES
[email protected]
AuthPass=testing123
UseTLS=YES

Send a one-liner like so:

echo "Testing...1...2...3" | ssmtp [email protected]

Once that part is complete, then to send an attachment with one line is equally simple.

First, install mpack:

sudo apt-get update
sudo apt-get install mpack

then, following the scenario of having the file pl12.png to send to user [email protected], it is accomplished with the following single command line:

mpack -s "12-Volt Battery Pack plot" /home/pi/python/pl12.png [email protected]

If the file is in the current directory, then it is not necessary to use a fully-qualified path.

cron jobs always run in the user's home directory, so it is always good practice to use a full directory path to the attachment file.


1
  • 1
    root parameter in the config should be gmail.com not gmail
    – qodeninja
    Aug 26, 2018 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .