Friday, July 29, 2011

Send Email from Command line Using ssmtp in linux

SSmtp command line email configuration:

Excellent Video tutorial to send mail from command prompt using ssmtp in linux environment
http://www.youtube.com/watch?v=QT-6NbjqjDM

Sendmail configuration:

Follow the below steps:

The SMART_HOST macro allows you to specify the host that should relay all outgoing mail that you are unable to deliver directly, and the mail transport protocol to use to talk to it.

Open your configuration file:# vi /etc/mail/sendmail.mc

Append or modify macro that read as follows :define(`SMART_HOST',`smtp.net4india.com')Replace smtp.net4india.com with your actual smtp server address.

If line contains word, dnl remove the dnl word. Regenerate a new sendmail.cf config file with m4 command:# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf


Restart sendmail service:# /etc/init.d/sendmail restart





Ksh script for sending the mail with the attachment


i have written the script which is used to sql query and retrieve the values and generate the  .csv file and mail the .csv using senmail utility

#!/usr/bin/ksh

#### Set the Path################################
export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export PATH=$PATH:$ORACLE_HOME/bin

###### N out report format####################
N_Report=N_`date +%y_%m_%d_%H_%M_%S`

##### sql report for n#########oracle Express edition SID=xe###############
sqlplus -s Username/password@SID<
SET FEEDBACK OFF;
SET ECHO OFF;
SET COLSEP '';
SET PAGESIZE 0;
SET LINESIZE 300;

-- header not needed
spool /home/usr/${N_Report}.csv

select * from tablename ;

spool off

commit;
quit;
!

Dir_N=/home/usr/${N_Report}.csv

(uuencode $Dir_N $N_Report.csv ; echo "N Report") | mail -s "N report" -t xyz@xyz.com

please post your comments if any one needs more clarity for the above.