【主機】sendEmail傳送帶有附件的郵件

楊奇龍發表於2011-08-01
需求:
    從資料庫獲取資料並儲存為CSV的檔案,並把資料檔案透過sendEmail以附件的形式傳送到指定郵箱。
    這裡主要利用sendEmail的: -m 郵件內容  -a 郵件附件 這兩個引數來完成此工作。
shell 指令碼:
#!/bin/sh
#function: get data from database and keep them in csv mode 
#then send the log and the csv file to var $TO
#author: yang qilong
#date: 2011 08 01

TODAY=`date +"%Y_%m_%d_%H%M%S"`
DIR='YANGDIR_TMP'
FILENAME=IM_REPORT_ERRINFO_$TODAY.csv
SQLCMD='select * from YANG.ERRINFO WHERE rownum <12'
LOGFILE=/tmp/oracle/get_im_report_errinfo.log
FROM=qilong.yangql@yangql.com
TO=yangqilong@163.com,yangql@163.com --為了傳送至多人

sqlplus yang/yang  1>$LOGFILE  2>&1 <
select sysdate from dual;
exec sys.csv.generate('$DIR','$FILENAME','$SQLCMD');
exit;
EOF!

attach_file_list=/tmp/oracle/$FILENAME

/home/oracle/admin/bin/sendEmail -u "`whoami`@`hostname`:$attach_file_list" \
 -m  --以執行過程日誌為檔案的內容
 -f $FROM -s 172.18.100.3:25 -t $TO \
 -a $attach_file_list  -傳送的附件
指令碼輸出:
oracle@rac1:rac1 /tmp/oracle>./test.sh  
SQL*Plus: Release 11.2.0.1.0 Production on Mon Aug 1 21:10:49 2011
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
yang@rac1>
SYSDATE
-----------------------------
Aug 01 2011 21:10:49
yang@rac1>
PL/SQL procedure successfully completed.
yang@rac1>Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
Reading message body from STDIN because the '-m' option was not used.
If you are manually typing in a message:
  - First line must be received within 60 seconds.
  - End manual input with a CTRL-D on its own line.
Aug 01 21:10:49 rac1 sendEmail[12684]: Message input complete.
Aug 01 21:10:49 rac1 sendEmail[12684]: Email was sent successfully!
oracle@rac1:rac1 /tmp/oracle>

附上此過程遇到的問題:
主要在 -m 的使用上,將執行結果重定向到 -m 的輸入內容。但是中間有註釋,寫法如下:
/home/oracle/admin/bin/sendEmail -u "`whoami`@`hostname`:$attach_file_list" \
 -m "`whoami`@`hostname`:$attach_file_list" < /tmp/oracle/sql.log \
 #-m  
 -f qilong.yang@yangql.com -s 10.18.100.3:25 -t yangql@163..com \
 -a $attach_file_list
輸出:
Aug 01 21:10:03 rac1 sendEmail[12628]: ERROR => You must specify a 'from' field!  Try --help.
./test.sh: line 22: -f: command not found
1 經過測試,發現sendEmail 語句中不能帶#註釋。
2 當格式為:
-m "`whoami`@`hostname`:$attach_file_list" < /tmp/oracle/sql.log 
此方式 -m 傳送的郵件的內容是:
root@localhost.localdomain:/tmp/IM_REPORT_ERRINFO_2011_08_01_182007.csv  而不是我們想要的從資料庫取資料的過程日誌。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-703772/,如需轉載,請註明出處,否則將追究法律責任。

相關文章