Important alert: (current site time 10/31/2014 3:58:44 AM EDT)
 

article

Send email through PL/SQL

Email
Submitted on: 5/14/2002 4:33:19 PM
By: Gnu Kemist  
Level: Advanced
User Rating: By 12 Users
Compatibility: Oracle
Views: 78869
author picture
(About the author)
 
     This stored procedure will allow you to send email to your users (or even yourself) with job status or even serve as an error reporting tool for your application. You could call it from other stored procedures or triggers upon insert/update/delete statements; or call it from a visual basic application to send you an email when an error occurrs; The uses are almost that unlimited! You can improve this procedure to send HTML or XML emails... Have fun and please vote!

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				create or replace package email_handler is
 -- Author : Og Maciel aka Gnu Kemist
 -- Created : 4/5/2002 4:14:05 PM
 -- Purpose : To serve as an email handler object
 
 procedure send_email(subjectin varchar2,
 to_useridin varchar2 := NULL,
 v_body in varchar2 := NULL,
 from_namein varchar2 := NULL,
 to_namein varchar2 := NULL,
 content_type in varchar2 := NULL);
end email_handler;
create or replace package body email_handler is
 /*
 Procedure: send_email
 Author: Og Maciel aka Gnu Kemist
 Purpose:
 */ 
 procedure send_email(subjectvarchar2,
to_useridvarchar2 := NULL,
v_bodyvarchar2 := NULL,
from_namevarchar2 := NULL,
to_namevarchar2 := NULL,
content_type varchar2 := NULL)
 IS
 c utl_smtp.connection;
 from_userid varchar2(40) := 'webmaster@yourcompany.com';
 send_user varchar2(40);
 from_domain VARCHAR2(200) := SUBSTR(from_userid,INSTR(from_userid,'@')+1);
 
 smtp_server varchar2(50) := 'YOUR_SERVER_NAME';
 my_body varchar2(32000);
 
 PROCEDURE header(name VARCHAR2, value VARCHAR2) IS
 BEGIN
utl_smtp.write_data(c, name || ': ' || value || utl_tcp.CRLF);
 END;
 
 BEGIN
 
 if to_userid is null then
send_user := user || '@yourcompany.com'; -- This sends email to current user logged in
 else
send_user := to_userid;
 end if;
 
 my_body := v_body;
 
 c := utl_smtp.open_connection(smtp_server);
 utl_smtp.helo(c, from_domain );
 utl_smtp.mail(c, from_userid );
 utl_smtp.rcpt(c, send_user );
 utl_smtp.open_data(c);
 header('From','"'||NVL(from_name,from_userid)||'" <'||from_userid||'>');
 header('To','"'||NVL(to_name,to_userid)||'" <'||to_userid||'>');
 header('Subject', subject );
 header('Content-Type', NVL(content_type,'text/html'));
 utl_smtp.write_data(c, utl_tcp.CRLF || my_body );
 utl_smtp.close_data(c);
 utl_smtp.quit(c);
 
 EXCEPTION
 WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
 utl_smtp.quit(c);
 end send_email;
end email_handler;

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/15/2002 10:19:35 AMGnu Kemist

I kindly ask you all to rate my code...
(If this comment was disrespectful, please report it.)

 
5/20/2002 8:40:44 PMKC

can you re-format the coding, it's kinda hard to read....

BTW, do we need to configure the SQL Email?
(If this comment was disrespectful, please report it.)

 
5/21/2002 1:37:31 PMDaniel

i like to test it.
(If this comment was disrespectful, please report it.)

 
5/21/2002 1:58:57 PMGnu Kemist

The article is now presented as a text file for download in order to preserve all the formatting. I ask that those who were kind enough to send me a comment, to rate my code.
Thank you
(If this comment was disrespectful, please report it.)

 
9/19/2002 5:24:48 AM

please comment how to send file attachment with this mail ie through oracle send mail.
please revert to me at vishal150@rediffmail.com
(If this comment was disrespectful, please report it.)

 
9/19/2002 5:27:08 AM

how to send an attachment file through this mail program
(If this comment was disrespectful, please report it.)

 
12/18/2002 5:43:46 PM

This is really a good program. How to send the attachment along with this mail.

thanks 'n regards
swamy

thanks and
(If this comment was disrespectful, please report it.)

 
12/30/2002 5:32:41 PM

When I run the program it compiles successfully but at run time throws up an error as

ORA-29549: class SYS.oracle/plsql/net/TCPConnection has changed, Java session state cleared
ORA-06512: at
(If this comment was disrespectful, please report it.)

 
1/4/2003 5:23:04 AM

There are some package named utl_smtp and utl_tcp which exists in the PL/SQL code but these dont exist in Oracle database what about them, where would i find them
(If this comment was disrespectful, please report it.)

 
3/26/2003 8:32:57 AMChris_Berry

I see, you wrote this without checking the syntax. You need to look at the language diferences before you execute it.
(If this comment was disrespectful, please report it.)

 
8/8/2003 5:01:38 AM

There are some package named utl_smtp and utl_tcp which exists in the PL/SQL code but these dont exist in Oracle database what about them, where would i find them
(If this comment was disrespectful, please report it.)

 
8/8/2003 5:03:08 AM

There are some package named utl_tcp these dont exist in Oracle database what about them, where would i find them
(If this comment was disrespectful, please report it.)

 
12/5/2004 5:55:51 AM

I suggest you to use CmdEmail component. The component can send email with attachment. For more information please visit http://www.lv2000.com/sendmail.htm
(If this comment was disrespectful, please report it.)

 
2/3/2005 10:36:14 AM

Hi,
how can I send an email to more then 1 recipient? Thank you for your help.
(If this comment was disrespectful, please report it.)

 
2/4/2005 2:58:35 AM

Hi, how can I send an email to more then 1 recipient? Thank you for your help.
(If this comment was disrespectful, please report it.)

 
1/30/2007 11:36:59 AMVamsi

Hi,
I would like to know how to use this code for sending e-mail alerts to the users through Oracle Applications front end. I am registering one plsql program in oracle applications and this program will validate some business rules and when there are any exceptions then I am writing them to one error table. my requirement is to read the error records from this table and send the e-mail to the concerned users with all the error details that are mentioned in the error table after the completion of running the program through oracle applications. please help
(If this comment was disrespectful, please report it.)

 
3/28/2007 10:09:35 AMVamsi

when I tried to run the package I got the following error.

ERROR at line 1:
ORA-29278: SMTP transient error: 421 Service not available
ORA-06512: at "SYS.UTL_SMTP", line 17
ORA-06512: at "SYS.UTL_SMTP", line 96
ORA-06512: at "SYS.UTL_SMTP", line 374
ORA-06512: at "SEND_EMAIL", line 39
ORA-29278: SMTP transient error: 421 Service not available
ORA-06512: at line 1
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.