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

omg
  • 136,412
  • 142
  • 288
  • 348

8 Answers8

62

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:

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Steven L
  • 15,304
  • 1
  • 21
  • 16
  • 1
    @NiKiZe MIME-Version *is* required if you use MIME features such as `Content-Type:` which obviously you need to use here. – tripleee Nov 02 '15 at 06: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 03 '15 at 07:01
  • Here is the MIME-Version requirement [RFC2045](https://tools.ietf.org/html/rfc2045#section-4) – Marko Kohtala Jan 20 '16 at 10:58
  • 1
    This works. If your HTML is in a file, just remove "echo "${message}";" and replace by "cat yourfile.html;" – Santiago Trias Jul 26 '19 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: me@xyz.com
To: them@xyz.com
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.

ocodo
  • 29,401
  • 18
  • 105
  • 117
Space
  • 7,049
  • 6
  • 49
  • 68
  • 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 '09 at 08: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 '09 at 08:49
  • 1
    The only part I'm not clear about is :boundary="PAA08673.1018277622/server.xyz.com". What does it mean? – omg Aug 26 '09 at 14:05
  • sendmail needs configuration before actually sending out email,right? – omg Aug 26 '09 at 16:07
  • 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. – Space Aug 28 '09 at 06:46
  • 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 '17 at 22:30
  • Yes, and the boundary string should be in double quotes, not single. – Tom Ellis Aug 21 '21 at 18:53
  • and I think the final boundary should *end* with `--` as well as begin. – Tom Ellis Aug 21 '21 at 19:10
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")" to.address@company.com -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.

DavidMWilliams
  • 834
  • 2
  • 14
  • 22
4

-a option?

Cf. man page:

-a file
          Attach the given file to the message.

Result:

Content-Type: text/html: No such file or directory
4

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

sendEmail -f "oracle@server" -t "name@domain.com" -u "Alert: Backup complete" -o message-content-type=html -o message-file=$LOG_FILE  -a $LOG_FILE_ATTACH 
Abdel
  • 674
  • 7
  • 13
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')" to.address@company.com

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.

Mike S
  • 1,235
  • 12
  • 19
-2

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

cat ~/campaigns/release-status.html | mail -s "Release Status [Green]" -a "Content-Type: text/html" to.address@company.com
PhilippeS
  • 5
  • 1