Linux | Send mail using internal mail command

November 16, 2016

Hi guys

We are on VEEM+VMWare infrastructure from a while, yet I am paranoid to maintain copies of the backups on different media once after going through couple of nightmares. We take weekly cold backup for our ERP Production server, move the tar files to a standby Linux server, and move those backups once again to an external HDD.

So basically I have a full VM backed up, the same VM holds a weekly cold backup, standby Linux server holding a copy of the cold backup files & to finish it, again copied to an external HDD. The funniest part is, we are moving the entire VMs to a TANDBERG Quick Station as well!

Though everything works fine till date, the last part of the deal needs to intimate me about successful completion of copying the tar files to the external media, ie, HDD that is formatted using NTFS, so that I can use it on both Linux and Windows environments

Be warned: The below bash script only works in an environment that has an internal SMTP server (or I don’t know how to relay the messages through an external SMTP relay and to disappoint you further, I don’t care about relaying through external SMTP). In addition, you must be on Linux 6 and above to use the internal mail command as demonstrated below.¬†Linux 5 doesn’t support many switches provided with the example.

Further, below example demonstrates the basic level of error capturing with “bash” scripts as well

/bin/cp -rf /u02/backup/PROD_DAILY_BACKUP*.* /media/Elements/ 2> /dev/null

if [ $? -eq 0 ]
echo "The files were successfully copied to external hard disk" | mailx -v -s "ERP Tar Files Moved to External HDD | Success" -S smtp=smtp:// -S from="ERP Alerts <>",
echo "Files were not copied to external HDD" | mailx -v -s "ERP Tar Files to External HDD | Failed" -S smtp=smtp:// -S from="ERP Alerts <>",

Try it and let e know whether it worked for you :)



Oracle send mail using UTL_SMTP (to multiple parties Cc)

October 17, 2012

v_From      VARCHAR2(80) := '';
v_Recipient VARCHAR2(80) := '';
v_Subject   VARCHAR2(80) := 'Checking Cc';
v_Mail_Host VARCHAR2(50) := '';
v_Mail_Conn utl_smtp.Connection;

name_array   DBMS_SQL.varchar2_table;

crlf        VARCHAR2(2)  := chr(13)||chr(10);
CC_parties varchar2(2000);


--populate from table-Cc parties
name_array(1) :=  '';
name_array(2) :=  '';
name_array(3) :=  '';

v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);

utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);
FOR i IN name_array.FIRST .. name_array.LAST
CC_parties := CC_parties||';'|| name_array(i);
utl_smtp.Rcpt(v_Mail_Conn, name_array(i));

'Date: '   || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
'From: '   || v_From || crlf ||
'Subject: '|| v_Subject || crlf ||
'To: '     || v_Recipient || crlf ||
'Cc: '     || CC_parties|| crlf ||
'Content-Type: text/html;' ||crlf ||
'New Mail from Procedure');


(Edited Image)

Hope you enjoy this.