Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using mutt to send html email with an attachment

Tags:

mutt

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

like image 610
garyLynch Avatar asked Nov 29 '12 14:11

garyLynch


People also ask

How do you send attachments on mutt?

Use mutt command in following format to specify subject, message body and attachment to send mail from command line. -s used to specify subject of mail. -i used to specify file containing message body. -a used to specify attachment file.

How do you send an attachment in HTML?

For html emails you have to set this in the header of the email content-type: text/html . However, if you want to send an attachment you have to change it to content-type: multipart/mixed . Which would make the html email...not html anymore.

How do you send an email on mutt?

Specify a draft file which contains header and body to use to send a message. Specify a file to include into the body of a message. Causes Mutt to bypass the system configuration file. Resume a postponed message.

How do I read emails on mutt?

To read the emails of a specific user, you need to specify which mail file to read. For example, You (as root) wants to read mails of user “John“, you need to specify his mail file with “-f” option with mutt command. You may also use “-R” option to open a mailbox in read-only mode.


2 Answers

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"
like image 173
Caleb Avatar answered Sep 30 '22 02:09

Caleb


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.

like image 37
qqx Avatar answered Sep 30 '22 03:09

qqx