使用linux命令在电子邮件中嵌入图像 [英] Embedding an image in an email using linux commands
问题描述
是否可以使用mutt
或sendmail
之类的linux命令将图像嵌入到电子邮件的正文中?
Is there a way to embed images into the body of an email using linux commands like mutt
or sendmail
?
我用了
mutt -e 'set content_type="text/image"' \
u.mohan@6dtech.co.in -s "TEST" \
-i image001.jpg < data.txt
但是它不起作用.
推荐答案
我编写了一个shell脚本,用mutt
发送带有嵌入式图像而不是链接图像的HTML消息.
I have written a shell script to send with mutt
an HTML message with embedded images rather than linked ones.
几个步骤:
- 下载通过
<img>
标记链接的原始HTML中的所有图像文件, - 通过将
src
网址更改为cid
,准备HTML文件, - 使用
(neo)mutt
准备多部分电子邮件
- 修复此电子邮件中的一些内容描述标签
- 发送
sendmail
- download all image files linked by
<img>
tags in the original HTML, - prepare the HTML file by changing the
src
url to acid
, - prepare a multipart e-mail with
(neo)mutt
- fix some content description tags in this e-mail
- send with
sendmail
这是将HTML文件名作为参数的主脚本(不执行任何检查,请不要将其视为Alpha软件):
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"
还需要一个自定义sendmail命令(在上面为mysendmail
),该命令对由mutt
生成的电子邮件文件进行后处理:
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 $*
我已经在GMail和其他一些网络邮件中对其进行了测试.欢迎报告邮件客户端或网络邮件的问题.
I have tested it in GMail and a few other webmails. Reports of problems with mail clients or webmails welcome.
这篇关于使用linux命令在电子邮件中嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!