views:

367

answers:

5

As the question title says, how can I send html mail using a shell script?

+25  A: 

First you need to compose the message. The bare minimum is composed of these two headers:

MIME-Version: 1.0
Content-Type: text/html

... and the appropriate message body:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head><title></title>
</head>
<body>

<p>Hello, world!</p>

</body>
</html>

Once you have it, you can pass the appropriate information to the mail command:

$body = '...'

echo $body | mail \
-a "From: [email protected]" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
[email protected]

This is an oversimplified example, since you also need to take care of charsets, encodings, maximum line length... But this is basically the idea.

Alternatively, you can write your script in Perl or PHP rather than plain shell.

Update

A shell script is basically a text file with Unix line endings that starts with a line called shebang that tells the shell what interpreter it must pass the file to, follow some commands in the language the interpreter understands and has execution permission (in Unix that's a file attribute). E.g., let's say you save the following as hello-world:

#!/bin/sh

echo Hello, world!

Then you assign execution permission:

chmod +x hello-world

And you can finally run it:

./hello-world

Whatever, this is kind of unrelated to the original question. You should get familiar with basic shell scripting before doing advanced tasks with it. Here you are a couple of links about bash, a popular shell:

http://www.gnu.org/software/bash/manual/html_node/index.html

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

Álvaro G. Vicario
can your answer in second part .. I don't know how to use it ?
Tree
I'm unsure about your question... Are you familiar with shell scripts? Where do you have the information you want to mail?
Álvaro G. Vicario
No i never used shell script .. its just point i need to use it part of my application development ...
Tree
Email should never be HTML, or indeed anything but plain text. \*waves grumpy old man stick\*
Zack
+6  A: 

The tags include 'sendmail' so here's a solution using that:

(
echo "From: [email protected] "
echo "To: [email protected] "
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/alternative; " 
echo ' boundary="some.unique.value.ABC123/server.xyz.com"' 
echo "Subject: Test HTML e-mail." 
echo "" 
echo "This is a MIME-encapsulated message" 
echo "" 
echo "--some.unique.value.ABC123/server.xyz.com" 
echo "Content-Type: text/html" 
echo "" 
echo "<html> 
<head>
<title>HTML E-mail</title>
</head>
<body>
<a href='http://www.google.com'&gt;Click Here</a>
</body>
</html>"
echo "--some.unique.value.ABC123/server.xyz.com"
) | sendmail -t

A wrapper for sendmail can make this job easier, for example, mutt:

mutt -e 'set content_type="text/html"' [email protected] -s "subject" <  message.html
mdma
what is use of echo ' boundary="some.unique.value.ABC123/server.xyz.com"' ?
Tree
what is use of echo "--some.unique.value.ABC123/server.xyz.com"
Tree
The "--some.unique.value...", which corresponds to the `boundary="some.unique.value..."` in the headers, is MIME's way of separating multipart messages. When it sees that, it knows that what follows is a new part, and that it should go back to parsing headers. This example is a bit more complicated than it has to be, as the multipart stuff isn't strictly necessary, but it's not a bad idea if you're sending HTML mail.
cHao
+1  A: 

Another option is the sendEmail script http://caspian.dotconf.net/menu/Software/SendEmail/, it also allows you to set the message type as html and include a file as the message body. See the link for details.

Matti Pastell
+1  A: 

shell send html email - The UNIX and Linux Forums

http://www.unix.com/shell-programming-scripting/80973-shell-send-html-email.html

Sending email from a shell script - Shell Scripting

http://www.daniweb.com/forums/thread15280.html

ratty
+1  A: 

Another option is using msmtp.

What you need is to set up your .msmtprc with something like this (example is using gmail):

account default
host smtp.gmail.com
port 587
from [email protected]
tls on
tls_starttls on
tls_trust_file ~/.certs/equifax.pem
auth on
user [email protected]
password <password>
logfile ~/.msmtp.log

Then just call:

(echo "Subject: <subject>"; echo; echo "<message>") | msmtp <[email protected]>

in your script

Ashaman