Embedding an image in an email using linux commands

前端 未结 6 644
醉话见心
醉话见心 2021-01-02 06:33

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 \         


        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 07:33

    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 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:\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"
    

    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: <\1@example.com>,' < /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.

提交回复
热议问题