Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to send_mail to multiple recipients

Posted on 2011-02-16
4
Medium Priority
?
5,370 Views
Last Modified: 2013-12-12
I have a scheduled job to run (I created a job chain).  I'd like to have the result automatically sent to multiple recipients after the job is finished .  It works with just send to one person, but I would like the email sent to multiple persons.  How do I do that?
These are the codes:

CREATE OR REPLACE PROCEDURE send_mail (
  p_mail_host  IN  VARCHAR2,
  p_from       IN  VARCHAR2,
  p_to         IN  VARCHAR2,
  p_subject    IN  VARCHAR2,
  p_message    IN  VARCHAR2)
AS
  l_mail_conn   UTL_SMTP.connection;
BEGIN
  l_mail_conn := UTL_SMTP.open_connection(p_mail_host, 25);
  UTL_SMTP.helo(l_mail_conn, p_mail_host);
  UTL_SMTP.mail(l_mail_conn, p_from);
  UTL_SMTP.rcpt(l_mail_conn, p_to);

  UTL_SMTP.open_data(l_mail_conn);

  UTL_SMTP.write_data(l_mail_conn, 'Date: ' || TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') || Chr(13));
  UTL_SMTP.write_data(l_mail_conn, 'From: ' || p_from || Chr(13));
  UTL_SMTP.write_data(l_mail_conn, 'Subject: ' || p_subject || Chr(13));
  UTL_SMTP.write_data(l_mail_conn, 'To: ' || p_to || Chr(13));
  UTL_SMTP.write_data(l_mail_conn, '' || Chr(13));
  UTL_SMTP.write_data(l_mail_conn, p_message || Chr(13));

  UTL_SMTP.close_data(l_mail_conn);
  UTL_SMTP.quit(l_mail_conn);
END send_mail;
/

CREATE OR REPLACE PROCEDURE email_alert AS
  l_mail_host  VARCHAR2(50) := 'ipaddress';
  l_from       VARCHAR2(50) := userid.com';
  l_to           VARCHAR2(50) := userid.com';  
  v_status     VARCHAR2(50);
BEGIN
 
     
  SELECT status INTO v_status FROM user_scheduler_job_log
  WHERE log_date=(SELECT MAX(log_date) FROM user_scheduler_job_log
                  WHERE JOB_NAME='TEST' AND  owner='MY');
 
IF v_status = 'SUCCEEDED'
THEN
  send_mail(p_mail_host => l_mail_host,
            p_from      => l_from,
            p_to        => l_to,
            p_subject   => 'EMAIL_ALERT: Successful.',
            p_message   => 'EMAIL_ALERT: completed successfully!');
ELSE
  send_mail(p_mail_host => l_mail_host,
              p_from      => l_from,
              p_to        => l_to,
              p_subject   => 'EMAIL_ALERT: Error',
              p_message   => 'EMAIL_ALERT: Failed With The Following
  Error:' || SQLERRM);
END IF;
END email_alert;
/
0
Comment
Question by:scpig
  • 3
4 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 total points
ID: 34909717
You need to call the  UTL_SMTP.rcpt   procedure once for each recipient.

The "to" text is irrelevant, but for cosmetic purposes you might want to update that as well.

Easiest method is probably to put your multiple people into a pl/sql collection and simply iterate through it ,  something like this...

for i in 1..p_recipients.count loop
    utl_smtp.rcpt(l_mail_conn,p_recipients(i));
end loop;

0
 

Author Comment

by:scpig
ID: 34909982
Where do I define the recipients for p_reipients?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34910315
since they are just strings you could declare them as dbms_sql.varchar2s in your email alert procedure and corresponding parameter in your send_mail (instead of the "to")


then simply assign them like you do a normal string

v_recipient(1) := 'somebody@someaddress.com';
v_recipient(2) := 'somebodyelse@someaddress.com';
v_recipient(3) := 'yetanotherperson@someaddress.com';

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 34910329
where those addresses come from, I don't know, it's not particularly important.

in your example code it looks like you currently have it hardcoded, you could have them in a table, or a file, or derived from a webserice, or any other place that would be appropriate for your system
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question