Shell Script to Send Email

Shell script to send email

Yes it works fine and is commonly used:

$ echo "hello world" | mail -s "a subject" someone@somewhere.com

Sending a mail from a linux shell script

If the server is well configured, eg it has an up and running MTA, you can just use the mail command.

For instance, to send the content of a file, you can do this:

$ cat /path/to/file | mail -s "your subject" your@email.com

man mail for more details.

Sending HTML mail using a shell script

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">
<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: me@example.com" \
-a "MIME-Version: 1.0" \
-a "Content-Type: text/html" \
-s "This is the subject" \
you@example.com

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

Sending bcc email to recipient using shell script mail utility


MAIL(1)                   BSD General Commands Manual                  MAIL(1)

NAME
mail - send and receive mail

SYNOPSIS
mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr... [-- sendmail-options...]
mail [-iInNv] -f [name]
mail [-iInNv] [-u user]
-c Send carbon copies to list of users.
-b Send blind carbon copies to list. List should be a comma-separated list of names.

The command is(I have tested it on CentOS6.x):

 echo -e "body" | mail  -S smtp=localhost -s "Test subject 1"  -b bccuser@gmail.com user@gmail.com

The to-addr is after cc-addr and bcc-addr

How do I send a file as an email attachment using Linux command line?

None of the mutt ones worked for me. It was thinking the email address was part of the attachment. Had to do:

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.example

Sending email using unix shell scripting

You forgot the quotes:

echo $body | mail $receiver -s "$subj"

Note that you must use double quotes (otherwise, the variable won't be expanded).

Now the question is: Why double quotes around $subj and not $body or $receiver. The answer is that echo doesn't care about the number of arguments. So if $body expands to several words, echo will just print all of them with a single space in between. Here, the quotes would only matter if you wanted to preserve double spaces.

As for $receiver, this works because it expands only to a single word (no spaces). It would break for mail addresses like John Doe <doe@none.com>.



Related Topics



Leave a reply



Submit