Using mailx to send emails in Unix

Published by Utkarsh Patel on 08/02/09 16:32:31
Last edited on 03/06/09 16:38:50

mailx is a Unix utility program that is used for sending and receiving mail and is an improved version of the mail utility. This article will focus on only sending emails using the mailx utility within a shell script.

Syntax: mailx [-s subject] [-a attachment] [-r from addr] to-addr

You can put the body of the subject right after the mailx command and terminate it with EOT (End of Transmission). However I find it easier to simply write your entire output to a temporary file and email the file content as the body of the email. This way the mailx command is only 1 line.

Usage:

#!/bin/bash
subject="Email Subject"
to="to@address.com"
from="from@address.com" 

echo This is the body of the email >> sendmail.tmp
echo This is the 2nd line in the email >> sendmail.tmp
echo `date` : is the date >> sendmail.tmp 

mailx -s{$subject} -r "YourName<$from>" $to < sendmail.tmp 

 

About the Author

Utkarsh Patel is one of our newest members, and he could not have come at a better time. He has helped bring back stability and control to Techlicity Ventures during our rapid growth. If he isn't marvelling at a recently discovered Oracle feature or technique he is bothering the rest of the team on why we should move everything over to Oracle databases. Utkarsh complements the team well with his determination and sound research.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!