Oracle触发器发送邮件 一、创建触发器tr_yw_info_beforeinsert 二、创建触发器tr_yw_info_beforeupdate 三、发送邮件存过:send_mail()
一、创建触发器tr_yw_info_beforeinsert
CREATE OR REPLACE TRIGGER tr_yw_info_beforeinsertBEFORE INSERT ON yw_infoFOR EACH ROW
DECLARE v_content VARCHAR2( 4000 ) ; v_fssj VARCHAR2( 50 ) ; v_clsj VARCHAR2( 50 ) ; v_date VARCHAR2( 20 ) ; v_title VARCHAR2( 1000 ) ; v_flxstr VARCHAR2( 50 ) ; v_gzlxstr VARCHAR2( 50 ) ; v_tzfsstr VARCHAR2( 50 ) ; v_cjrstr VARCHAR2( 50 ) ; v_cldwstr VARCHAR2( 50 ) ; v_wcqkstr VARCHAR2( 50 ) ;
BEGIN SELECT 'YW' || seq_yw_info_info_id. nextval INTO :new. info_id FROM dual; SELECT to_char( :new. fs_time, 'yyyy-mm-dd hh24:mi:ss' ) INTO v_fssjFROM dual; SELECT to_char( :new. cl_time, 'yyyy-mm-dd hh24:mi:ss' ) INTO v_clsjFROM dual; SELECT to_char( SYSDATE, 'yyyymmdd-hh24miss' ) INTO v_dateFROM dual; SELECT ( CASE :new. info_type_idWHEN '40070' THEN ( SELECT a. a01FROM yw_code aWHERE a. code_type = '1001' AND rownum = 1 ) WHEN '40071' THEN ( SELECT a. a01FROM yw_code aWHERE a. code_type = '1005' AND rownum = 1 ) END ) INTO v_flxstrFROM dual; SELECT ( CASE :new. info_type_idWHEN '40070' THEN ( SELECT a. code_descFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1001' AND a. code_value = :new. gzlx_id) WHEN '40071' THEN ( SELECT a. code_descFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1005' AND a. code_value = :new. gzlx_id) END ) INTO v_gzlxstrFROM dual; SELECT a. code_descINTO v_tzfsstrFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1002' AND a. code_value = :new. tzfs; SELECT b. desc_nameINTO v_cjrstrFROM iu_user bWHERE b. user_id = :new. cjr; SELECT a. code_descINTO v_cldwstrFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1003' AND a. code_value = :new. cldw; SELECT a. code_descINTO v_wcqkstrFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1004' AND a. code_value = :new. wcqk; v_title := '【运维管理平台】-' || v_date || '-' || v_cjrstr || '-新增信息' ; v_content := '【运维信息】新增,时间:' || to_char( SYSDATE, 'yyyy-mm-dd hh24:mi:ss' ) || '<br>' || CHR( 13 ) || CHR( 10 ) || '信息标识:' || :new. info_id|| '<br>' || CHR( 13 ) || CHR( 10 ) || '信息类别:' || v_flxStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '信息类型:' || v_gzlxStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '通知方式:' || v_tzfsStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '创建用户:' || v_cjrStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '发生时间:' || v_fssj|| '<br>' || CHR( 13 ) || CHR( 10 ) || '报障单位:' || :new. bzdw|| '<br>' || CHR( 13 ) || CHR( 10 ) || '报障人员:' || :new. bzr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '详情描述:' || :new. xqms|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理时间:' || v_clsj|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理单位:' || v_cldwStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理人员:' || :new. clr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理情况:' || :new. clqk|| '<br>' || CHR( 13 ) || CHR( 10 ) || '完成情况:' || v_wcqkStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '备注信息:' || :new. bz; send_mail( 'tttzzzqqq@qq.com' , v_title, v_content) ; send_mail( '52487400@qq.com' , v_title, v_content) ;
END ;
二、创建触发器tr_yw_info_beforeupdate
CREATE OR REPLACE TRIGGER tr_yw_info_beforeupdateBEFORE UPDATE ON yw_infoFOR EACH ROW
DECLARE v_content VARCHAR2( 4000 ) ; v_fssj VARCHAR2( 50 ) ; v_clsj VARCHAR2( 50 ) ; v_date VARCHAR2( 20 ) ; v_title VARCHAR2( 1000 ) ; v_flxstr VARCHAR2( 50 ) ; v_gzlxstr VARCHAR2( 50 ) ; v_tzfsstr VARCHAR2( 50 ) ; v_cjrstr VARCHAR2( 50 ) ; v_cldwstr VARCHAR2( 50 ) ; v_wcqkstr VARCHAR2( 50 ) ; v_flag VARCHAR2( 20 ) ;
BEGIN SELECT 'YW' || seq_yw_info_info_id. nextval INTO :new. info_id FROM dual; SELECT to_char( :new. fs_time, 'yyyy-mm-dd hh24:mi:ss' ) INTO v_fssjFROM dual; SELECT to_char( :new. cl_time, 'yyyy-mm-dd hh24:mi:ss' ) INTO v_clsjFROM dual; SELECT to_char( SYSDATE, 'yyyymmdd-hh24miss' ) INTO v_dateFROM dual; SELECT ( CASE :new. info_type_idWHEN '40070' THEN ( SELECT a. a01FROM yw_code aWHERE a. code_type = '1001' AND rownum = 1 ) WHEN '40071' THEN ( SELECT a. a01FROM yw_code aWHERE a. code_type = '1005' AND rownum = 1 ) END ) INTO v_flxstrFROM dual; SELECT ( CASE :new. info_type_idWHEN '40070' THEN ( SELECT a. code_descFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1001' AND a. code_value = :new. gzlx_id) WHEN '40071' THEN ( SELECT a. code_descFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1005' AND a. code_value = :new. gzlx_id) END ) INTO v_gzlxstrFROM dual; SELECT a. code_descINTO v_tzfsstrFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1002' AND a. code_value = :new. tzfs; SELECT b. desc_nameINTO v_cjrstrFROM iu_user bWHERE b. user_id = :new. cjr; SELECT a. code_descINTO v_cldwstrFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1003' AND a. code_value = :new. cldw; SELECT a. code_descINTO v_wcqkstrFROM yw_code aWHERE a. del_flag = '0' AND a. code_type = '1004' AND a. code_value = :new. wcqk; IF :new. del_flag = '1' THEN v_title := '【运维管理平台】-' || v_date || '-' || v_cjrstr || '-删除信息' ; v_flag := '删除' ; ELSE v_title := '【运维管理平台】-' || v_date || '-' || v_cjrstr || '-更新信息' ; v_flag := '更新' ; END IF ; v_content := '【运维信息】' || v_flag || ',时间:' || to_char( SYSDATE, 'yyyy-mm-dd hh24:mi:ss' ) || '<br>' || CHR( 13 ) || CHR( 10 ) || '信息标识:' || :new. info_id|| '<br>' || CHR( 13 ) || CHR( 10 ) || '信息类别:' || v_flxStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '信息类型:' || v_gzlxStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '通知方式:' || v_tzfsStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '创建用户:' || v_cjrStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '发生时间:' || v_fssj|| '<br>' || CHR( 13 ) || CHR( 10 ) || '报障单位:' || :new. bzdw|| '<br>' || CHR( 13 ) || CHR( 10 ) || '报障人员:' || :new. bzr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '详情描述:' || :new. xqms|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理时间:' || v_clsj|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理单位:' || v_cldwStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理人员:' || :new. clr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '处理情况:' || :new. clqk|| '<br>' || CHR( 13 ) || CHR( 10 ) || '完成情况:' || v_wcqkStr|| '<br>' || CHR( 13 ) || CHR( 10 ) || '备注信息:' || :new. bz; send_mail( 'tttzzzqqq@qq.com' , v_title, v_content) ;
END ;
三、发送邮件存过:send_mail()
CREATE OR REPLACE PROCEDURE send_mail( p_recipient VARCHAR2, p_subject VARCHAR2, p_message VARCHAR2 ) IS v_mailhost VARCHAR2( 30 ) := 'smtp.163.com' ; v_user VARCHAR2( 30 ) := 'tttzzzqqq2019@163.com' ; v_pass VARCHAR2( 20 ) := 'dttzq84' ; v_sender VARCHAR2( 50 ) := 'tttzzzqqq2019@163.com' ; v_conn utl_smtp. connection; v_msg VARCHAR2( 4000 ) ;
BEGIN v_conn := utl_smtp. open_connection( v_mailhost, 25 ) ; utl_smtp. helo( v_conn, v_mailhost) ; utl_smtp. command( v_conn, 'AUTH LOGIN' ) ; utl_smtp. command( v_conn, utl_raw. cast_to_varchar2( utl_encode. base64_encode( utl_raw. cast_to_raw( v_user) ) ) ) ; utl_smtp. command( v_conn, utl_raw. cast_to_varchar2( utl_encode. base64_encode( utl_raw. cast_to_raw( v_pass) ) ) ) ; utl_smtp. mail( v_conn, v_sender) ; utl_smtp. rcpt( v_conn, p_recipient) ; v_msg := 'Date:' || to_char( SYSDATE, 'yyyy-mm-dd hh24:mi:ss' ) || utl_tcp. crlf || 'From: ' || '<' || v_sender || '>' || utl_tcp. crlf || 'To: ' || '<' || p_recipient || '>' || utl_tcp. crlf || 'Subject: ' || p_subject || utl_tcp. crlf || 'Content-Type:text/html;charset=GBK' || utl_tcp. crlf || utl_tcp. crlf || p_message; dbms_output. put_line( '------BEGIN------SEND_MAIL------' ) ; dbms_output. put_line( '邮件发送成功!' ) ; dbms_output. put_line( '报文信息:' || v_msg) ; dbms_output. put_line( '------E N D------SEND_MAIL------' || chr( 13 ) || chr( 10 ) ) ; utl_smtp. open_data( v_conn) ; utl_smtp. write_raw_data( v_conn, utl_raw. cast_to_raw( convert ( v_msg, 'ZHS16GBK' ) ) ) ; utl_smtp. close_data( v_conn) ; utl_smtp. quit( v_conn) ;
EXCEPTIONWHEN OTHERS THEN dbms_output. put_line( '邮件发送失败!' ) ; dbms_output. put_line( dbms_utility. format_error_stack) ; dbms_output. put_line( dbms_utility. format_call_stack) ;
END send_mail;