Simplest way to send mail with image attachment from command line using gmail?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

With mutt:

SMTP_URL='smtps://user:<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8d8c9dbdbdfc7dacce8dbc5dcd886cfc5c9c1c486cbc7c5">[email protected]</a>' mutt 
  -F /dev/null 
  -e 'set from="Me <<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5a5c4a5d6f48424e4643014c4042">[email protected]</a>>"' 
  -e 'set smtp_url=$SMTP_URL' 
  -s 'pl12.png file' 
  -a pl12.png --  
  <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20534f4d454f4e45604558414d504c450e434f4d">[email protected]</a> << 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,nnsee pl12.png attachednn-- nMen' | SMTP_URL='smtps://user:<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2a3b29292d35283e1a29372e2a743d373b333674393537">[email protected]</a>' mutt -F /dev/null -e 'set from="Me <<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96e3e5f3e4d6f1fbf7fffab8f5f9fb">[email protected]</a>>"' -e 'set smtp_url=$SMTP_URL' -s 'pl12.png file' -a pl12.png  -- <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7c60626a60616a4f6a776e627f636a216c6062">[email protected]</a>

Method 2

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:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03716c6c773e71736a3062616043646e626a6f">[email protected]</a>
mailhub=smtp.gmail.com:465
FromLineOverride=YES
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="074672736f527462753a75776e3466656447606a666e6b2964686a">[email protected]</a>
AuthPass=testing123
UseTLS=YES

Send a one-liner like so:

echo "Testing...1...2...3" | ssmtp <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35584c404650475b545850755258545c591b565a58">[email protected]</a>

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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="badbd8defadedfdc94d9d5d7">[email protected]</a>

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.



All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x