How to sent email in Oracle PL/SQL package to multiple receivers?

爷,独闯天下 提交于 2019-12-01 11:07:48
Alex Poole

You need to call utl_smtp.rcpt multiple times, once for each recipient; you can't give a list of values in one call.

From the UTL_SMTP.RCPT documentation:

To send a message to multiple recipients, call this routine multiple times. Each invocation schedules delivery to a single e-mail address.

That means you can't really pass a string of names, unless you're happy to parse the individual addresses out; it would be easier to pass an array of values, probably.

The TO header is a separate issue; if I recall correctly, that is really just for display, and having an address as a rcpt but not in the TO (or CC) header is how BCC is implemented. Citation needed though...

Here's an old AskTom article demonstrating this. jonearles suggestion to use UTL_MAIL should be investigated though.

UTL_MAIL

The format is:

UTL_MAIL.SEND (sender, recipientlist, cc, bcc, subject, Message, mime_type, priority)

The recipientlist, cc, and bcc parameters are all comma-separated lists of recipient, copy to, and blind copy e-mail addresses.

The sender, subject, message, and mime_type parameters are all single item fields.

Just run below procedure with change code:

v_Mail_Host VARCHAR2(50) := 'uacemail.rxcorp.com'; -- your host ip or name


Execute:

begin
prc_email_send( 'sohid10@yahoo.com', -- Mail From
'smolla@bd.imshealth.com',---Recipient
'sohidatibd@gmail.com;smokarem@bd.imshealth.com',-- Cc List
'This is mail subject ',
'This is mail body' );
end;
/

Procedure Code:

Create or replace procedure prc_email_send(
v_From      VARCHAR2,
v_Recipient VARCHAR2,
v_cc_list varchar2,
v_Subject   VARCHAR2,   
v_Mail_body VARCHAR2
)

is
v_Mail_Host VARCHAR2(50) := 'uacemail.rxcorp.com';
v_Mail_Conn utl_smtp.Connection;

crlf        VARCHAR2(2)  := chr(13)||chr(10);
CC_parties varchar2(2000);

begin

v_Mail_Conn := utl_smtp.Open_Connection(v_Mail_Host, 25);
utl_smtp.Helo(v_Mail_Conn, v_Mail_Host);
utl_smtp.Mail(v_Mail_Conn, v_From);

utl_smtp.Rcpt(v_Mail_Conn, v_Recipient);

 for i in (SELECT LEVEL AS id, REGEXP_SUBSTR(v_cc_list, '[^;]+', 1, LEVEL) AS cc_email_name
           FROM dual
           CONNECT BY REGEXP_SUBSTR(v_cc_list, '[^;]+', 1, LEVEL) IS NOT NULL) loop
    CC_parties := CC_parties||';'|| i.cc_email_name;
  utl_smtp.Rcpt(v_Mail_Conn,i.cc_email_name);

end loop;


utl_smtp.Data(v_Mail_Conn,
                'Date: '   || to_char(sysdate, 'Dy, DD Mon YYYY hh24:mi:ss') || crlf ||
                'From: '   || v_From || crlf ||
                'Subject: '|| v_Subject || crlf ||
                'To: '     || v_Recipient || crlf ||
                'Cc: '     || CC_parties|| crlf ||
                'Content-Type: text/html;' ||crlf ||
                v_Mail_body);
utl_smtp.Quit(v_mail_conn);

EXCEPTION
   WHEN OTHERS THEN
      BEGIN
     DBMS_OUTPUT.put_line (
        SUBSTR (
              'Unable to send mail to recipients. Error message: '
              || SQLERRM
              || CHR (10)
              || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE (),
                 1,255));
     UTL_SMTP.quit (v_Mail_Conn);
     UTL_TCP.close_all_connections;
  EXCEPTION
     WHEN UTL_SMTP.transient_error OR UTL_SMTP.permanent_error   THEN
        NULL;     
  END;
END;

This is working fine for myself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!