Rabu, 05 Juni 2013

Beberapa hal yang harus diperhatikan saat memakai apex_mail

Salah satu API yang disediakan oleh APEX adalah send email melalui APEX_MAIL. Dari contoh API kita bisa mengirimkan email baik text atau format html. Contoh :
DECLARE
 l_body CLOB;
 l_body_html CLOB;
BEGIN
 l_body := 'To view the content of this message, please use an HTML enabled mail client.'||utl_tcp.crlf;
 l_body_html := '<html>
                 <head>
                 <style type="text/css">
                 body{font-family: Arial, Helvetica, sans-serif;
                 font-size:10pt;
                 margin:30px;
                 background-color:#ffffff;}
                 span.sig{font-style:italic;
                 font-weight:bold;
                 color:#811919;}
                 </style>
                 </head>
                 <body>'||utl_tcp.crlf;
 l_body_html := l_body_html ||'<p>Thank you for your interest in the <strong>APEX_MAIL</strong> package.</p>'||utl_tcp.crlf;
 l_body_html := l_body_html ||' Sincerely,<br />'||utl_tcp.crlf;
 l_body_html := l_body_html ||' <span class="sig">The APEX Dev Team</span><br/>'||utl_tcp.crlf;
 l_body_html := l_body_html ||'</body></html>';
 apex_mail.send( p_to => 'some_user@somewhere.com', -- change to your email address
                 p_from => 'some_sender@somewhere.com', -- change to a real senders email address
                 p_body => l_body,
                 p_body_html => l_body_html,
                 p_subj => 'APEX_MAIL Package - HTML formatted message');
END;
/

Beberapa error yang muncul seperti :

  • PLS-00307: too many declarations of 'SEND'
  • Hal ini terjadi jika type yang tidak sama anda berikan pada value p_body dan p_body_html pastikan jika memakai varchar2 semua atau clob semua

  • ORA-06502: PL/SQL: numeric or value error
  • Hal ini terjadi kita memakai clob dengan data memakai concate '||'yang melebihi kapasitas varchar2(32767)

    Solusinya kita dapat mengganti dengan tipe data CLOB dengan memakai DBMS_LOB.APPEND, pada contoh diatas dapat dimodifikasi dengan cara

    DECLARE
     l_body CLOB;
     l_body_html CLOB;
    BEGIN
     l_body := 'To view the content of this message, please use an HTML enabled mail client.'||utl_tcp.crlf;
     dbms_lob.createtemporary (l_body_html,true);
     DBMS_LOB.append (l_body_html,'<html>
                     <head>
                     <style type="text/css">
                     body{font-family: Arial, Helvetica, sans-serif;
                     font-size:10pt;
                     margin:30px;
                     background-color:#ffffff;}
                     span.sig{font-style:italic;
                     font-weight:bold;
                     color:#811919;}
                     </style>
                     </head>
                     <body>');
     DBMS_LOB.append (l_body_html,'<p>Thank you for your interest in the <strong>APEX_MAIL</strong> package.</p>');
     DBMS_LOB.append (l_body_html,' Sincerely,<br />');
     DBMS_LOB.append (l_body_html,' <span class="sig">The APEX Dev Team</span><br/>');
     DBMS_LOB.append (l_body_html,'</body></html>');
     apex_mail.send(p_to => 'some_user@somewhere.com', -- change to your email address
                    p_from => 'some_sender@somewhere.com', -- change to a real senders email address
                    p_body => l_body,
                    p_body_html => l_body_html,
                    p_subj => 'APEX_MAIL Package - HTML formatted message');
     dbms_lob.freetemporary (l_body_html);  
    END;
    /