Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to send_mail to multiple recipients

Posted on 2011-02-16
4
Medium Priority
?
5,086 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 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

Independent Software Vendors: 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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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 Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

688 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