3

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?

5

With mutt:

SMTP_URL='smtps://user:password@smtp.gmail.com' mutt \
  -F /dev/null \
  -e 'set from="Me <user@gmail.com>"' \
  -e 'set smtp_url=$SMTP_URL' \
  -s 'pl12.png file' \
  -a pl12.png --  \
  someone@example.com << 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:password@smtp.gmail.com' mutt -F /dev/null -e 'set from="Me <user@gmail.com>"' -e 'set smtp_url=$SMTP_URL' -s 'pl12.png file' -a pl12.png  -- someone@example.com
  • It must be -a filenames -- have a look at the mutt --help – Joerg Mar 20 '18 at 21:32
4

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
AuthUser=rpi3abc@gmail.com
AuthPass=testing123
UseTLS=YES

Send a one-liner like so:

echo "Testing...1...2...3" | ssmtp myusername@gmail.com

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 abc@def.com, it is accomplished with the following single command line:

mpack -s "12-Volt Battery Pack plot" /home/pi/python/pl12.png abd@def.com

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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.