我正在尝试从Raspberry Pi 3上的Raspbian Jessie 8.0上的命令行发送邮件。我使用的是mailutils (sudo apt-get install mailutils)的一部分mail (mail (GNU Mailutils) 2.99.98)
我正在尝试用HTML邮件发送一个附件,其中包含一些特殊的斯文尼亚字符:
echo "Hi,<br>this is mail body with special slovenian characters: <b>ČŠŽ</b>." | mail -s "$(echo -e "Test subject\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: quoted-printable")" -A attachment.jpg my.email@provider.com问题是,收到的邮件包含附件,但不是HTML格式,特殊字符编码不正确。
如果我尝试发送不带-A参数的邮件,它会正常运行。
会有什么问题呢?
发布于 2022-02-03 10:23:53
我发现,如果您使用选项--attach,则mail.mailutils版本3.1.1将始终将消息正文视为plain/text内容类型。即使您尝试设置不同的Content-Type标头,mail.mailutils也会插入优先的Content-Type: text/plain标头。
因此,我的方法是将整个电子邮件构建为多部分电子邮件(就像90年代那样)。
发送HTML消息和CSV附件的示例代码:
#!/bin/bash
BOUNDARY="1643879608-342111580=:74189"
tmpfile=$(tempfile)
# First part - HTML content
echo "--${BOUNDARY}" >> ${tmpfile}
echo "Content-ID: <$(date +"%Y%m%d%H%M%S.%N").0@$(hostname)>" >> ${tmpfile}
echo -e "Content-Type: text/html; charset=UTF-8\n" >> ${tmpfile}
cat my_html_content.html >> ${tmpfile}
# Second part - CSV attachment
echo "--${BOUNDARY}" >> ${tmpfile}
echo "Content-ID: <$(date +"%Y%m%d%H%M%S.%N").1@$(hostname)>" >> ${tmpfile}
echo "Content-Type: text/csv; name=my_csv_attachment.csv" >> ${tmpfile}
echo "Content-Transfer-Encoding: base64" >> ${tmpfile}
echo -e "Content-Disposition: attachment; filename=my_csv_attachment.csv\n" >> ${tmpfile}
base64 my_csv_attachment.csv >> ${tmpfile}
# No more parts
echo "--${BOUNDARY}--" >> ${tmpfile}
mail --subject="My subject" \
--append="Content-Type: multipart/mixed; boundary=\"${BOUNDARY}\"" \
recipient@sample.com < ${tmpfile}
rm -f ${tmpfile}发布于 2016-04-08 02:43:04
试试yagmail --一个python包。Github:https://github.com/kootenpv/yagmail/。不仅可以很容易地在python脚本中包含并运行该功能,而且还可以容纳CLI上的部分功能。
pip install yagmail然后:
yagmail -u myemail@gmail.com
-p password
-s My Subject
-c "Hi,\nthis is mail body with slovenian characters: <b>ČŠŽ</b>."
"attachment.jpg"一个线条:
yagmail -u myemail@gmail.com -p password -s My Subject -c "Hi,\nthis is mail body with slovenian characters: <b>ČŠŽ</b>." "attachment.jpg"在contents -c中,如果您放置一个文件名,它将被附加。如果可能,电子邮件将自动作为HTML电子邮件发送。
https://stackoverflow.com/questions/36478768
复制相似问题