12

We use mutt to send out emails.

mutt -s "Test" -e "Content-Type: text/html" test@example.com < message.html

We send out our email updates in HTML format. We would like to send emails as multi-part containing both a text version and an html version.

  1. 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?
  2. Can we use mutt to send the multipart email created in step 1, from the linux prompt?

Envirnonment: RedHat Enterprise Linux 5, mutt

0
10

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 <news@example.com>
To: Joe User <joe.u@example.net>
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 joe.u@example.net)

1
  • Thanks for the great answer, grawity! Thanks also for explaining in detail how to compose a multipart email. :) – user544192 May 23 '11 at 21:40
0

Take a look at https://github.com/shivylp/mailprod. I developed this small Python utility/library since using command-line for large mails did not seem to be a reliable option. With mailprodcli it is as simple as saying mailprodcli template.xml --send. Example template.xml which even includes an image is as simple as:

<?xml version="1.0" encoding="UTF-8"?>
<mail>
  <from>sender@source.com</from>
  <to>recipient1@destination1.com</to>
  <to>recipient2@destination2.com</to>
  <body type="text/html">
    <![CDATA[<html>
<b>Neat!</b><br><center><img src="cid:sample"></center>
</html>]]>
  </body>
  <body type="image" src="sample.png" id="sample" />
</mail>
1
  • Will this also send a plaintext version? I believe OP wants to send a multi-part email where both HTML and plaintext content is included in the same message. – Cas Oct 7 '16 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.