HomeSearchE-MailPrint
French English Chinese

HTML Email & PL/SQL

How to send a email with an PL/SQL procedure

Votes: 30

Comments: 0

Visits: 4672

PL/SQL 25-JUN-2004 10:37 AM, Arnaud Bontemps

This procedure will allow you to send an HTML email with a PL/SQL procedure.
You will need to give explicitly the smtp hostname and the smtp port number.

PROCEDURE html_email(p_to IN VARCHAR2,
p_from IN VARCHAR2,
p_subject IN VARCHAR2,
p_text IN VARCHAR2 DEFAULT NULL,
p_html IN VARCHAR2 DEFAULT NULL,
p_smtp_hostname IN VARCHAR2,
p_smtp_portnum IN VARCHAR2) IS
l_boundary VARCHAR2(255) DEFAULT 'a1b2c3d4e3f2g1';
l_connection utl_smtp.connection;
l_body_html CLOB := empty_clob; --This LOB will be the email message
l_offset NUMBER;
l_ammount NUMBER;
l_temp VARCHAR2(32767) DEFAULT NULL;
l_dest VARCHAR2(32767);
l_dest_tmp VARCHAR2(32767);
BEGIN
l_dest := p_to;
l_connection := utl_smtp.open_connection(p_smtp_hostname, p_smtp_portnum);
utl_smtp.helo(l_connection, p_smtp_hostname);
utl_smtp.mail(l_connection, p_from);
WHILE instr(l_dest, ',') != 0 LOOP
l_dest_tmp := substr(l_dest, 1, instr(l_dest, ',') - 1);
l_dest := substr(l_dest, instr(l_dest, ',') + 1);
utl_smtp.rcpt(l_connection, l_dest_tmp);
END LOOP;
utl_smtp.rcpt(l_connection, l_dest);
l_temp := l_temp || 'MIME-Version: 1.0' || chr(13) || chr(10);
l_temp := l_temp || 'To: ' || p_to || chr(13) || chr(10);
l_temp := l_temp || 'From: ' || p_from || chr(13) || chr(10);
l_temp := l_temp || 'Subject: ' || p_subject || chr(13) || chr(10);
l_temp := l_temp || 'Reply-To: ' || p_from || chr(13) || chr(10);
l_temp := l_temp || 'Content-Type: multipart/alternative; boundary=' ||
chr(34) || l_boundary || chr(34) || chr(13) || chr(10);
----------------------------------------------------
-- Write the headers
dbms_lob.createtemporary(l_body_html, FALSE, 10);
dbms_lob.WRITE(l_body_html, length(l_temp), 1, l_temp);
----------------------------------------------------
-- Write the text boundary
l_offset := dbms_lob.getlength(l_body_html) + 1;
l_temp := '--' || l_boundary || chr(13) || chr(10);
l_temp := l_temp || 'content-type: text/plain; charset=windows-1252' ||
chr(13) || chr(10) || chr(13) || chr(10);
dbms_lob.WRITE(l_body_html, length(l_temp), l_offset, l_temp);
----------------------------------------------------
-- Write the plain text portion of the email
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.WRITE(l_body_html, length(p_text), l_offset, p_text);
----------------------------------------------------
-- Write the HTML boundary
l_temp := chr(13) || chr(10) || chr(13) || chr(10) || '--' ||
l_boundary || chr(13) || chr(10);
l_temp := l_temp || 'content-type: text/html; charset=windows-1252' ||
chr(13) || chr(10) || chr(13) || chr(10);
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.WRITE(l_body_html, length(l_temp), l_offset, l_temp);
----------------------------------------------------
-- Write the HTML portion of the message
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.WRITE(l_body_html, length(p_html), l_offset, p_html);
----------------------------------------------------
-- Write the final html boundary
l_temp := chr(13) || chr(10) || '--' || l_boundary || '--' || chr(13);
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.WRITE(l_body_html, length(l_temp), l_offset, l_temp);
----------------------------------------------------
-- Send the email in 1900 byte chunks to UTL_SMTP
l_offset := 1;
l_ammount := 1900;
utl_smtp.open_data(l_connection);
WHILE l_offset < dbms_lob.getlength(l_body_html) LOOP
utl_smtp.write_data(l_connection, dbms_lob.substr(l_body_html, l_ammount, l_offset));
l_offset := l_offset + l_ammount;
l_ammount := least(1900, dbms_lob.getlength(l_body_html) - l_ammount);
END LOOP;
utl_smtp.close_data(l_connection);
utl_smtp.quit(l_connection);
dbms_lob.freetemporary(l_body_html);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END;
Next Previous

Comments

Login to leave a comment.

Articles

Oracle Installation on SUSE Linux Enterprise Server

Administration 14-JUN-2008 07:03 PM, Fabien Loudet

We will follow step-by-step the installation of Oracle Database 10g on SUSE Linux Enterprise Server. We choose here to install Oracle Enterprise Edition and create a database.


Articles RSS
Source Codes

Add Multi-language support

Portal 25-NOV-2004 10:30 AM, Pierre-Henry Tupin

Instant Messenger

Communication 24-NOV-2004 10:31 AM, Arnaud Bontemps

Who is online

Portal 24-NOV-2004 10:26 AM, Arnaud Bontemps

Change the Portal CSS

Portal 15-JUN-2004 04:15 PM, Arnaud Bontemps, Yoan Sultan


Source Codes RSS
News

Oracle VM Templates

02-SEP-2008 12:15 PM, Jérémie Hodnik

Further enabling customers to benefit from server consolidation, energy and space cost savings by using enterprise-class server virtualization, Oracle today announced Oracle® VM Templates.

Oracle Unbreakable Linux

02-SEP-2008 12:08 PM, Thibaut Maxant

Oracle's Linux commitment began in 1998 with the first commercial database on Linux.

Oracle's Industry Strategy

01-SEP-2008 12:01 PM, Thibaut Maxant

Oracle is the #1 in Relational Database

20-AUG-2008 07:26 PM, Thibaut Maxant

Gartner 2007 Worldwide RDBMS Market Share Reports 48.6% Share for Oracle.

Oracle Buys Skywire Software

02-AUG-2008 11:18 AM, Zhen Dong

June 23, 2008, Oracle announced that it has reached an agreement with Skywire Software and buys of its business application software.


News RSS