Tuesday, 16 October 2012

Send Mail In Linux With Attachment and Without Attachment



1) Send Mail Command  In Linux   


Listed below different commands to send mail.

1) Send mail by passing mail body from text file.
$ mailx -s "Email Test subject" you@me.com test@me.com  < message.txt

2) Send mail by passing mail body from echo.
$ echo "this is test body" |  mail -s  "Email Test subject"  you@me.com test@me.com

3) Send mail by passing mail body from shell script variable.
$ echo "$body" | mail -s "Email Test subject" you@me.com test@me.com

If above command is not working check below packages and services functioning:

           Check mail service is installed and running with below commands.
           1) Check  sendmail installed or not:

                   a)  For Fedora,Redhat,Cestos or rpm packages : rpm -q sendmail
                   b)  For Ubuntu,Debian or deb packages           : dpkg -s apache-perl

                    If package sendmail is not installed use following command to install sendmail.     

                   a)  For Fedora,Redhat,Cestos   : yum  install sendmail
                   b)  For Ubuntu,Debian           : apt -get sendmail

              2) Check if sendmail service is running :

                   service sendmail status

                   If service is not running start with following command

                   service sendmail start

              3) If mail service is running and mail is not sent then check the log file at /var/log/maillog.

2) Send Mail Command With Attachment In Linux:


       To send mail with attachment perform following steps:

       1)Install sendmail  with following command:

               a)  For Fedora,Redhat,Cestos   : yum  install sendmail

               b)  For Ubuntu,Debian           :  apt -get sendmail


       2) Install mutt with following command:

               a) For Fedora,Redhat,Cestos: yum  install mutt

               b)  For Ubuntu,Debian           : apt -get mutt


        3) Start sendmail service with command service sendmail start

        4) use following command to send mail with attachment


mutt -s "subject" -a "/usr/MyAttachment.txt" me@you.com < tempBody.txt

3) Send Mail In Crontab

         If user want to execute  command using crontab and send a status of command in a mail use following :

e.g  Execute backup.sh every day at 2 am in the morning and send output of script in a mail.

0  2  * *  *  usr/backup.sh | mail -s  "Email Test subject"  you@me.com test@me.com
 


Also check below links :






No comments:

Post a Comment