how to export data from log table to email body in oracle

后端 未结 2 1424
误落风尘
误落风尘 2020-11-27 23:25

is there a way in oracle how to export data from table to email? The thing is, I have a log table, where I keep failed logs. I would like to have a procedure which check if

2条回答
  •  执念已碎
    2020-11-27 23:54

    You can generate an HTML script and add it in mail. Like if you want a tabular format, you can create the HTML script and attach it in mail.

    Take the following for refference

    DECLARE
       p_message_body   CLOB                := EMPTY_CLOB ();
       p_smtp_host      VARCHAR2 (20)       := ;
       p_smtp_port      VARCHAR2 (10)       := '25';
       p_message_type   VARCHAR2 (100)      := ' text/html';
       crlf             VARCHAR2 (2)        := UTL_TCP.crlf;
       ls_dt_start      VARCHAR2 (50);
       ls_dt_end        VARCHAR2 (50);
       l_mail_conn      UTL_SMTP.connection;
       pf_to_name       VARCHAR2 (100);
    BEGIN
       BEGIN
          SELECT TO_CHAR (SYSDATE - 1, 'HH12:MI:SS AM'),
                 TO_CHAR (SYSDATE + 1, 'HH12:MI:SS AM')
            INTO ls_dt_start,
                 ls_dt_end
            FROM DUAL;
       EXCEPTION
          WHEN OTHERS
          THEN
             NULL;
       END;
    
       p_message_body :=
          '    ';
       p_message_body :=
             p_message_body
          || ' 

    Hello ,


    Dummy Message. Find Table Below '; p_message_body := p_message_body || '

    Start Time End Time
    ' || ls_dt_start || ' ' || ls_dt_end || '
    '; l_mail_conn := UTL_SMTP.open_connection (p_smtp_host, p_smtp_port); UTL_SMTP.helo (l_mail_conn, p_smtp_host); UTL_SMTP.mail (l_mail_conn, 'TEST@ORACLE.COM'); pf_to_name := 'abc@xyz.com'; UTL_SMTP.rcpt (l_mail_conn, 'abc@xyz.com'); UTL_SMTP.open_data (l_mail_conn); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('To: ' || pf_to_name || crlf) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ( 'Date: ' || TO_CHAR (SYSDATE, 'Dy, DD Mon YYYY hh24:mi:ss' ) || crlf ) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ( 'From: ' || 'TEST@ORACLE.COM' || crlf ) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ( 'Subject: ' || 'Test {' || TO_CHAR (SYSDATE, 'DD Mon YYYY' ) || '}' || crlf ) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('MIME-Version: 1.0' || crlf) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ( 'Content-Type: multipart/mixed; boundary="SECBOUND"' || crlf || crlf ) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('--SECBOUND' || crlf) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ( 'Content-Type: ' || p_message_type || crlf || crlf ) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw (p_message_body || crlf) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('--SECBOUND' || crlf) ); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('--SECBOUND' || crlf) ); --Defining content type as attachment and specifying the filename. UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('' || crlf)); UTL_SMTP.write_raw_data (l_mail_conn, UTL_RAW.cast_to_raw ('--SECBOUND' || crlf) ); --Close connection and send mail. UTL_SMTP.close_data (l_mail_conn); UTL_SMTP.quit (l_mail_conn); EXCEPTION WHEN OTHERS THEN NULL; END;

    You can substitute the data you want with your table.

提交回复
热议问题