Thursday, 28 October 2010

how to create pl/sql procedure to send mails

This is an example of a procedure that sends an email from an Oracle10g database:

CREATE OR REPLACE PROCEDURE  p_send_mail (
   sender          IN VARCHAR2,
   recipient       IN VARCHAR2,
   carbon_copies   IN VARCHAR2 DEFAULT NULL ,
   mail_subject    IN VARCHAR2,
   mail_body       IN VARCHAR2
)
AS
   conntype          UTL_SMTP.connection;
   mailsrvr          VARCHAR2 (50) := 'mail_server_name_or_ip';  
   svrport           VARCHAR2 (3) := 25;
   svrdate           VARCHAR2 (255) := TO_CHAR (SYSDATE, 'dd Mon yy hh24:mi:ss');
   msghdr            VARCHAR2 (2000) := NULL;
   linefeed          VARCHAR (2) := CHR (13) || CHR (10);
   msgbody           VARCHAR2 (8000);
   activation_link   VARCHAR2 (100);
BEGIN
   conntype := UTL_SMTP.open_connection (mailsrvr, svrport);
   msghdr :=
         'Date: '
      || svrdate
      || linefeed
      || 'From: <'
      || sender
      || '>'
      || linefeed
      || 'Subject: '
      || mail_subject
      || linefeed
      || 'To: '
      || recipient
      || linefeed
      || ''
      || linefeed;
   msgbody := msghdr || mail_body;
   UTL_SMTP.helo (conntype, mailsrvr);
   UTL_SMTP.mail (conntype, sender);
   UTL_SMTP.rcpt (conntype, recipient);
   UTL_SMTP.DATA (conntype, msgbody);
   UTL_SMTP.quit (conntype);
END;
/

 On this post you may find this procedure,  an example on how to use this procedure and a solution to an error that he gets.
http://kr.forums.oracle.com/forums/thread.jspa?threadID=650243
Use it well.

2 comments:

  1. I don't know why you deleted my post but thank you for posting the source of the code.
    Good day,
    Kumar

    ReplyDelete
  2. Sorry I have deleted it, I was quite in a hurry yesterday when I wanted to edit this post and your comment.
    Thank you very much for visiting my blog, and I'm really happy that you find it useful.

    ReplyDelete