Send HTML message with attachement (.pdf) from Shell

谁都会走 提交于 2021-01-29 07:29:56

问题


I understood how I can send a HTML message with sendmail doing:

(
echo "From: me@example.com";
echo "To: you@example.com";
echo "Subject: this is my subject";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "My <b>HTML message<\b> goes here!";
) | sendmail -t

I also manages to send an attachement with mail doing

uuencode file.pdf file.pdf | mail -s "my subject" you@example.com

but I fail to send an HTML message with an attachement (.pdf).

Note that I failed to install mutt or mpack (using homebrew) so far so I would love a solution that works with mail, mailx or sendmail. I am on Mac OS X 10.11


回答1:


What you need to do is use multipart mime.

Your Content-Type should be something like:

Content-Type: multipart/mixed; boundary=multipart-boundary

Your multipart-boundary can be any string you like. Then you output a line "--multipart-boundary" followed by headers, then body for each part.

For example:

--multipart-boundary
Content-Type: text/html

My <b>HTML message<\b> goes here!
--multipart-boundary
Content-Type: application/pdf
Content-Disposition: attachment; filename=file.pdf
Content-Transfer-Encoding: base64

**cat your base64 encoded file here **

--multipart-boundary--

The extra two dashes at the end mark the end of last part. You can add as many attachments as you like.



来源:https://stackoverflow.com/questions/33470547/send-html-message-with-attachement-pdf-from-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!