Creating a multipart email and sending it in Linux
How do we create such a multipart message in Linux when the email subject, and HTML and text versions of the email body are given?
Create a message of type multipart/alternative
as documented in RFC 2046:
From: Example Company <[email protected]> To: Joe User <[email protected]> Date: Sat, 21 May 2011 17:40:11 +0300 Subject: Multipart message example MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=asdfghjkl --asdfghjkl Content-Type: text/plain; charset=utf-8 Hello everyone! --asdfghjkl Content-Type: text/html; charset=utf-8 <!DOCTYPE html> <body> <p>Hello everyone!</p> </body> --asdfghjkl--
See RFC 2046 and RFC 5322 for the exact syntax.
Can we use mutt to send the multipart email created in step 1, from the linux prompt?
If you find a way to set the right Content-Type header. (In your example, you are using -e
, but mutt uses -e
for different purposes. Even -e "my_hdr Content-Type: ..."
leaves the original text/plain header intact.)
It's better to send generated mail directly through sendmail
. You'll have to create the headers yourself – see the example; use strftime("%a, %d %b %Y %T %z")
for Date and a string of random alphanumeric characters for the boundary. Then pipe the prepared message, including headers, to sendmail -i -t
:
sendmail -i -t < above-example.txt
(The -t
option means "get recipients from the To: line"; you can alternatively use sendmail -i [email protected]
)