Sendmail 通过邮件发送 corrupted/unreadable pdf,以 Linux 中的 base64 编码发送

Sendmail sending corrupted/unreadable pdf over mail, sending with base64 encoding in Linux

我正在使用 shell 脚本发送电子邮件,我使用的代码如下所示:

declare -a ATTACH
ATTACH="TEST.pdf"
(
echo "To: user1@domain.com";
echo "Cc: user2@domain.com";
echo "Subject: Example Subject";
echo "MIME-Version: 1.0";
echo "Content-Type:multipart/mixed; boundary=\"B835649000072104Jul07\"";

echo "--B835649000072104Jul07";
echo "Content-Type: text/html;charset=\"UTF-8\"";
echo "Content-Transfer-Encoding: 7bit";
echo "Content-Disposition: inline";
echo "<html><head></head><body>Example</body></html>"
echo ""
echo "--B835649000072104Jul07";
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"';
echo "Content-Transfer-Encoding: base64";
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"';
base64 $(basename $ATTACH)
echo
echo "--B835649000072104Jul07";
) | /usr/sbin/sendmail -t -oi

我的目标是发送 HTML 正文文本和 pdf 附件。上面的代码能够发送 Content-type: text.csv 的有效 CSV 文件,也可以使用上面的脚本发送 pdf 文件,但该 pdf 文件不可读或已损坏。我彻底搜索了这个问题,但只找到了另一种编码 uuencode 编码,但我听说 base64 编码更好更有效。
我应该做哪些更改才能有效且完全有效地发送 pdf 文件。我将不胜感激。

好的,我对代码做了一些改动,当我用 base64 给 OpenSSL 命令时它起作用了。

declare -a ATTACH
ATTACH="TEST.pdf"
(
echo "To: user1@domain.com";
echo "Cc: user2@domain.com";
echo "Subject: Example Subject";
echo "MIME-Version: 1.0";
echo "Content-Type:multipart/mixed; boundary=\"B835649000072104Jul07\"";

echo "--B835649000072104Jul07";
echo "Content-Type: text/html;charset=\"UTF-8\"";
echo "Content-Transfer-Encoding: 7bit";
echo "Content-Disposition: inline";
echo "<html><head></head><body>Example</body></html>"
echo ""
echo "--B835649000072104Jul07";
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"';
echo "Content-Transfer-Encoding: base64";
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"';
echo
openssl base64 < /home/username/TEST.pdf
) | /usr/sbin/sendmail -t -oi

现在可以使用了:-)