taT4Nix | Using mailx and sending automated mails..

Ever wondered how to send an email using shell scripting and that too with an attachment.

Well, here’s the syntax to do the trick;

(cat "<filename>"; uuencode "<path-to-attachment>" ["<attachment-name-with-extn>"]) | mailx [-c <list-of-CC-addressee's>] -s "<mail-subject>" <list-of-TO-addressee's>

Very interesting, yet simple.. I will break  it into parts to give more details about the syntax;

cat "<filename>"

This forms the body part of your email. The message is kept inside some file, referred as <filename>. You need to provide full path of the file containing the message.

uuencode "<path-to-attachment>" "<attachment-name-with-extn>"

This will attach file <path-to-attachment> (fully qualified path required) you want to attach to your email. Of course, you want to give some name <attachment-name-with-extn> to your attachment. Remember to give the extension, else your mail receiver will have a tough time reading the attachment.

mailx [-c <list-of-CC-addressees>] -s "<mail-subject>"  <list-of-TO-addressees>

The square brackets denote that part is optional. Well, its not always that you have to CC someone.

Here’s an example;

(cat ${MSG_DIR}/mail_body.txt; uuencode ${ATCHMT_DIR}/attach_log.txt attachment.txt ) | mailx -c diehard.coder@gmail.com -s "This is a test email" nvarun@netbeans.org

There’s another way of sending an email, without attachments;

cat "<filename>" | mailx [-c <list-of-CC-addressees>] -s "<mail-subject>" <list-of-TO-addressees>

HTH, good luck 🙂

Advertisements

One thought on “taT4Nix | Using mailx and sending automated mails..

  1. Pingback: taT4Nix | Pattern-based Files Searching.. « PrayogShala

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s