Oracle定時email通知

來源:互聯網
上載者:User

標籤:for   tor   sub   stat   notice   creat   status   ace   pre   


small_program_task 這張表的資料是待發送的email通知,再次之前已經有一個job會定時掃描固定時間內未接收到小程式回報狀態將其寫入到該表,send_flag為N,表示為寄過通知。
email_notice_expired這個procedure負責寄出通訊,簡單的HTML也直接寫在procedure裡面。
/*EMAIL通知procedure email_notice_expiredscan small_program_task表,將send_flag標記為N的寄出去。written by milo 20170309*/CREATE OR REPLACE PROCEDURE email_notice_expired AS  v_email_recipient_string varchar(1000);  v_email_html_content     varchar2(32767);  v_expired_minutes        number;  v_last_active_time       date;  v_station                varchar(200);  v_program                varchar(200);  v_timeout                number;  v_task_id                varchar2(36);  CURSOR c_task_table IS    SELECT t1.station,           t1.program_id,           t1.last_active_time,           t1.program_timeout,           t1.expired_time,           t2.mail_for_error,           t1.job_no      FROM small_program_task t1      left join TK_CUST_PROG_M t2        ON t1.station = t2.custom_no     WHERE t1.send_flag = ‘N‘;BEGIN  OPEN c_task_table;  LOOP    FETCH c_task_table      INTO v_station,           v_program,           v_last_active_time,           v_timeout,           v_expired_minutes,           v_email_recipient_string,           v_task_id;    EXIT WHEN c_task_table%NOTFOUND;      --null or empty string    IF (v_email_recipient_string IS NULL OR v_email_recipient_string = ‘ ‘) THEN      CONTINUE;    END IF;    --dbms_output.put_line(‘v_email_recipient_string: ‘ || v_email_recipient_string);      v_email_html_content := ‘<html><head>    <title></title></head><body><p> Hi ‘ || v_station || ‘,</p><p> This email is to notify you that the following program was not sent running     status to server. Please check it ASAP.</p><p> <Detail></p><p> ------------------------------------------------------------------------------</p><p> <b>Program Name | Last Active Time | Duration(Minutes)</b></p><p> ‘ || v_program || ‘   |   ‘ ||                            to_char(v_last_active_time,                                    ‘dd-mm-yyyy hh24:mi‘) || ‘   |   ‘ ||                            v_expired_minutes || ‘</p><p> ------------------------------------------------------------------------------</p></body></html>‘;      --call procedure to send email.    send_mail(p_to        => v_email_recipient_string,              p_from      => ‘[email protected]***.com‘,              p_subject   => ‘Program Status Notice(‘ || v_program || ‘)‘,              p_text_msg  => ‘This is a test message.‘,              p_html_msg  => v_email_html_content,              p_smtp_host => ‘spam.***.com‘,              p_account   => ‘[email protected]***.com‘,              p_password  => ‘***‘);    -- set send_flag ‘Y‘ that an email has been sent.    update small_program_task set send_flag = ‘Y‘ where job_no = v_task_id;    commit;  END LOOP;  CLOSE c_task_table;END;

 

可直接先測試以上的procedure是否可以成功寄信出去。

--測試procedure email_notice_expiredbegin  email_notice_expired;end;

 

寫完了procedure,之後就是設置一個oracle排程去定時call這個procedure寄信就可以了。

/*call the stored procedure get_expired_program every 5 minutes.written by milo on 20170309*/begin  sys.dbms_scheduler.create_job(job_name            => ‘PLOEC.EMAIL_NOTICE_EXPIRED_TASK‘,                                job_type            => ‘STORED_PROCEDURE‘,                                job_action          => ‘email_notice_expired‘,                                start_date          => to_date(‘06-03-2017 00:00:00‘, ‘dd-mm-yyyy hh24:mi:ss‘),                                repeat_interval     => ‘Freq=Minutely;Interval=10‘,                                end_date            => to_date(‘07-03-2049 00:00:00‘, ‘dd-mm-yyyy hh24:mi:ss‘),                                job_class           => ‘DBMS_JOB$‘,                                enabled             => true,                                auto_drop           => false,                                comments            => ‘每10分鐘掃呼叫get_expired_program一次‘);end;/

 

可用以下SQL statement查看schedule的下次執行時間等。

--查看當前的scheduleSELECT * FROM dba_scheduler_jobs WHERE job_name = ‘EMAIL_NOTICE_EXPIRED_TASK‘;

 

Oracle定時email通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.