How to send a html email with the bash command "sendmail"?
Asked Answered
I

8

33

Anyone has a demo available?

Sendmail is said to be not scalable,but it's free,so I decided to use it first for now:)

Interlingua answered 26/8, 2009 at 8:9 Comment(3)
What do you mean by HTML email. HTML pages at attachment or the email should be in HTLM format?Relegate
the email should be in HTLM formatInterlingua
Is sendmail a requirement? I can do the same thing using basic POSIX mail but don't have access to sendmail.Dormouse
F
63

The following works:

(
echo "From: ${from}";
echo "To: ${to}";
echo "Subject: ${subject}";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo "${message}";
) | sendmail -t

For troubleshooting msmtp, which is compatible with sendmail, see:

Frontal answered 8/4, 2013 at 13:1 Comment(4)
@NiKiZe MIME-Version is required if you use MIME features such as Content-Type: which obviously you need to use here.Ierna
True, it should be there to follow spec. however many clients will use the Content-Type header without caring for MIME-Version. If this is for an bigger audience ofcourse you should follow spec, but for a quick hack (if you need to type it out to send email to self) it might not be needed to get desired result.Tell
Here is the MIME-Version requirement RFC2045Vlada
This works. If your HTML is in a file, just remove "echo "${message}";" and replace by "cat yourfile.html;"Kellby
R
19

If I understand you correctly, you want to send mail in HTML format using linux sendmail command. This code is working on Unix. Please give it a try.

echo "From: [email protected]
To: [email protected]
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary='PAA08673.1018277622/server.xyz.com'
Subject: Test HTML e-mail.

This is a MIME-encapsulated message

--PAA08673.1018277622/server.xyz.com
Content-Type: text/html

<html> 
<head>
<title>HTML E-mail</title>
</head>
<body>
<a href='http://www.google.com'>Click Here</a>
</body>
</html>
--PAA08673.1018277622/server.xyz.com
" | sendmail -t

For the sendmail configuration details, please refer to this link. Hope this helps.

Relegate answered 26/8, 2009 at 8:24 Comment(8)
If the content of email is pre-generated and restored in a file called content.html,how to send it to B from A with sendmail?Interlingua
if you want to read the contents from an external file you can use some bash script to read lines from external html file and put in at data field of the email OR just simply copy paste the html code. Also, please have a look to link given by virus. Hope this helps.Relegate
The only part I'm not clear about is :boundary="PAA08673.1018277622/server.xyz.com". What does it mean?Interlingua
sendmail needs configuration before actually sending out email,right?Interlingua
Yes you have configure sendmail before using it. If you still wondering about the details, I have edited my comment with the link which was helpful for me last time.Relegate
I believe the boundary='...' line needs to appended to the line right above it, the Content-Type:...; line, right? I tried to make the edit, but StackExchange blocked it as not enough characters to be changed. :(Kevon
Yes, and the boundary string should be in double quotes, not single.Tyrocidine
and I think the final boundary should end with -- as well as begin.Tyrocidine
D
6

I understand you asked for sendmail but why not use the default mail? It can easily send html emails.

Works on: RHEL 5.10/6.x & CentOS 5.8

Example:

cat ~/campaigns/release-status.html | mail -s "$(echo -e "Release Status [Green]\nContent-Type: text/html")" [email protected] -v

CodeShare: http://www.codeshare.io/8udx5

Dormouse answered 18/9, 2014 at 6:11 Comment(0)
A
6

-a option?

Cf. man page:

-a file
          Attach the given file to the message.

Result:

Content-Type: text/html: No such file or directory
Antevert answered 18/8, 2017 at 14:0 Comment(2)
you need newer version of mailStinko
Just read the docs. Thanks!!!Burka
Z
5

This page should help - http://www.zedwood.com/article/103/bash-send-mail-with-an-attachment

It includes a script to send e-mail with a MIME attachment, ie with a HTML page and images included.

Zizith answered 26/8, 2009 at 8:13 Comment(1)
Not the same thing as the author questioned for, but probably still helpful.Thalassography
B
4

Found solution in http://senthilkl.blogspot.lu/2012/11/how-to-send-html-emails-using-sendemail.html

sendEmail -f "oracle@server" -t "[email protected]" -u "Alert: Backup complete" -o message-content-type=html -o message-file=$LOG_FILE  -a $LOG_FILE_ATTACH 
Byte answered 8/3, 2018 at 12:1 Comment(0)
S
2

To follow up on the previous answer using mail :

Often times one's html output is interpreted by the client mailer, which may not format things using a fixed-width font. Thus your nicely formatted ascii alignment gets all messed up. To send old-fashioned fixed-width the way the God intended, try this:

{ echo -e "<pre>"
echo "Descriptive text here."
shell_command_1_here
another_shell_command
cat <<EOF

This is the ending text.
</pre><br>
</div>
EOF
} | mail -s "$(echo -e 'Your subject.\nContent-Type: text/html')" [email protected]

You don't necessarily need the "Descriptive text here." line, but I have found that sometimes the first line may, depending on its contents, cause the mail program to interpret the rest of the file in ways you did not intend. Try the script with simple descriptive text first, before fine tuning the output in the way that you want.

Still answered 18/11, 2014 at 16:26 Comment(0)
C
-2

It's simpler to use, the -a option :

cat ~/campaigns/release-status.html | mail -s "Release Status [Green]" -a "Content-Type: text/html" [email protected]
Copperplate answered 4/12, 2015 at 10:23 Comment(2)
can you explain what the -a does ?Whimwham
the -a command doesn't work in CentOS but works in ubuntuWhimwham

© 2022 - 2024 — McMap. All rights reserved.