Solved

How to send_mail to multiple recipients

Posted on 2011-02-16
4
4,293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to take different types of Oracle backups using RMAN.

751 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