using mutt to send html email with an attachment
Asked Answered
P

2

10

I have been using mutt for some to time to send plain text emails with a pdf attachment using:

mutt -s "Subject" -a file.pdf < mybody.txt

and html emails without an attachment using:

mutt -e "my_hdr Content-Type: text/html" -s "Subject" < mybody.html

but I now want send an html email with a pdf attachment and:

mutt -e "my_hdr Content-Type: text/html" -s "Subject" -a file.pdf < mybody.html

does not work.

Both the html and encoded pdf along with their headers show as plain text in the body of the email.

Anyone know how to do this?

Cheers

Gary

Pelisse answered 29/11, 2012 at 14:16 Comment(0)
A
7

You can't really do this. Mutt was not meant for sending messages programmatically, it's meant to be an interactive mail client; the command-line flags for sending messages are there only as a minor add-on, there is much that is not possible in that way.

The my_hdr command isn't supposed to be used to set MIME headers like Content-Type. It doesn't really work even when you don't include an attachment. The message from your second command would have two Content-Type headers; the first one that mutt generates which states that the message is text/plain, and the second one that you specified. It just happens that the client you're using to check the message is looking at your header. For me mutt will display that message as HTML, but thunderbird will display the un-rendered document.

When trying to combine sending HTML and an attachment, the same thing happens. There are multiple Content-Type headers. The first is from mutt saying that the message is multipart/mixed, the second saying that it's text/html. In my testing mutt will again respect the second header and so try to render the entire message, including the attachment, as HTML. Thunderbird again honors the first Content-Type header and so finds two parts, the HTML and the attachment, but the HTML part doesn't have a Content-Type header of its own specifying that it is HTML, so it isn't rendered as such instead the raw source is shown.

You could get somewhat close by sending both the HTML and the PDF as attachments, with an empty body:

mutt -s "Subject" -a mybody.html -a file.pdf -- [email protected] < /dev/null

But you'd likely be better off using a tool that was actually designed to send more complex messages programmatically.

Agronomy answered 29/11, 2012 at 15:49 Comment(0)
H
9

By Caleb Roger Davis

Mutt Version: 1.5.21

With some research and help with from a co-worker, I was able to get this to work ( send html file as body & pdf attachment ).

I have the .html file & .pdf as separate files. I do some keyword substitution in the .html file (replacing customer name etc) before sending.

Here is the bash script that works.

I think I could get this to work with sendmail as well if anyone is interested.

#!/bin/bash
START=`date`

# --------------------------------------------------------------------------------------
# can be comma delimited list of email addresses
[email protected]
[email protected]

subject="Monthly PDF report"
attachments="../src/test/resources/my.pdf ../src/test/resources/my2.pdf"

html_file="../src/main/resources/email_template.html"

# --------------------------------------------------------------------------------------
# send html with pdf attachment
# You can send additional attachments, the attachment list can be terminated with the "--"
mutt -e "set content_type=text/html" -s "$subject" $TO -a $attachments -- < $html_file

END=`date`
echo "START=$START"
echo "END=$END"
Hanford answered 4/3, 2014 at 17:55 Comment(1)
Just for the record, @MoabTek's solution worked for me. I am using mutt 1.5.20.Appetence
A
7

You can't really do this. Mutt was not meant for sending messages programmatically, it's meant to be an interactive mail client; the command-line flags for sending messages are there only as a minor add-on, there is much that is not possible in that way.

The my_hdr command isn't supposed to be used to set MIME headers like Content-Type. It doesn't really work even when you don't include an attachment. The message from your second command would have two Content-Type headers; the first one that mutt generates which states that the message is text/plain, and the second one that you specified. It just happens that the client you're using to check the message is looking at your header. For me mutt will display that message as HTML, but thunderbird will display the un-rendered document.

When trying to combine sending HTML and an attachment, the same thing happens. There are multiple Content-Type headers. The first is from mutt saying that the message is multipart/mixed, the second saying that it's text/html. In my testing mutt will again respect the second header and so try to render the entire message, including the attachment, as HTML. Thunderbird again honors the first Content-Type header and so finds two parts, the HTML and the attachment, but the HTML part doesn't have a Content-Type header of its own specifying that it is HTML, so it isn't rendered as such instead the raw source is shown.

You could get somewhat close by sending both the HTML and the PDF as attachments, with an empty body:

mutt -s "Subject" -a mybody.html -a file.pdf -- [email protected] < /dev/null

But you'd likely be better off using a tool that was actually designed to send more complex messages programmatically.

Agronomy answered 29/11, 2012 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.