I want to send an HTML report that is piped from another command, eg: mycommandthatprintshtml | mail [email protected] . When I do this currently, it prints all the html tags. Is there a way I can set the required headers to make it display in HTML formatted?
2 Answers
In addition to the email body, you also need to print the email headers:
echo "From: [email protected]\r\nDate: $(date)\r\nSubject: subject\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=utf-8\r\n\r\n$(mycommandthatprintshtml)" mail [email protected]
Note that From, Date, Subject are mandatory. MIME-Version and Content-Type are there to help the recipient understand how the email is formatted.
-
Message-ID is mandatory as well, or does SSMTP add it if necessary?Giel– Giel2014-08-30 12:41:29 +00:00Commented Aug 30, 2014 at 12:41
-
The RFC 5322 says that the message-id should be present, but it's not mandatory. Most of the servers will add one for you if it isn't. I don't know if SSMTP adds one for you.Frederik Deweerdt– Frederik Deweerdt2014-08-30 16:00:18 +00:00Commented Aug 30, 2014 at 16:00
-
Any chance of an example with HTML message plus attachment(s)?RTF– RTF2016-10-17 16:12:55 +00:00Commented Oct 17, 2016 at 16:12
-
@RTF late comment, but likely possible using
multipart/mixed. See unix.stackexchange.com/a/250189/182991bvj– bvj2018-09-30 05:57:15 +00:00Commented Sep 30, 2018 at 5:57 -
@Giel SSMTP adds message id when necessary on my servers. I know I'm a bit late, but maybe someone else will neeed that information.Kamil– Kamil2023-01-16 16:55:50 +00:00Commented Jan 16, 2023 at 16:55
with ssmtp you can send a mail from a file and specify headers inside (similar to what Federik did)
ssmtp -t < mail.txt
mail.txt:
to: [email protected]
bcc: [email protected]
From: [email protected]
MIME-Version: 1.0
Content-Type: text; charset=utf-8
Subject: Some subject
Dear Person,
....