This is a text-only version of the following page on https://raymii.org:
---
Title       : 	Send email with multiple inline images via bash with a loop
Author      : 	Remy van Elst
Date        : 	23-07-2018
URL         : 	https://raymii.org/s/snippets/Send_email_with_multiple_inline_images_via_bash.html
Format      : 	Markdown/HTML
---



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)

<p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called  Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p>


![][2]

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.

   [1]: https://www.digitalocean.com/?refcode=7435ae6b8212
   [2]: https://raymii.org/s/inc/img/automate-all-the-things.png

---

License:
All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.