Vous êtes sur la page 1sur 3

utl_smtp

The utl_smtp utility enables e-mail messages to be sent from the database (PL/S
QL) 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 o
ccur. These events could be anything ranging from space deficits to unauthorize
d database access. Anything that can be monitored can be sent in an e-mail.

The procedure below (sendmail.sql) provides simple e-mail capability. It requir


es an SMTP host port (25) to be opened. The host can be specified by host name
or IP address, with 127.0.0.1 representing the local host machine. Once the por
t is opened, the procedure specifies the subject, sender, recipient and body of
the message.

< 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;
/

Calling the sendmail procedure can be performed like below:

exec send_mail(msg_to=>'dave@oracleutilities.com', -
msg_subject => 'Hello from Oracle', -
msg_text => 'This is the body of the message'-
);

The e-mail will be sent from the database and received by the recipient. Figure
5.2 displays the actual e-mail as delivered to my inbox.

Figure 5.2 Email Received From Oracle


E-mail is a powerful feature that can enhance any monitoring environment. E-mai
ls can be sent from any PL/SQL code to any valid e-mail address including wirele
ss devices that are e-mail enabled. Database content can be included in the bod
y of the message thereby giving the DBA all of the information they need to reso
lve the problem. The following events are certainly worthy of an e-mail when th
ey occur:
· A tablespace running out of space
· The archive destination directory running out of space
· An object unable to allocate the next extent
· Unauthorized access to, or within, the database
· A query taking n seconds of CPU
· New errors in the alert log
utl_smtp is a powerful utility that should be utilized in a monitoring environme
nt.
--------------------------------------------------------------------------------
---
The following example illustrates how UTL_SMTP is used by an application to send
e-mail. The application connects to an SMTP server at port 25 and sends a simpl
e text message.
DECLARE
c UTL_SMTP.CONNECTION;
PROCEDURE send_header(name IN VARCHAR2, header IN VARCHAR2) AS
BEGIN
UTL_SMTP.WRITE_DATA(c, name || ': ' || header || UTL_TCP.CRLF);
END;
BEGIN
c := UTL_SMTP.OPEN_CONNECTION('smtp-server.acme.com');
UTL_SMTP.HELO(c, 'foo.com');
UTL_SMTP.MAIL(c, 'sender@foo.com');
UTL_SMTP.RCPT(c, 'recipient@foo.com');
UTL_SMTP.OPEN_DATA(c);
send_header('From', '"Sender" <sender@foo.com>');
send_header('To', '"Recipient" <recipient@foo.com>');
send_header('Subject', 'Hello');
UTL_SMTP.WRITE_DATA(c, UTL_TCP.CRLF || 'Hello, world!');
UTL_SMTP.CLOSE_DATA(c);
UTL_SMTP.QUIT(c);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error THEN
BEGIN
UTL_SMTP.QUIT(c);
EXCEPTION
WHEN UTL_SMTP.TRANSIENT_ERROR OR UTL_SMTP.PERMANENT_ERROR THEN
NULL; -- When the SMTP server is down or unavailable, we don't have
-- a connection to the server. The QUIT call will raise an
-- exception that we can ignore.
END;
raise_application_error(-20000,
'Failed to send mail due to the following error: ' || sqlerrm);
END;

Vous aimerez peut-être aussi