Embed an image in an email using Linux commands

Is there a way to embed images in the body of a message using linux commands like mutt or sendmail ?

I used this

 mutt -e 'set content_type="text/image"' \ u.mohan@6dtech.co.in -s "TEST" \ -i image001.jpg < data.txt 

but it does not work.

+7
source share
6 answers

I wrote a shell script to send mutt HTML messages with inline images, not connected ones.

A few steps:

  1. load all image files associated with <img> tags in the source HTML,
  2. prepare the HTML file by changing the src url to cid ,
  3. prepare a composite email with (neo)mutt
  4. fix some content description tags in this email
  5. send with sendmail

Here is the main script that takes the HTML file name as an argument (checks are not performed, please do not consider it as an alpha program):

 #!/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:\ 2@example.com ",g' < "$1" > "$DIR/$F" neomutt -e 'set smtp_url=""' -e 'set sendmail="mysendmail"' -e "set content_type=text/html" me@example.com -s "test" -a $DIR/Img/* < "$DIR/$F" 

A special sendmail command is also required ( mysendmail above), which post-processes the email file generated by mutt :

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

I checked this in GMail and several other emails. Reporting problems with email clients or webmail is welcome.

+3
source

You can attach the image by changing -i on the command line to -a . This will not include the perse image, but will include it. If you want to insert it, the mail you send must be of the text/html content type and include the img tag to display the attached image.

See this SO page on how to properly embed image attachments in HTML mail.

Insert Attached Images in HTML Messages

0
source

For those who want to send emails with embedded images as part of an email using a bash script, I put this code together.

Email begins with this line:

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

Inside the loop that defines each attached file:

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

After the cycle, the email is filled with the following lines:

 EMAILBODY=$EMAILBODY"\" | mutt -e \"set content_type=text/html\" -s \"House Alarm went off!\"" EMAILSTRING=$EMAILBODY$EMAILATTACH" -- user@server.net " eval $EMAILSTRING 

My last hurdle is that when I get this on my Android phone (maybe the same in other browsers), it does not display the image, but only a small box (even after you downloaded the attachments). In Outlook, this is clearly visible.

0
source

This is possible even when using the main mail command.

Do you want to create a mime a la email:

How to embed images in email

Then take the headers (all lines before the first border), remove them from this input and add them separately using the -a command after mail, for example:

fooobar.com/questions/111379 / ... or Sending HTML mail using a shell script

0
source

Here is an improved version of the Joce script .

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

It is parametric, so you do not need to change its code for different recipients, etc., And it offers several other usefulnesses.

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

 <script.sh> /srv/emailbody.html "Sender Name" sender@example.com "Embedded images" 'Recipent name < recipient@example.net >' 

It depends on mktemp (the original script did not), because I like it more than using $$ , but this is just 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}" 
0
source
 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" -- myaka@abc.com " eval $EMAILSTRING 
-1
source

All Articles