4

I have a pl/sql procedure that emails a number of people (20,000+). My question has to do with utl_mail package, and when the connection is actually opened to the email server.

Example:

BEGIN
...
OPEN CUR_person;
FETCH CUR_person INTO REC_person;    
WHILE CUR_person%FOUND

LOOP
  UTL_MAIL.send(sender => '[email protected]',
                recipients => '[email protected]',
                subject => 'Test Mail',
                message => 'Hello World',
                mime_type => 'text/html');
FETCH CUR_person INTO REC_person;                  
END LOOP;

...
END;
/

I was wondering if the connection gets opened once per person, or if it opens for the first person, and stays open until the procedure completes?

If it is opened once per person- is that bad coding? Will it bomb if I encounter bad data?

If any more info is needed, please ask. Thanks!!

1
  • Yes, each execution of UTL_MAIL.send will open and close a connection to the mail server. Whether this is a problem, however, depends on how many rows you're processing, how much data is in the email, and how often your procedure is called. If the numbers are low, it probably doesn't matter a great deal. Sep 17, 2012 at 4:57

1 Answer 1

6

UTL_MAIL hides the under lying SMTP API which is UTL_SMTP. It is preferred generally for ease of use. If you see, UTL_SMTP you will notice that you have to do a utl_smtp.Rcpt for each email address and unfortunately it accepts only one email id at a time. So having said this for more than one email address the underlying logic is same i.e. loop through the email addresses and call utl_smtp.Rcpt for each email address. A better solution to this would be to just use emailing distribution lists i.e. one email id which has a group of other email id's. So, with UTL_SMTP it looks something like below-

declare
  v_From      VARCHAR2(80) := '[email protected]';
  v_cc        VARCHAR2(80) := '[email protected]';  
  v_Recipient VARCHAR2(80) := '[email protected]';
  v_Subject   VARCHAR2(80) := 'test Subject';
  v_Mail_Host VARCHAR2(50) := 'hub.fun.com';
  v_Mail_Conn utl_smtp.Connection;
  crlf        VARCHAR2(2)  := chr(13)||chr(10); 
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); 
 utl_smtp.Rcpt(v_Mail_Conn, v_cc); -- To CC recepient

 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: '     || v_cc || crlf ||
   'Content-Type: text/html;' ||crlf ||
   'Hello this is a test email');

 utl_smtp.Quit(v_mail_conn);
end;

I don't see any bad programming in the way your code looks like, its rather the only way to do it if you have multiple email id's.Oracle engine needs to submit the email payload to the SMTP server (as specified by you) for each email id, like submitting messages to a queue. A rather obvious suggestion would be to create a couple of Distribution List with a bunch of email ids evenly distributed and use the DL ids rather than the actual email ids.

This is one of my "Ask Tom" questions that might clear this a little bit.

Another quick way would be to modify your code to build a comma separated string of email address as UTL_MAIL can accept comma separated email addresses, like-

BEGIN
...
email_list   VARCHAR2(10000) := NULL;
email_count  NUMBER(10) := 0;
FOR c_rec in CUR_person
LOOP
   email_list := c_rec.CUR_person || ' , ' || email_list;
   email_count := email_count +1;
   if (email_count = 100)  --100 email ids at a time
   then
      UTL_MAIL.send(sender => '[email protected]',
                recipients => email_list,
                subject => 'Test Mail',
                message => 'Hello World',
                mime_type => 'text/html');
       email_list := NULL; 
       email_count := 0; 
   end if;
END LOOP

...
END;
/
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.