(?) The Answer Gang (!)


By Jim Dennis, Ben Okopnik, Dan Wilder, Breen, Chris, and the Gang, the Editors of Linux Gazette... and You!
Send questions (or interesting answers) to [email protected]

There is no guarantee that your questions here will ever be answered. You can be published anonymously - just let us know!


(?) sendmail

From Kenneth Moad

Answered By Jonathan Markevich, Dan Wilder

(?) I am trying to have sendmail send the contents of a file to an email address. I want to do this from the command line though.

I think the command is something like [sendmail -t <<fff] but that does not seem to be working correctly.

(!) [Jonathan] I like to use a MUA rather than a MTA for this... I prefer mutt (of course).

mutt -s "Here's the file" -a ~/procmail.log root@localhost
(!) [Dan] On the other hand, sendmail is possibly more suitable for scripting applications, such as automatic email notifications of irregularities in logs. It offers much better portablility in scripts, and better control over headers. For example, using sendmail you have direct control over "From:" headers, which can be something of a trick with various MUAs. If needed, you can script-generate MIME attachments, secure in the knowledge that they won't be mangled by an MUA that thinks it knows more about what you want than you do.
Most MTAs offer a "sendmail" binary with at least some command line compatibility. I've used Smail, Exim, and Postfix in preference to sendmail these last ten years, and the following works just fine with all of them. Most likely, it also works with Qmail, MMDF, and anything else that attempts to offer some sendmail compatibility.
"sendmail" may not be on your path. Try

which sendmail
and if it doesn't get you anything, use the full pathname. The usual location these days is /usr/sbin/sendmail. On older systems try /usr/lib/sendmail. If that doesn't work, try "locate sendmail".
To use "sendmail -t" you put the headers in the source document, with no intervening blank lines, then an empty line, then your email text. For example (drop the indents in the real thing):
From: [email protected]
To: [email protected]
Subject: email test

This is a test.  If this was a real email you would
have been asked to read it.  This is only a test
The "<<" construct you mention is a so-called "here" document. The above example, in the context of such, would look like (again, delete any indents or ">" quoting):
/usr/sbin/sendmail -t <<fff
From: [email protected]
To: [email protected]
Subject: email test

This is a test.  If this was a real email you would
have been asked to read it.  This is only a test.
fff
This can be very handy for scripts, as the shell expands shell variables that may appear inline. So:
WHAT="small armadillo"
/usr/sbin/sendmail -t <<fff
From: [email protected]
To: [email protected]
Subject: email ${WHAT}

This is a ${WHAT}.  If this was a real email you would
have been asked to read it.  This is only a ${WHAT}.
fff
will expand what was previously "test" as "small armadillo".
To use the contents of a separate file, say, a file called "fff", use
/usr/sbin/sendmail -t <fff
note the single "<". The contents of the file need to be the same as in the "here" document, no blank lines before the end of the headers, headers including at least "From: ", "To: ", and "Subject: ", then an empty line, then the body of the email.

(?) Thank you very much for the help! I decided I will use the "<" instead of the "<<" in my script thanks to your email. You also gave me a couple of other ideas too.

shade


This page edited and maintained by the Editors of Linux Gazette Copyright © 2001
Published in issue 65 of Linux Gazette April 2001
HTML script maintained by Heather Stern of Starshine Technical Services, http://www.starshine.org/


[ Table Of Contents ][ Answer Guy Current Index ] greetings   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29 [ Index of Past Answers ]