Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding an image in an email using linux commands

Is there a way to embed images into the body of an email using linux commands like mutt or sendmail?

I used this

mutt -e 'set content_type="text/image"' \
   [email protected] -s "TEST" \
    -i image001.jpg < data.txt

but it's not working.

like image 809
unnismohan Avatar asked Jan 17 '13 14:01

unnismohan


1 Answers

I have written a shell script to send with mutt an HTML message with embedded images rather than linked ones.

Several steps:

  1. download all image files linked by <img> tags in the original HTML,
  2. prepare the HTML file by changing the src url to a cid,
  3. prepare a multipart e-mail with (neo)mutt
  4. fix some content description tags in this e-mail
  5. send with sendmail

Here's the main script which takes the HTML filename as argument (no checks performed, please do not consider it as an alpha software):

#!/bin/bash
F=$(basename "$1")
DIR="/tmp/inlinizer-$$/"
mkdir -p $DIR/Img
grep "src=" "$1" | sed -e "s,.*src=\"\([^\"]*/\)*\([^\"/]*\)\".*,wget \1\2 -O $DIR/Img/\2," > $DIR/get_img.sh
bash $DIR/get_img.sh
sed -e 's,src="\([^"]*/\)*\([^"/]*\)",src="cid:\[email protected]",g' < "$1" > "$DIR/$F"
neomutt -e 'set smtp_url=""' -e 'set sendmail="mysendmail"' -e "set content_type=text/html" [email protected] -s "test" -a $DIR/Img/* < "$DIR/$F"

One also needs a custom sendmail command (mysendmail in the above) which post-processes the e-mail file generated by mutt:

sed -e 's,Content-Disposition: attachment; filename="\([^"]*\)",Content-Disposition: inline; name="\1"\nContent-ID: <\[email protected]>,' < /dev/stdin | sed -e 's,Content-Type: multipart/mixed;,Content-Type: multipart/related;,' | sendmail $*

I have tested it in GMail and a few other webmails. Reports of problems with mail clients or webmails welcome.

like image 148
Joce NoToPutinsWarInUkraine Avatar answered Sep 21 '22 06:09

Joce NoToPutinsWarInUkraine