ORACLE_自動發郵件程式碼
該程式指令碼最主要的功能實現為透過oracle自帶的過程包傳送郵件來監控ETL的執行情況:
ORACLE_SID=orcl
ORACLE_BASE=/opt/oracle
ORACLE_HOME=/opt/oracle/product/10.2.0
export ORACLE_SID ORACLE_BASE ORACLE_HOME
PWD_DIR=/home/oracle/shell
SQLPLUS=${ORACLE_HOME}/bin/sqlplus
CONFIG_INI=${PWD_DIR}/ini/config.ini
while read gameuser
do
echo ${gameuser}
echo ${SQLPLUS}
cd ${PWD_DIR}
${SQLPLUS} ${gameuser} << !
@etl_monitor.sql;
/
exit;
!
done<${CONFIG_INI}
etl_monitor.sql指令碼為:
DECLARE
p_txt VARCHAR2 (4000);
p_txt_all VARCHAR2 (4000);
BEGIN
FOR r IN (SELECT job_name,
run_cnt,
table_name,
column_name
FROM etl_monitor_config_tab)
LOOP
-- Call the Etl Monitor function
p_txt :=
etl_monitor (r.job_name,
r.run_cnt,
r.table_name,
r.column_name);
p_txt_all := p_txt_all || CHR (13) || p_txt;
END LOOP;
-- Call the Send Mail function
procsendemail (p_txt_all,
'Etl Moniotr',
'xxx@163.com',
'xxxx@kingsoft.com',
'mail.kingsoft.com',
25,
1,
'xxxxxx',
'xxxxxx',
'',
'bit 7');
p_txt_all := '';
END;
create or replace function etl_monitor(job_name varchar2,
run_cnt int,
table_name varchar2,
column_name varchar2)
RETURN varchar2 IS
v_monitor_date date; --The monitor of the proc's date
v_job_name varchar2(130);
v_log_id number;
v_result1 char(1); --The status of the proc's result1
v_result2 char(1); --The status of the proc's result2
v_status_cnt int;
v_record_num int; --The number of the job run
v_result varchar2(4000);
v_sql varchar2(1000);
begin
v_monitor_date := trunc(sysdate);
v_job_name := job_name;
v_result1 := '0';
v_result2 := '0';
v_sql := 'select count(1) from ';
if run_cnt = 1 then
select log_id
into v_log_id
from user_scheduler_job_run_details
where job_name = v_job_name
and trunc(actual_start_date) = v_monitor_date;
else
select max(log_id)
into v_log_id
from user_scheduler_job_run_details
where job_name = v_job_name
and trunc(actual_start_date) = v_monitor_date;
end if;
select count(*)
into v_status_cnt
from user_scheduler_job_run_details
where log_id = v_log_id
and status = 'SUCCEEDED';
if v_status_cnt = 0 then
goto error1;
end if;
v_result1 := '1';
v_sql := v_sql || table_name || ' ' || 'where trunc(' || column_name ||
') =' || 'trunc(sysdate-1) and rownum=1';
execute immediate v_sql
into v_record_num;
if v_record_num > 0 then
v_result2 := '1';
else
v_status_cnt := 0;
goto error1;
end if;
if v_result1 = '1' and v_result2 = '1' then
v_result := SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') || '.' ||
v_job_name || ' At ' || v_monitor_date || ' IS SUCCEEDED';
end if;
<<error1>>
if v_status_cnt = 0 then
select OWNER || '.' || JOB_NAME || ' At ' || TRUNC(ACTUAL_START_DATE) ||
'IS ' ADDITIONAL_INFO
into v_result
from user_scheduler_job_run_details
where log_id = v_log_id;
end if;
return v_result;
exception
when others then
return SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') || '.' || v_job_name || ' At ' || v_monitor_date || ' IS NOT EXECUTE';
end;
最主要的procsendmail的過程為:
CREATE OR REPLACE PROCEDURE PROCSENDEMAIL (
P_TXT VARCHAR2,
P_SUB VARCHAR2,
P_SENDOR VARCHAR2,
P_RECEIVER VARCHAR2,
P_SERVER VARCHAR2,
P_PORT NUMBER DEFAULT 25 ,
P_NEED_SMTP INT DEFAULT 0 ,
P_USER VARCHAR2 DEFAULT NULL ,
P_PASS VARCHAR2 DEFAULT NULL ,
P_FILENAME VARCHAR2 DEFAULT NULL ,
P_ENCODE VARCHAR2 DEFAULT 'bit 7'
)
AUTHID CURRENT_USER
IS
/*
作用:用oracle傳送郵件
主要功能:1、支援多收件人。
2、支援中文
3、支援抄送人
4、支援大於32K的附件
5、支援多行正文
6、支援多附件
7、支援文字附件和二進位制附件
8、支援HTML格式
8、支援
作者:suk
引數說明:
p_txt :郵件正文
p_sub: 郵件標題
p_SendorAddress : 傳送人郵件地址
p_ReceiverAddress : 接收地址,可以同時傳送到多個地址上,地址之間用","或者";"隔開
p_EmailServer : 郵件伺服器地址,可以是域名或者IP
p_Port :郵件伺服器埠
p_need_smtp:是否需要smtp認證,0表示不需要,1表示需要
p_user:smtp驗證需要的使用者名稱
p_pass:smtp驗證需要的密碼
p_filename:附件名稱,必須包含完整的路徑,如"d:\temp\a.txt"。
可以有多個附件,附件名稱只見用逗號或者分號分隔
p_encode:附件編碼轉換格式,其中 p_encode='bit 7' 表示文字型別附件
p_encode='base64' 表示二進位制型別附件
注意:
1、對於文字型別的附件,不能用base64的方式傳送,否則出錯
2、對於多個附件只能用同一種格式傳送
*/
L_CRLF VARCHAR2 (2) := UTL_TCP.CRLF;
L_SENDORADDRESS VARCHAR2 (4000);
L_SPLITE VARCHAR2 (10) := '++';
BOUNDARY CONSTANT VARCHAR2 (256) := '-----BYSUK';
FIRST_BOUNDARY CONSTANT VARCHAR2 (256) := '--' || BOUNDARY || L_CRLF;
LAST_BOUNDARY CONSTANT VARCHAR2 (256)
:= '--' || BOUNDARY || '--' || L_CRLF ;
MULTIPART_MIME_TYPE CONSTANT VARCHAR2 (256)
:= 'multipart/mixed; boundary="' || BOUNDARY || '"' ;
/* 以下部分是傳送大二進位制附件時用到的變數 */
L_FIL BFILE;
L_FILE_LEN NUMBER;
L_MODULO NUMBER;
L_PIECES NUMBER;
L_FILE_HANDLE UTL_FILE.FILE_TYPE;
L_AMT BINARY_INTEGER := 672 * 3; /* ensures proper format; 2016 */
L_FILEPOS PLS_INTEGER := 1; /* pointer for the file */
L_CHUNKS NUMBER;
L_BUF RAW (2100);
L_DATA RAW (2100);
L_MAX_LINE_WIDTH NUMBER := 54;
L_DIRECTORY_BASE_NAME VARCHAR2 (100) := 'DIR_FOR_SEND_MAIL';
L_LINE VARCHAR2 (1000);
L_MESG VARCHAR2 (32767);
/* 以上部分是傳送大二進位制附件時用到的變數 */
TYPE ADDRESS_LIST
IS
TABLE OF VARCHAR2 (100)
INDEX BY BINARY_INTEGER;
MY_ADDRESS_LIST ADDRESS_LIST;
TYPE ACCT_LIST
IS
TABLE OF VARCHAR2 (100)
INDEX BY BINARY_INTEGER;
MY_ACCT_LIST ACCT_LIST;
-------------------------------------返回附件原始檔所在目錄或者名稱-----------------------
FUNCTION GET_FILE (P_FILE VARCHAR2, P_GET INT)
RETURN VARCHAR2
IS
--p_get=1 表示返回目錄
--p_get=2 表示返回檔名
L_FILE VARCHAR2 (1000);
BEGIN
IF INSTR (P_FILE, '\') > 0
THEN
--windows
IF P_GET = 1
THEN
L_FILE := SUBSTR (P_FILE, 1, INSTR (P_FILE, '\', -1) - 1);
ELSIF P_GET = 2
THEN
L_FILE :=
SUBSTR (P_FILE, - (LENGTH (P_FILE) - INSTR (P_FILE, '\', -1)));
END IF;
ELSIF INSTR (P_FILE, '/') > 0
THEN
--linux/unix
IF P_GET = 1
THEN
L_FILE := SUBSTR (P_FILE, 1, INSTR (P_FILE, '/', -1) - 1);
ELSIF P_GET = 2
THEN
L_FILE :=
SUBSTR (P_FILE, - (LENGTH (P_FILE) - INSTR (P_FILE, '/', -1)));
END IF;
END IF;
RETURN L_FILE;
END;
---------------------------------------------刪除directory------------------------------------
PROCEDURE DROP_DIRECTORY (P_DIRECTORY_NAME VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'drop directory ' || P_DIRECTORY_NAME;
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;
--------------------------------------------------建立directory-----------------------------------------
PROCEDURE CREATE_DIRECTORY (P_DIRECTORY_NAME VARCHAR2, P_DIR VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'create directory '
|| P_DIRECTORY_NAME
|| ' as '''
|| P_DIR
|| '''';
EXECUTE IMMEDIATE 'grant read,write on directory '
|| P_DIRECTORY_NAME
|| ' to public';
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
--------------------------------------------分割郵件地址或者附件地址--------------------
PROCEDURE P_SPLITE_STR (P_STR VARCHAR2, P_SPLITE_FLAG INT DEFAULT 1 )
IS
L_ADDR VARCHAR2 (254) := '';
L_LEN INT;
L_STR VARCHAR2 (4000);
J INT := 0; --表示郵件地址或者附件的個數
BEGIN
/*處理接收郵件地址列表,包括去空格、將;轉換為,等*/
L_STR :=
TRIM (RTRIM (REPLACE (REPLACE (P_STR, ';', ','), ' ', ''), ','));
L_LEN := LENGTH (L_STR);
FOR I IN 1 .. L_LEN
LOOP
IF SUBSTR (L_STR, I, 1) <> ','
THEN
L_ADDR := L_ADDR || SUBSTR (L_STR, I, 1);
ELSE
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN --表示處理郵件地址
--前後需要加上'<>',否則很多郵箱將不能傳送郵件
L_ADDR := '<' || L_ADDR || '>';
--呼叫郵件傳送過程
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN --表示處理附件名稱
MY_ACCT_LIST (J) := L_ADDR;
END IF;
L_ADDR := '';
END IF;
IF I = L_LEN
THEN
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN
--呼叫郵件傳送過程
L_ADDR := '<' || L_ADDR || '>';
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN
MY_ACCT_LIST (J) := L_ADDR;
END IF;
END IF;
END LOOP;
END;
------------------------------------------------寫郵件頭和郵件內容------------------------------------------
PROCEDURE WRITE_DATA (P_CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
P_NAME IN VARCHAR2,
P_VALUE IN VARCHAR2,
P_SPLITE VARCHAR2 DEFAULT ':' ,
P_CRLF VARCHAR2 DEFAULT L_CRLF )
IS
BEGIN
/* utl_raw.cast_to_raw 對解決中文亂碼問題很重要*/
UTL_SMTP.WRITE_RAW_DATA (
P_CONN,
UTL_RAW.CAST_TO_RAW (
CONVERT (P_NAME || P_SPLITE || P_VALUE || P_CRLF, 'ZHS16GBK')
)
);
END;
----------------------------------------寫MIME郵件尾部-----------------------------------------------------
PROCEDURE END_BOUNDARY (CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
LAST IN BOOLEAN DEFAULT FALSE )
IS
BEGIN
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
IF (LAST)
THEN
UTL_SMTP.WRITE_DATA (CONN, LAST_BOUNDARY);
END IF;
END;
----------------------------------------------傳送附件----------------------------------------------------
PROCEDURE ATTACHMENT (
CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
MIME_TYPE IN VARCHAR2 DEFAULT 'text/plain' ,
INLINE IN BOOLEAN DEFAULT TRUE ,
FILENAME IN VARCHAR2 DEFAULT 't.txt' ,
TRANSFER_ENC IN VARCHAR2 DEFAULT '7 bit' ,
DT_NAME IN VARCHAR2 DEFAULT '0'
)
IS
L_FILENAME VARCHAR2 (1000);
BEGIN
--寫附件頭
UTL_SMTP.WRITE_DATA (CONN, FIRST_BOUNDARY);
--設定附件格式
WRITE_DATA (CONN, 'Content-Type', MIME_TYPE);
--如果檔名稱非空,表示有附件
DROP_DIRECTORY (DT_NAME);
--建立directory
CREATE_DIRECTORY (DT_NAME, GET_FILE (FILENAME, 1));
--得到附件檔名稱
L_FILENAME := GET_FILE (FILENAME, 2);
IF (INLINE)
THEN
WRITE_DATA (CONN,
'Content-Disposition',
'inline; filename="' || L_FILENAME || '"');
ELSE
WRITE_DATA (CONN,
'Content-Disposition',
'attachment; filename="' || L_FILENAME || '"');
END IF;
--設定附件的轉換格式
IF (TRANSFER_ENC IS NOT NULL)
THEN
WRITE_DATA (CONN, 'Content-Transfer-Encoding', TRANSFER_ENC);
END IF;
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
--begin 貼附件內容
IF TRANSFER_ENC = 'bit 7'
THEN
--如果是文字型別的附件
BEGIN
L_FILE_HANDLE := UTL_FILE.FOPEN (DT_NAME, L_FILENAME, 'r'); --開啟檔案
--把附件分成多份,這樣可以傳送超過32K的附件
LOOP
UTL_FILE.GET_LINE (L_FILE_HANDLE, L_LINE);
L_MESG := L_LINE || L_CRLF;
WRITE_DATA (CONN,
'',
L_MESG,
'',
'');
END LOOP;
UTL_FILE.FCLOSE (L_FILE_HANDLE);
END_BOUNDARY (CONN);
EXCEPTION
WHEN OTHERS
THEN
UTL_FILE.FCLOSE (L_FILE_HANDLE);
END_BOUNDARY (CONN);
NULL;
END; --結束文字型別附件的處理
ELSIF TRANSFER_ENC = 'base64'
THEN
--如果是二進位制型別的附件
BEGIN
--把附件分成多份,這樣可以傳送超過32K的附件
L_FILEPOS := 1; --重置offset,在傳送多個附件時,必須重置
L_FIL := BFILENAME (DT_NAME, L_FILENAME);
L_FILE_LEN := DBMS_LOB.GETLENGTH (L_FIL);
L_MODULO := MOD (L_FILE_LEN, L_AMT);
L_PIECES := TRUNC (L_FILE_LEN / L_AMT);
IF (L_MODULO <> 0)
THEN
L_PIECES := L_PIECES + 1;
END IF;
DBMS_LOB.FILEOPEN (L_FIL, DBMS_LOB.FILE_READONLY);
DBMS_LOB.READ (L_FIL,
L_AMT,
L_FILEPOS,
L_BUF);
L_DATA := NULL;
FOR I IN 1 .. L_PIECES
LOOP
L_FILEPOS := I * L_AMT + 1;
L_FILE_LEN := L_FILE_LEN - L_AMT;
L_DATA := UTL_RAW.CONCAT (L_DATA, L_BUF);
L_CHUNKS := TRUNC (UTL_RAW.LENGTH (L_DATA) / L_MAX_LINE_WIDTH);
IF (I <> L_PIECES)
THEN
L_CHUNKS := L_CHUNKS - 1;
END IF;
UTL_SMTP.WRITE_RAW_DATA (CONN,
UTL_ENCODE.BASE64_ENCODE (L_DATA));
L_DATA := NULL;
IF (L_FILE_LEN < L_AMT AND L_FILE_LEN > 0)
THEN
L_AMT := L_FILE_LEN;
END IF;
DBMS_LOB.READ (L_FIL,
L_AMT,
L_FILEPOS,
L_BUF);
END LOOP;
DBMS_LOB.FILECLOSE (L_FIL);
END_BOUNDARY (CONN);
EXCEPTION
WHEN OTHERS
THEN
DBMS_LOB.FILECLOSE (L_FIL);
END_BOUNDARY (CONN);
RAISE;
END; --結束處理二進位制附件
END IF; --結束處理附件內容
DROP_DIRECTORY (DT_NAME);
END; --結束過程ATTACHMENT
---------------------------------------------真正傳送郵件的過程--------------------------------------------
PROCEDURE P_EMAIL (P_SENDORADDRESS2 VARCHAR2, --傳送地址
P_RECEIVERADDRESS2 VARCHAR2) --接受地址
IS
L_CONN UTL_SMTP.CONNECTION; --定義連線
BEGIN
/*初始化郵件伺服器資訊,連線郵件伺服器*/
L_CONN := UTL_SMTP.OPEN_CONNECTION (P_SERVER, P_PORT);
UTL_SMTP.HELO (L_CONN, P_SERVER);
/* smtp伺服器登入校驗 */
IF P_NEED_SMTP = 1
THEN
UTL_SMTP.COMMAND (L_CONN, 'AUTH LOGIN', '');
UTL_SMTP.COMMAND (
L_CONN,
UTL_RAW.CAST_TO_VARCHAR2 (
UTL_ENCODE.BASE64_ENCODE (UTL_RAW.CAST_TO_RAW (P_USER))
)
);
UTL_SMTP.COMMAND (
L_CONN,
UTL_RAW.CAST_TO_VARCHAR2 (
UTL_ENCODE.BASE64_ENCODE (UTL_RAW.CAST_TO_RAW (P_PASS))
)
);
END IF;
/*設定傳送地址和接收地址*/
UTL_SMTP.MAIL (L_CONN, P_SENDORADDRESS2);
UTL_SMTP.RCPT (L_CONN, P_RECEIVERADDRESS2);
/*設定郵件頭*/
UTL_SMTP.OPEN_DATA (L_CONN);
WRITE_DATA (L_CONN, 'Date', TO_CHAR (SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));
/*設定傳送人*/
WRITE_DATA (L_CONN, 'From', P_SENDOR);
/*設定接收人*/
WRITE_DATA (L_CONN, 'To', P_RECEIVER);
/*設定郵件主題*/
WRITE_DATA (L_CONN, 'Subject', P_SUB);
WRITE_DATA (L_CONN, 'Content-Type', MULTIPART_MIME_TYPE);
UTL_SMTP.WRITE_DATA (L_CONN, UTL_TCP.CRLF);
UTL_SMTP.WRITE_DATA (L_CONN, FIRST_BOUNDARY);
WRITE_DATA (L_CONN, 'Content-Type', 'text/plain;charset=gb2312');
--單獨空一行,否則,正文內容不顯示
UTL_SMTP.WRITE_DATA (L_CONN, UTL_TCP.CRLF);
/* 設定郵件正文
把分隔符還原成chr(10)。這主要是為了shell中呼叫該過程,如果有多行,則先把多行的內容合併成一行,並用 l_splite分隔
然後用 l_crlf替換chr(10)。這一步是必須的,否則將不能傳送郵件正文有多行的郵件
*/
WRITE_DATA (
L_CONN,
'',
REPLACE (REPLACE (P_TXT, L_SPLITE, CHR (10)), CHR (10), L_CRLF),
'',
''
);
END_BOUNDARY (L_CONN);
--如果檔名稱不為空,則傳送附件
IF (P_FILENAME IS NOT NULL)
THEN
--根據逗號或者分號拆分附件地址
P_SPLITE_STR (P_FILENAME, 2);
--迴圈傳送附件(在同一個郵件中)
FOR K IN 1 .. MY_ACCT_LIST.COUNT
LOOP
ATTACHMENT (
CONN => L_CONN,
FILENAME => MY_ACCT_LIST (K),
TRANSFER_ENC => P_ENCODE,
DT_NAME => L_DIRECTORY_BASE_NAME || TO_CHAR (K)
);
END LOOP;
END IF;
/*關閉資料寫入*/
UTL_SMTP.CLOSE_DATA (L_CONN);
/*關閉連線*/
UTL_SMTP.QUIT (L_CONN);
/*異常處理*/
EXCEPTION
WHEN OTHERS
THEN
NULL;
RAISE;
END;
---------------------------------------------------主過程-----------------------------------------------------
BEGIN
L_SENDORADDRESS := '<' || P_SENDOR || '>';
P_SPLITE_STR (P_RECEIVER); --處理郵件地址
FOR K IN 1 .. MY_ADDRESS_LIST.COUNT
LOOP
P_EMAIL (L_SENDORADDRESS, MY_ADDRESS_LIST (K));
END LOOP;
/*處理郵件地址,根據逗號分割郵件*/
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
ORACLE_SID=orcl
ORACLE_BASE=/opt/oracle
ORACLE_HOME=/opt/oracle/product/10.2.0
export ORACLE_SID ORACLE_BASE ORACLE_HOME
PWD_DIR=/home/oracle/shell
SQLPLUS=${ORACLE_HOME}/bin/sqlplus
CONFIG_INI=${PWD_DIR}/ini/config.ini
while read gameuser
do
echo ${gameuser}
echo ${SQLPLUS}
cd ${PWD_DIR}
${SQLPLUS} ${gameuser} << !
@etl_monitor.sql;
/
exit;
!
done<${CONFIG_INI}
etl_monitor.sql指令碼為:
DECLARE
p_txt VARCHAR2 (4000);
p_txt_all VARCHAR2 (4000);
BEGIN
FOR r IN (SELECT job_name,
run_cnt,
table_name,
column_name
FROM etl_monitor_config_tab)
LOOP
-- Call the Etl Monitor function
p_txt :=
etl_monitor (r.job_name,
r.run_cnt,
r.table_name,
r.column_name);
p_txt_all := p_txt_all || CHR (13) || p_txt;
END LOOP;
-- Call the Send Mail function
procsendemail (p_txt_all,
'Etl Moniotr',
'xxx@163.com',
'xxxx@kingsoft.com',
'mail.kingsoft.com',
25,
1,
'xxxxxx',
'xxxxxx',
'',
'bit 7');
p_txt_all := '';
END;
create or replace function etl_monitor(job_name varchar2,
run_cnt int,
table_name varchar2,
column_name varchar2)
RETURN varchar2 IS
v_monitor_date date; --The monitor of the proc's date
v_job_name varchar2(130);
v_log_id number;
v_result1 char(1); --The status of the proc's result1
v_result2 char(1); --The status of the proc's result2
v_status_cnt int;
v_record_num int; --The number of the job run
v_result varchar2(4000);
v_sql varchar2(1000);
begin
v_monitor_date := trunc(sysdate);
v_job_name := job_name;
v_result1 := '0';
v_result2 := '0';
v_sql := 'select count(1) from ';
if run_cnt = 1 then
select log_id
into v_log_id
from user_scheduler_job_run_details
where job_name = v_job_name
and trunc(actual_start_date) = v_monitor_date;
else
select max(log_id)
into v_log_id
from user_scheduler_job_run_details
where job_name = v_job_name
and trunc(actual_start_date) = v_monitor_date;
end if;
select count(*)
into v_status_cnt
from user_scheduler_job_run_details
where log_id = v_log_id
and status = 'SUCCEEDED';
if v_status_cnt = 0 then
goto error1;
end if;
v_result1 := '1';
v_sql := v_sql || table_name || ' ' || 'where trunc(' || column_name ||
') =' || 'trunc(sysdate-1) and rownum=1';
execute immediate v_sql
into v_record_num;
if v_record_num > 0 then
v_result2 := '1';
else
v_status_cnt := 0;
goto error1;
end if;
if v_result1 = '1' and v_result2 = '1' then
v_result := SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') || '.' ||
v_job_name || ' At ' || v_monitor_date || ' IS SUCCEEDED';
end if;
<<error1>>
if v_status_cnt = 0 then
select OWNER || '.' || JOB_NAME || ' At ' || TRUNC(ACTUAL_START_DATE) ||
'IS ' ADDITIONAL_INFO
into v_result
from user_scheduler_job_run_details
where log_id = v_log_id;
end if;
return v_result;
exception
when others then
return SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') || '.' || v_job_name || ' At ' || v_monitor_date || ' IS NOT EXECUTE';
end;
最主要的procsendmail的過程為:
CREATE OR REPLACE PROCEDURE PROCSENDEMAIL (
P_TXT VARCHAR2,
P_SUB VARCHAR2,
P_SENDOR VARCHAR2,
P_RECEIVER VARCHAR2,
P_SERVER VARCHAR2,
P_PORT NUMBER DEFAULT 25 ,
P_NEED_SMTP INT DEFAULT 0 ,
P_USER VARCHAR2 DEFAULT NULL ,
P_PASS VARCHAR2 DEFAULT NULL ,
P_FILENAME VARCHAR2 DEFAULT NULL ,
P_ENCODE VARCHAR2 DEFAULT 'bit 7'
)
AUTHID CURRENT_USER
IS
/*
作用:用oracle傳送郵件
主要功能:1、支援多收件人。
2、支援中文
3、支援抄送人
4、支援大於32K的附件
5、支援多行正文
6、支援多附件
7、支援文字附件和二進位制附件
8、支援HTML格式
8、支援
作者:suk
引數說明:
p_txt :郵件正文
p_sub: 郵件標題
p_SendorAddress : 傳送人郵件地址
p_ReceiverAddress : 接收地址,可以同時傳送到多個地址上,地址之間用","或者";"隔開
p_EmailServer : 郵件伺服器地址,可以是域名或者IP
p_Port :郵件伺服器埠
p_need_smtp:是否需要smtp認證,0表示不需要,1表示需要
p_user:smtp驗證需要的使用者名稱
p_pass:smtp驗證需要的密碼
p_filename:附件名稱,必須包含完整的路徑,如"d:\temp\a.txt"。
可以有多個附件,附件名稱只見用逗號或者分號分隔
p_encode:附件編碼轉換格式,其中 p_encode='bit 7' 表示文字型別附件
p_encode='base64' 表示二進位制型別附件
注意:
1、對於文字型別的附件,不能用base64的方式傳送,否則出錯
2、對於多個附件只能用同一種格式傳送
*/
L_CRLF VARCHAR2 (2) := UTL_TCP.CRLF;
L_SENDORADDRESS VARCHAR2 (4000);
L_SPLITE VARCHAR2 (10) := '++';
BOUNDARY CONSTANT VARCHAR2 (256) := '-----BYSUK';
FIRST_BOUNDARY CONSTANT VARCHAR2 (256) := '--' || BOUNDARY || L_CRLF;
LAST_BOUNDARY CONSTANT VARCHAR2 (256)
:= '--' || BOUNDARY || '--' || L_CRLF ;
MULTIPART_MIME_TYPE CONSTANT VARCHAR2 (256)
:= 'multipart/mixed; boundary="' || BOUNDARY || '"' ;
/* 以下部分是傳送大二進位制附件時用到的變數 */
L_FIL BFILE;
L_FILE_LEN NUMBER;
L_MODULO NUMBER;
L_PIECES NUMBER;
L_FILE_HANDLE UTL_FILE.FILE_TYPE;
L_AMT BINARY_INTEGER := 672 * 3; /* ensures proper format; 2016 */
L_FILEPOS PLS_INTEGER := 1; /* pointer for the file */
L_CHUNKS NUMBER;
L_BUF RAW (2100);
L_DATA RAW (2100);
L_MAX_LINE_WIDTH NUMBER := 54;
L_DIRECTORY_BASE_NAME VARCHAR2 (100) := 'DIR_FOR_SEND_MAIL';
L_LINE VARCHAR2 (1000);
L_MESG VARCHAR2 (32767);
/* 以上部分是傳送大二進位制附件時用到的變數 */
TYPE ADDRESS_LIST
IS
TABLE OF VARCHAR2 (100)
INDEX BY BINARY_INTEGER;
MY_ADDRESS_LIST ADDRESS_LIST;
TYPE ACCT_LIST
IS
TABLE OF VARCHAR2 (100)
INDEX BY BINARY_INTEGER;
MY_ACCT_LIST ACCT_LIST;
-------------------------------------返回附件原始檔所在目錄或者名稱-----------------------
FUNCTION GET_FILE (P_FILE VARCHAR2, P_GET INT)
RETURN VARCHAR2
IS
--p_get=1 表示返回目錄
--p_get=2 表示返回檔名
L_FILE VARCHAR2 (1000);
BEGIN
IF INSTR (P_FILE, '\') > 0
THEN
--windows
IF P_GET = 1
THEN
L_FILE := SUBSTR (P_FILE, 1, INSTR (P_FILE, '\', -1) - 1);
ELSIF P_GET = 2
THEN
L_FILE :=
SUBSTR (P_FILE, - (LENGTH (P_FILE) - INSTR (P_FILE, '\', -1)));
END IF;
ELSIF INSTR (P_FILE, '/') > 0
THEN
--linux/unix
IF P_GET = 1
THEN
L_FILE := SUBSTR (P_FILE, 1, INSTR (P_FILE, '/', -1) - 1);
ELSIF P_GET = 2
THEN
L_FILE :=
SUBSTR (P_FILE, - (LENGTH (P_FILE) - INSTR (P_FILE, '/', -1)));
END IF;
END IF;
RETURN L_FILE;
END;
---------------------------------------------刪除directory------------------------------------
PROCEDURE DROP_DIRECTORY (P_DIRECTORY_NAME VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'drop directory ' || P_DIRECTORY_NAME;
EXCEPTION
WHEN OTHERS
THEN
NULL;
END;
--------------------------------------------------建立directory-----------------------------------------
PROCEDURE CREATE_DIRECTORY (P_DIRECTORY_NAME VARCHAR2, P_DIR VARCHAR2)
IS
BEGIN
EXECUTE IMMEDIATE 'create directory '
|| P_DIRECTORY_NAME
|| ' as '''
|| P_DIR
|| '''';
EXECUTE IMMEDIATE 'grant read,write on directory '
|| P_DIRECTORY_NAME
|| ' to public';
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
--------------------------------------------分割郵件地址或者附件地址--------------------
PROCEDURE P_SPLITE_STR (P_STR VARCHAR2, P_SPLITE_FLAG INT DEFAULT 1 )
IS
L_ADDR VARCHAR2 (254) := '';
L_LEN INT;
L_STR VARCHAR2 (4000);
J INT := 0; --表示郵件地址或者附件的個數
BEGIN
/*處理接收郵件地址列表,包括去空格、將;轉換為,等*/
L_STR :=
TRIM (RTRIM (REPLACE (REPLACE (P_STR, ';', ','), ' ', ''), ','));
L_LEN := LENGTH (L_STR);
FOR I IN 1 .. L_LEN
LOOP
IF SUBSTR (L_STR, I, 1) <> ','
THEN
L_ADDR := L_ADDR || SUBSTR (L_STR, I, 1);
ELSE
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN --表示處理郵件地址
--前後需要加上'<>',否則很多郵箱將不能傳送郵件
L_ADDR := '<' || L_ADDR || '>';
--呼叫郵件傳送過程
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN --表示處理附件名稱
MY_ACCT_LIST (J) := L_ADDR;
END IF;
L_ADDR := '';
END IF;
IF I = L_LEN
THEN
J := J + 1;
IF P_SPLITE_FLAG = 1
THEN
--呼叫郵件傳送過程
L_ADDR := '<' || L_ADDR || '>';
MY_ADDRESS_LIST (J) := L_ADDR;
ELSIF P_SPLITE_FLAG = 2
THEN
MY_ACCT_LIST (J) := L_ADDR;
END IF;
END IF;
END LOOP;
END;
------------------------------------------------寫郵件頭和郵件內容------------------------------------------
PROCEDURE WRITE_DATA (P_CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
P_NAME IN VARCHAR2,
P_VALUE IN VARCHAR2,
P_SPLITE VARCHAR2 DEFAULT ':' ,
P_CRLF VARCHAR2 DEFAULT L_CRLF )
IS
BEGIN
/* utl_raw.cast_to_raw 對解決中文亂碼問題很重要*/
UTL_SMTP.WRITE_RAW_DATA (
P_CONN,
UTL_RAW.CAST_TO_RAW (
CONVERT (P_NAME || P_SPLITE || P_VALUE || P_CRLF, 'ZHS16GBK')
)
);
END;
----------------------------------------寫MIME郵件尾部-----------------------------------------------------
PROCEDURE END_BOUNDARY (CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
LAST IN BOOLEAN DEFAULT FALSE )
IS
BEGIN
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
IF (LAST)
THEN
UTL_SMTP.WRITE_DATA (CONN, LAST_BOUNDARY);
END IF;
END;
----------------------------------------------傳送附件----------------------------------------------------
PROCEDURE ATTACHMENT (
CONN IN OUT NOCOPY UTL_SMTP.CONNECTION,
MIME_TYPE IN VARCHAR2 DEFAULT 'text/plain' ,
INLINE IN BOOLEAN DEFAULT TRUE ,
FILENAME IN VARCHAR2 DEFAULT 't.txt' ,
TRANSFER_ENC IN VARCHAR2 DEFAULT '7 bit' ,
DT_NAME IN VARCHAR2 DEFAULT '0'
)
IS
L_FILENAME VARCHAR2 (1000);
BEGIN
--寫附件頭
UTL_SMTP.WRITE_DATA (CONN, FIRST_BOUNDARY);
--設定附件格式
WRITE_DATA (CONN, 'Content-Type', MIME_TYPE);
--如果檔名稱非空,表示有附件
DROP_DIRECTORY (DT_NAME);
--建立directory
CREATE_DIRECTORY (DT_NAME, GET_FILE (FILENAME, 1));
--得到附件檔名稱
L_FILENAME := GET_FILE (FILENAME, 2);
IF (INLINE)
THEN
WRITE_DATA (CONN,
'Content-Disposition',
'inline; filename="' || L_FILENAME || '"');
ELSE
WRITE_DATA (CONN,
'Content-Disposition',
'attachment; filename="' || L_FILENAME || '"');
END IF;
--設定附件的轉換格式
IF (TRANSFER_ENC IS NOT NULL)
THEN
WRITE_DATA (CONN, 'Content-Transfer-Encoding', TRANSFER_ENC);
END IF;
UTL_SMTP.WRITE_DATA (CONN, UTL_TCP.CRLF);
--begin 貼附件內容
IF TRANSFER_ENC = 'bit 7'
THEN
--如果是文字型別的附件
BEGIN
L_FILE_HANDLE := UTL_FILE.FOPEN (DT_NAME, L_FILENAME, 'r'); --開啟檔案
--把附件分成多份,這樣可以傳送超過32K的附件
LOOP
UTL_FILE.GET_LINE (L_FILE_HANDLE, L_LINE);
L_MESG := L_LINE || L_CRLF;
WRITE_DATA (CONN,
'',
L_MESG,
'',
'');
END LOOP;
UTL_FILE.FCLOSE (L_FILE_HANDLE);
END_BOUNDARY (CONN);
EXCEPTION
WHEN OTHERS
THEN
UTL_FILE.FCLOSE (L_FILE_HANDLE);
END_BOUNDARY (CONN);
NULL;
END; --結束文字型別附件的處理
ELSIF TRANSFER_ENC = 'base64'
THEN
--如果是二進位制型別的附件
BEGIN
--把附件分成多份,這樣可以傳送超過32K的附件
L_FILEPOS := 1; --重置offset,在傳送多個附件時,必須重置
L_FIL := BFILENAME (DT_NAME, L_FILENAME);
L_FILE_LEN := DBMS_LOB.GETLENGTH (L_FIL);
L_MODULO := MOD (L_FILE_LEN, L_AMT);
L_PIECES := TRUNC (L_FILE_LEN / L_AMT);
IF (L_MODULO <> 0)
THEN
L_PIECES := L_PIECES + 1;
END IF;
DBMS_LOB.FILEOPEN (L_FIL, DBMS_LOB.FILE_READONLY);
DBMS_LOB.READ (L_FIL,
L_AMT,
L_FILEPOS,
L_BUF);
L_DATA := NULL;
FOR I IN 1 .. L_PIECES
LOOP
L_FILEPOS := I * L_AMT + 1;
L_FILE_LEN := L_FILE_LEN - L_AMT;
L_DATA := UTL_RAW.CONCAT (L_DATA, L_BUF);
L_CHUNKS := TRUNC (UTL_RAW.LENGTH (L_DATA) / L_MAX_LINE_WIDTH);
IF (I <> L_PIECES)
THEN
L_CHUNKS := L_CHUNKS - 1;
END IF;
UTL_SMTP.WRITE_RAW_DATA (CONN,
UTL_ENCODE.BASE64_ENCODE (L_DATA));
L_DATA := NULL;
IF (L_FILE_LEN < L_AMT AND L_FILE_LEN > 0)
THEN
L_AMT := L_FILE_LEN;
END IF;
DBMS_LOB.READ (L_FIL,
L_AMT,
L_FILEPOS,
L_BUF);
END LOOP;
DBMS_LOB.FILECLOSE (L_FIL);
END_BOUNDARY (CONN);
EXCEPTION
WHEN OTHERS
THEN
DBMS_LOB.FILECLOSE (L_FIL);
END_BOUNDARY (CONN);
RAISE;
END; --結束處理二進位制附件
END IF; --結束處理附件內容
DROP_DIRECTORY (DT_NAME);
END; --結束過程ATTACHMENT
---------------------------------------------真正傳送郵件的過程--------------------------------------------
PROCEDURE P_EMAIL (P_SENDORADDRESS2 VARCHAR2, --傳送地址
P_RECEIVERADDRESS2 VARCHAR2) --接受地址
IS
L_CONN UTL_SMTP.CONNECTION; --定義連線
BEGIN
/*初始化郵件伺服器資訊,連線郵件伺服器*/
L_CONN := UTL_SMTP.OPEN_CONNECTION (P_SERVER, P_PORT);
UTL_SMTP.HELO (L_CONN, P_SERVER);
/* smtp伺服器登入校驗 */
IF P_NEED_SMTP = 1
THEN
UTL_SMTP.COMMAND (L_CONN, 'AUTH LOGIN', '');
UTL_SMTP.COMMAND (
L_CONN,
UTL_RAW.CAST_TO_VARCHAR2 (
UTL_ENCODE.BASE64_ENCODE (UTL_RAW.CAST_TO_RAW (P_USER))
)
);
UTL_SMTP.COMMAND (
L_CONN,
UTL_RAW.CAST_TO_VARCHAR2 (
UTL_ENCODE.BASE64_ENCODE (UTL_RAW.CAST_TO_RAW (P_PASS))
)
);
END IF;
/*設定傳送地址和接收地址*/
UTL_SMTP.MAIL (L_CONN, P_SENDORADDRESS2);
UTL_SMTP.RCPT (L_CONN, P_RECEIVERADDRESS2);
/*設定郵件頭*/
UTL_SMTP.OPEN_DATA (L_CONN);
WRITE_DATA (L_CONN, 'Date', TO_CHAR (SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));
/*設定傳送人*/
WRITE_DATA (L_CONN, 'From', P_SENDOR);
/*設定接收人*/
WRITE_DATA (L_CONN, 'To', P_RECEIVER);
/*設定郵件主題*/
WRITE_DATA (L_CONN, 'Subject', P_SUB);
WRITE_DATA (L_CONN, 'Content-Type', MULTIPART_MIME_TYPE);
UTL_SMTP.WRITE_DATA (L_CONN, UTL_TCP.CRLF);
UTL_SMTP.WRITE_DATA (L_CONN, FIRST_BOUNDARY);
WRITE_DATA (L_CONN, 'Content-Type', 'text/plain;charset=gb2312');
--單獨空一行,否則,正文內容不顯示
UTL_SMTP.WRITE_DATA (L_CONN, UTL_TCP.CRLF);
/* 設定郵件正文
把分隔符還原成chr(10)。這主要是為了shell中呼叫該過程,如果有多行,則先把多行的內容合併成一行,並用 l_splite分隔
然後用 l_crlf替換chr(10)。這一步是必須的,否則將不能傳送郵件正文有多行的郵件
*/
WRITE_DATA (
L_CONN,
'',
REPLACE (REPLACE (P_TXT, L_SPLITE, CHR (10)), CHR (10), L_CRLF),
'',
''
);
END_BOUNDARY (L_CONN);
--如果檔名稱不為空,則傳送附件
IF (P_FILENAME IS NOT NULL)
THEN
--根據逗號或者分號拆分附件地址
P_SPLITE_STR (P_FILENAME, 2);
--迴圈傳送附件(在同一個郵件中)
FOR K IN 1 .. MY_ACCT_LIST.COUNT
LOOP
ATTACHMENT (
CONN => L_CONN,
FILENAME => MY_ACCT_LIST (K),
TRANSFER_ENC => P_ENCODE,
DT_NAME => L_DIRECTORY_BASE_NAME || TO_CHAR (K)
);
END LOOP;
END IF;
/*關閉資料寫入*/
UTL_SMTP.CLOSE_DATA (L_CONN);
/*關閉連線*/
UTL_SMTP.QUIT (L_CONN);
/*異常處理*/
EXCEPTION
WHEN OTHERS
THEN
NULL;
RAISE;
END;
---------------------------------------------------主過程-----------------------------------------------------
BEGIN
L_SENDORADDRESS := '<' || P_SENDOR || '>';
P_SPLITE_STR (P_RECEIVER); --處理郵件地址
FOR K IN 1 .. MY_ADDRESS_LIST.COUNT
LOOP
P_EMAIL (L_SENDORADDRESS, MY_ADDRESS_LIST (K));
END LOOP;
/*處理郵件地址,根據逗號分割郵件*/
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-1969354/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python自動發郵件Python
- C#實現自動發郵件C#
- EM配置Oracle郵件自動告警Oracle
- Springboot 自動傳送郵件Spring Boot
- 郵件開發:傳送程式
- oracle資料庫自動發郵件實現報警功能Oracle資料庫
- 禪道開源版本12.4.1配置SMTP自動發郵件
- 郵件開發:接收解析郵件
- 乾貨丨RPA郵件自動化技巧
- linux下設定郵件自動傳送Linux
- 自動監控Oracle 表空間資訊併傳送郵件指令碼Oracle指令碼
- 群發郵件
- shell發郵件
- oracle 發郵件Oracle
- Focussend:營銷自動化下的八大觸發類郵件
- redmine建立新聞,自動發郵件給專案組所有成員
- iphone 6s自帶郵箱收發郵件設定教程iPhone
- 關於作業自動排程失敗後自動發郵件給操作員的問題
- 郵件營銷自動化:批次郵箱API傳送技巧與策略API
- win10系統下outlook收到郵件後自動刪除郵件怎麼解決Win10
- 用 Python 指令碼實現電腦喚醒後自動拍照 截圖併發郵件通知Python指令碼
- c# 傳送郵件程式碼,帶附件C#
- 用Oracle發郵件Oracle
- 發郵件的例子
- linux 發郵件Linux
- 郵件開發:複雜郵件的一個示例
- nodejs什麼值得買自動簽到自動評論郵件傳送NodeJS
- win10系統自帶郵箱發不出去郵件怎麼辦_win10郵箱發不出去郵件解決方案Win10
- ASP.NET自動傳送郵件功能的實現ASP.NET
- 自動生成addm報告並以郵件傳送
- 自動生成AWR報告並以郵件附件傳送
- 洩露至少5億資訊 雅虎已暫禁電子郵件自動轉發
- win10系統中自帶郵件如何設定接收網易郵箱郵件Win10
- oracle_備份指令碼Oracle指令碼
- python 發個郵件Python
- 郵件開發:DNS、JDNIDNS
- Oracle 發郵件過程Oracle
- 轉發郵件附件丟了