Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sending email from a shell script using sendmail or mail

973 views
Skip to first unread message

Mark S. Reichman

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
I want to send a simple email from a unix
shell script. The message must include
a subject. I have done this...

%> echo 'this is the body of the email' | sendmail joe_s...@acme.com

The above does work, I get a body,
but I dont get a subject line. It's blank.

I have also done this..

%> echo 'subject: this is a subject' | sendmail joe_...@acme.com

This does put 'this is a subject' on the Subject: line
but no body. I discovered this out by total luck.
I figure I must be on to something here.
I would like to have a subject and a body.
Can any refer me to a tutorial on the web about formating text files
to be piped to sendmail or mail for that matter.
Does anyone have other simpler ideas?
I have read the man pages on sendmail and rfc 821 but they
dont seem help on this area.

Thanks..


Barry Margolin

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
In article <36c09...@news.rlcn.rl.af.mil>,

See RFC 822 for the full syntax of a message, which is what sendmail
wants. E.g.

sendmail joe_...@acme.com <<EOM
To: joe_...@acme.com
Subject: this is a subject

This is the body
EOM

Also, if the destination address is coming from user-supplied data, I
suggest not putting it on the command line, but instead putting it in the
header. The -t option to sendmail tells it to look for To/Cc/Bcc headers
to determine where to send the message.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.

pdca...@my-dejanews.com

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
In article <36c09...@news.rlcn.rl.af.mil>,

reic...@removethis.rl.af.mil. (Mark S. Reichman) wrote:
> I want to send a simple email from a unix
> shell script. The message must include
> a subject. I have done this...
>
> %> echo 'this is the body of the email' | sendmail joe_s...@acme.com
>
> The above does work, I get a body,
> but I dont get a subject line. It's blank.
>
> I have also done this..
>
> %> echo 'subject: this is a subject' | sendmail joe_...@acme.com
>
> This does put 'this is a subject' on the Subject: line
> but no body. I discovered this out by total luck.
> I figure I must be on to something here.
> I would like to have a subject and a body.
> Can any refer me to a tutorial on the web about formating text files
> to be piped to sendmail or mail for that matter.
> Does anyone have other simpler ideas?
> I have read the man pages on sendmail and rfc 821 but they
> dont seem help on this area.
>
> Thanks..
>
>

Note: send an email to yourself using all headers and view the header contents
of the email received to view the proper order of "Subject: ", "Cc: ", etc...

Script something like:
# ==========================================================================
# Quick Email Demo Version 1.0
# ==========================================================================

to="to_...@hostname.domain" # Send mail to
from="from...@hostname.domain" # Send mail from (reply address)
subject="Hello. This is a test message" # mail subject line
cc="none" # Send mail Carbon Copy to
bcc="to_...@hostname.domain" # Send blind carbon copy
body="blah blah\nblah blah" # Body of email message
# Note: multi-line using "\n"
importance="none" # Supported by some email programs
# valid: "high", "low"

# Of course you could use parameters to script instead of "hardcoding"
# e.g. importance="$7"

# Create a pipe to the sendmail utility to set message headers and body
{
echo "From: $from"
echo "Subject: $subject"

# If the cc variable is not "none" then echo the cc field
if ( [ "$cc" != "none"] ) then
echo "Cc: $cc"
fi

# If the bcc variable is not "none" then echo the bcc field
if ( [ "$bcc" != "none"] ) then
echo "Bcc: $bcc"
fi

# Other options may be set here
# An example is importance (high,low) - or "none" when not set
if ( [ "$importance" != "none ] ) then
echo "Importance: $importance"
fi

echo "$message"
} | /usr/lib/sendmail -t $to

# You can also use any other valid headers for your mail reader, for
# example if you have Microsoft Exchange server you can set the sensitivity
# of the message by inserting "Sensitivity: $sensitivity" in the header
# similar to Cc, Bcc. Like I said, send yourself an email and look at the
# order of the headers u want to use and echo them out in the same order
# IMPORTANT: DO NOT HAVE A NEWLINE IN THE HEADER. I WILL PLACE ANYTHING
# AFTER IN THE BODY OF THE MESSAGE. (depends on sendmail utility version)
# ==========================================================================
# End of Script
# ==========================================================================

Cheers,
Paul

------------------------------------------------------------------------
pdca...@my-dejanews.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Gilbert Soueidy

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
What unix are you using ?

The command /usr/bin/mailx exists on Solaris, and allows you to
specify a subject on the line command : mailx -s 'This is my subject'.
On Linux, the command /bin/mail allow you the same thing. Do a
simple man mail and see what your manual tells you.

This was the easy way; If you wanna do it using the hard way, you
must compose your email's COMPLETE text (with body and headers)
before sending it to sendmail. I know that on Solaris, the following should
work :
/usr/bin/echo "From: em...@domain.com\nReturn-Path: em...@domain.com\nTo:
em...@domain.com\nSubject: yourSubject\nxxxx" | sendmail -t

if you use echo, be ware of the shell built-in command, it's a little bit
different

Cheers

Mark S. Reichman wrote in message <36c09...@news.rlcn.rl.af.mil>...

Mark S. Reichman

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
Hey thanks.. I am using Solaris at the moment. I am always
conscious of writing code as portable as possible. After
posting I did find the mailx command. Thanks for your
sendmail example. You da man!!

Later Mark

In article <79rfm0$cfm$1...@zeus.esplanade3000.net>, gsou...@kleline.com says...

Marco Magagnini

unread,
Feb 11, 1999, 3:00:00 AM2/11/99
to
A simple way to know how to format file directed to sendmail is to look at a
wll-formed message (i.e. those ones created with a mail programs such as
pine, elm, Eudora, Outlook and so on.). Try looking directly your e-mail
queue in /var/spool/<YOUR-LOGIN>.
However a simple mail message will be:

##############################
From: <fr...@aaa.bbb.ccc>
To: <t...@aaa.bbb.ccc>
Subject: your subject

mail body
##############################

Be carefull of putting the line between mail-header and mail-body (i.e.
after 'Subcjet:')

Hope this may help


Marco


--
Please remove '-MAPS-' from my e.mail address to send me e-mail

Rimuovi '-MAPS-' dal mio indirizzo di posta elettronica per rispondermi
Mark S. Reichman ha scritto nel messaggio

0 new messages