Embedding an image in an email using linux commands
Asked Answered
R

7

10

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.

Rolandrolanda answered 17/1, 2013 at 14:19 Comment(0)
G
5

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.

Giuseppinagiustina answered 5/2, 2019 at 14:32 Comment(0)
E
0

You can attach the image, by changing the -i in your command line to a -a. This won't embed the image perse, but will include it. If you want to embed it, the mail you send will have to be of content type text/html and include an img tag to show the attached image.

See this SO page about how to correctly embed image attachments in HTML mail.

Embedding attached images in HTML emails

Ester answered 2/2, 2013 at 0:32 Comment(3)
actually in mutt on option was there to attach files inline using -i but it was not workingRolandrolanda
It sounds like -i is only for text files for the body of the message.Ester
am using -a option to attach but i need inline attach asper my requirementsRolandrolanda
C
0

For those looking to send emails with embedded images as part of the email using a bash script, I pieced this code together.

The email is started with these line:

EMAILBODY="echo \"Alarm went off! "
EMAILATTACH=""

Inside of a loop that defines each file to attach:

EMAILATTACH=$EMAILATTACH" -a /home/xyz/"$ID"/"$Event"/"$Frame"-capture.jpg"
EMAILBODY=$EMAILBODY"<BR> <IMG Height=150 SRC=\"$Frame-capture.jpg\">"

After the loop, the email is completed with these lines:

EMAILBODY=$EMAILBODY"\" | mutt -e \"set content_type=text/html\" -s \"House Alarm went off!\""
EMAILSTRING=$EMAILBODY$EMAILATTACH" -- [email protected]"
eval $EMAILSTRING

My last hurdle is that when I receive this on my android phone (maybe the same on other browsers), it doesn't display the picture, only a small box (even after you have downloaded the attachments). It shows up fine in Outlook though.

Chirpy answered 3/5, 2013 at 23:42 Comment(1)
this doesn't work because it misses the Content-ID for the attachment to be linked into the html email.Show
I
0

It's even possible with the basic mail command

You want to create a mime HTML email a la:

How to embed images in email

Then take the headers (all the lines before the first boundary), remove them from that input and add them individually with the -a command after mail such as:

https://mcmap.net/q/146433/-how-to-send-html-email-using-linux-command-line or Sending HTML mail using a shell script

Interlay answered 9/1, 2014 at 16:37 Comment(1)
There is no single universal mail comcand with this capability. If yours does; good for you; but for this to be useful to othes, this answer should describe which platform you're on.Bilious
P
0

Here is an improved version of the Joce's script.

This one does not require a custom sendmail command, because it creates one on the fly, uses it and deletes it afterwards.

It is parametric, so you don't need to change its code for different recipients and the like, and it offers a few other goodies.

The first few lines of code should be clear enough to explain the five positional parameters meaning, but here is an example, just in case:

<script.sh> /srv/emailbody.html "Sender's Name" [email protected] "Embedded images" 'Recipent's name <[email protected]>' 

It depends on mktemp (the original script did not), because I like that more than using $$, but that's only a matter of taste.

#!/bin/bash

HTMLFULLPATH="$1"
SENDER="$2"
SENDEREMAIL="$3"
SUBJECT="$4"
RECIPIENT="$5"

HTML=$(basename "${HTMLFULLPATH}")
SENDERDOMAIN=$(echo "${SENDEREMAIL}" | cut -d@ -f2)

if ! [[ "${RECIPIENT}" == '*<*' ]] ; then
  RECIPIENT="${RECIPIENT}"'<'"${RECIPIENT}"'>' # TO_NO_BRKTS_* SpamAssassin rules
fi

function atexit
{
  rm -rf "${TEMPDIR}" "${NEOMUTTCONFIG}" >/dev/null 2>&1
}

trap atexit INT TERM EXIT

TEMPDIR=$(mktemp -d)
mkdir -p "${TEMPDIR}/img"
grep "src=" "${HTMLFULLPATH}" | sed -e "s,.*src=\"\([^\"]*/\)*\([^\"/]*\)\".*,wget \1\2 -O ${TEMPDIR}/img/\2," > "${TEMPDIR}/getimg.sh"
bash "${TEMPDIR}/getimg.sh" >/dev/null 2>&1
sed -e 's,src="\([^"]*/\)*\([^"/]*\)",src="cid:\2@'${SENDERDOMAIN}'",g' < "${HTMLFULLPATH}" > "${TEMPDIR}/${HTML}"
SENDMAIL="${TEMPDIR}/sendmail.sh"

cat > "${SENDMAIL}" << EOF
#!/bin/bash
sed -e 's,Content-Disposition: attachment; filename="\([^"]*\)",Content-Disposition: inline; name="\1"\nContent-ID: <\1@'${SENDERDOMAIN}'>,' < /dev/stdin | sed -e 's,Content-Type: multipart/mixed;,Content-Type: multipart/related;,' | sendmail \$*
EOF

chmod a+rx "${SENDMAIL}"

NEOMUTTCONFIG=$(mktemp)

echo 'set from="'"${SENDER}"' <'"${SENDEREMAIL}"'>"' >> "${NEOMUTTCONFIG}"
echo 'set smtp_url=""' >> "${NEOMUTTCONFIG}"
echo 'set sendmail="'${SENDMAIL}'"' >> "${NEOMUTTCONFIG}"
echo "set content_type=text/html" >> "${NEOMUTTCONFIG}"

neomutt -F "${NEOMUTTCONFIG}" "${RECIPIENT}" -s "${SUBJECT}" -a "${TEMPDIR}/img/"* < "${TEMPDIR}/${HTML}"
Punctilio answered 12/7, 2019 at 23:20 Comment(0)
A
0

I built a command line for this purpose https://github.com/gonejack/embed-email

go get github.com/gonejack/embed-email
Usage:
  embed-email *.eml [flags]

Flags:
  -v, --verbose   verbose
  -h, --help      help for embed-email
Asexual answered 29/3, 2021 at 2:26 Comment(0)
E
-1
EMAILBODY="echo \"Alarm went off! "
EMAILATTACH=""
EMAILATTACH=$EMAILATTACH" -a "/home/uat12mgr/XXDBD_AR_INV_PRINT.jpg""
EMAILBODY=$EMAILBODY"<BR> <IMG Height=150 SRC=\""/home/uat12mgr/XXDBD_AR_INV_PRINT.jpg"\">"
EMAILBODY=$EMAILBODY"\" | mutt -e \"set content_type=text/html\" -s \"House Alarm went off!\""
EMAILSTRING=$EMAILBODY$EMAILATTACH" -- [email protected]"
eval $EMAILSTRING
Evermore answered 23/9, 2016 at 3:5 Comment(2)
This is not working for me. Error in command line: content_type: unknown variableEvermore
It is IIRC from version 1.5.2 something that it was added to muttShow

© 2022 - 2024 — McMap. All rights reserved.