Linux – Send Email using mail

Background

Playing around with Linux and exploring sending emails.

Tool

mail.

Option

Option Explanation Sample
-a Attachment diskfree.txt
-b Bcc pier@dekalb.org
-c Cc joey@abc.com
-r From Sally in Accounting
-s Subject Disk Free
Recipient Address toni@nobody.com

Sample

Sample #1

Outline

  1. Run command
    • Command:- df ( diskfree )
    • Capture command into a text file ( diskfree.txt )
    • Attach command output ( diskfree.txt )
  2. The subject is “disk free on @HOSTNAME
  3. The from field should be set Susan
  4. The recipients are listed at the end of the command
    • In our case dba@companya.com

Code


df  -h > diskfree.txt |  echo "" | mail -s "disk free on $HOSTNAME " -r "Susan" -a diskfree.txt  dba@companya.com

 

Sample #2

Outline

  1. Run command
    • Command:- df ( diskfree )
    • Capture command into a text file ( diskfree.txt )
    • Send command output as email’s body
  2. The subject is “disk free on @HOSTNAME
  3. The from field should be set Susan
  4. The recipients are listed at the end of the command
    • In our case dba@companya.com

Code


df  -h > diskfree.txt |  mail -s "disk free on $HOSTNAME " -r "Susan" -a diskfree.txt  dba@companya.com < diskfree.txt

 

References

  1. die.net
    • mail(1) – Linux man page
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s