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:)

3
  • What do you mean by HTML email. HTML pages at attachment or the email should be in HTLM format?
    – Space
    Aug 26, 2009 at 8:18
  • the email should be in HTLM format
    – omg
    Aug 26, 2009 at 8:39
  • 1
    Is sendmail a requirement? I can do the same thing using basic POSIX mail but don't have access to sendmail. Sep 18, 2014 at 6:01

8 Answers 8

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:

4
  • 1
    @NiKiZe MIME-Version is required if you use MIME features such as Content-Type: which obviously you need to use here.
    – tripleee
    Nov 2, 2015 at 6:49
  • 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.
    – NiKiZe
    Nov 3, 2015 at 7:01
  • Here is the MIME-Version requirement RFC2045 Jan 20, 2016 at 10:58
  • 1
    This works. If your HTML is in a file, just remove "echo "${message}";" and replace by "cat yourfile.html;" Jul 26, 2019 at 21:08
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.

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?
    – omg
    Aug 26, 2009 at 8:39
  • 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.
    – Space
    Aug 26, 2009 at 8:49
  • 1
    The only part I'm not clear about is :boundary="PAA08673.1018277622/server.xyz.com". What does it mean?
    – omg
    Aug 26, 2009 at 14:05
  • sendmail needs configuration before actually sending out email,right?
    – omg
    Aug 26, 2009 at 16:07
  • 1
    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. :(
    – bgoodr
    Dec 14, 2017 at 22:30
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

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.

1
  • Not the same thing as the author questioned for, but probably still helpful.
    – ypnos
    Aug 26, 2009 at 8:26
5

-a option?

Cf. man page:

-a file
          Attach the given file to the message.

Result:

Content-Type: text/html: No such file or directory
1
  • you need newer version of mail
    – Jasonw
    Dec 17, 2019 at 1:32
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 
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.

-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]
2
  • can you explain what the -a does ?
    – MaXi32
    Jul 6, 2020 at 21:17
  • the -a command doesn't work in CentOS but works in ubuntu
    – MaXi32
    Jul 6, 2020 at 21:40

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