Solved

How to send_mail to multiple recipients

Posted on 2011-02-16
4
3,413 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 73

Accepted Solution

by:
sdstuber earned 250 total points
Comment Utility
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
Comment Utility
Where do I define the recipients for p_reipients?
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
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…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now