Skip to main content

Send email with multiple inline images via bash with a loop

23-07-2018 | Remy van Elst | Text only version of this article


Table of Contents


Recently I had a request from a user that whished to receive a scheduled email with two screenshots. The screenshots were automated via AutoIt on a network share, the user manually logged in every evening to check the pictures. With bash and postfix/sendmail we can automate this process, the user now doesn't have to login but can just check their email. There are a lot of snippets and guides to attach emails via the shell, but displaying multiple images inline as an HTML mail was something I had to figure out. You cannot embed the image in base64 HTML because Outlook doesn't show that, you must use the Content-ID style embed. Like UUENCODE, but more complicated. (The next step in this process with the user is to automate the reason why they have to check those screenshots every night, that is something for another article)

If you like this article, consider sponsoring me by trying out a Digital Ocean VPS. With this link you'll get a $5 VPS for 2 months free (as in, you get $10 credit). (referral link)

The script first fetches the images from a remote URL and places them on the filesystem, in a date-structured folder for archival purposes. It then constructs the HTML email, appending the attachments as base64 encoded content below the text part of the email.

Script

#!/bin/bash
# date with hour and minute for in filename
CURDATE=$(date +%Y%m%dT%H%M)
# date for folder name
FOLDERDATE=$(date +%Y%m%d)
# path where files are stored for archival
SAVEPATH=/opt/monitoring/screenshots/$FOLDERDATE
mkdir -p $SAVEPATH
# declare the array with the images to mail, can be as many as you want.
declare -A ATTS
#filename without extension. Key = header, value is filename without ext
ATTS["Screenshot 1, Most important"]="image1"
ATTS["Screenshot 2, Some more info"]="image2"
# extension must all be the same
EXT=".png"
# loop over all images and download them locally
$(for key in "${!ATTS[@]}"; do 
  value=${ATTS[$key]};
  /usr/bin/wget -q -O $SAVEPATH/$value-$CURDATE$EXT https://[...]/$value$EXT;
  cp $SAVEPATH/$value-$CURDATE$EXT $SAVEPATH/$value$EXT;
done)

for key in "${!ATTS[@]}"; do 
  # due to formatting of the base64 output in the command loop
  # the file must be base64 encoded in a variable.
  # here we encode the file, in the loop we get in it a var.
  value=${ATTS[$key]};
  /usr/bin/base64 "$SAVEPATH/$value$EXT" > $SAVEPATH/$value$EXT.base64;
done

/usr/sbin/sendmail -t <<EOT
TO: user@example.org
FROM: automation@example.com
BCC: manager@example.org
SUBJECT: State of the union $(date +%c) 
MIME-Version: 1.0
Content-Type: multipart/related;boundary="XYZ"

--XYZ
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
$(for key in "${!ATTS[@]}"; do 
  value=${ATTS[$key]};
  echo "<h1>$key</h1>";
  echo "<img src=\"cid:$value\"></br>";
done)
<a href="https://[...]">More information</a>
</body>
</html>

$(for key in "${!ATTS[@]}"; do 
  value=${ATTS[$key]};
  ATT=$(cat $SAVEPATH/$value$EXT.base64);
  echo "--XYZ"
  echo "Content-Type: image/png;name=\"$value$EXT\""
  echo "Content-Transfer-Encoding: base64"
  echo "Content-ID: <$value>"
  echo "Content-Disposition: inline; filename=\"$value$EXT\""
  echo ""
  echo "$ATT"
done)
--XYZ--
EOT


for key in "${!ATTS[@]}"; do 
  value=${ATTS[$key]};
  rm $SAVEPATH/$value$EXT.base64;
done

The --XYZ variable has been copied from a script online.

More images can be added in this loop by editing the array ATTS. Make sure they all have the same extension.


Tags: attachments  automation  bash  inline  mail  outlook  postfix  sendmail  shell  snippets  uuencode