Oracle utl_smtp utility to sending e-mail
The utl_smtp
utility enables e-mail messages to be sent
from the database (PL/SQL) to any valid
e-mail address. This can be very useful in
database monitoring since e-mails can be
sent to production support personnel when
certain events occur. These events could be
anything ranging from space deficits to
unauthorized database access. Anything that
can be monitored can be sent in an e-mail.
sendmail.sql
CREATE OR REPLACE PROCEDURE SEND_MAIL (
msg_to varchar2,
msg_subject varchar2,
msg_text varchar2 )
IS
c utl_smtp.connection;
rc integer;
msg_from varchar2(50) := 'Oracle9.2';
mailhost VARCHAR2(30) := '127.0.0.1'; --
local database host
BEGIN
c := utl_smtp.open_connection(mailhost, 25);
-- SMTP on port 25
utl_smtp.helo(c, mailhost);
utl_smtp.mail(c, msg_from);
utl_smtp.rcpt(c, msg_to);
utl_smtp.data(c,'From: Oracle Database' ||
utl_tcp.crlf ||
'To: ' || msg_to || utl_tcp.crlf ||
'Subject: ' || msg_subject ||
utl_tcp.crlf || msg_text);
utl_smtp.quit(c);
EXCEPTION
WHEN UTL_SMTP.INVALID_OPERATION THEN
dbms_output.put_line(' Invalid Operation in
Mail attempt
using UTL_SMTP.');
WHEN UTL_SMTP.TRANSIENT_ERROR THEN
dbms_output.put_line(' Temporary e-mail
issue - try again');
WHEN UTL_SMTP.PERMANENT_ERROR THEN
dbms_output.put_line(' Permanent Error
Encountered.');
END;
/
According
to OTN:
"This
new package makes it possible for a
PL/SQL programmer to send
programmatically composed emails. It
requires only the normal mental model of
a user of a GUI email client rather than
an understanding of the underlying
protocol (SMTP) features. This
distinguishes it from Utl_Smtp which was
introduced in Oracle8i Database.
Utl_Smtp requires that the programmer
understands the details of the SMTP
protocol. Utl_Mail is much simpler to
use because it supports just a limited,
but very common, subset of the
functionality that Utl_Smtp provides."
utl_smtp is
a powerful e-mail utility that should be utilized
in a monitoring environment.
|
|