Since 2010, OraERP is a Oracle Forums, Community of Oracle Professionals including Fusion/Cloud Application Consultants, Enterprise Architects, ERP Cloud, HCM Cloud, CX Cloud and OCI Experts, ERP Experts, Oracle Apps Functional Consultants, Apps DBAs, DBAs, Cloud DBAs, Digital Architect, PaaS Experts, IaaS, OCI Architects, Technical Consultants, Fusion Middleware Experts, SQL, PL/SQL Developers and Project Managers. Welcome to OraERP.com Social Community, a friendly and active community of Oracle Technology Professionals who believe that technology can ‘make the world a better place’. By joining Oracle ERP Community you will have the ability to Post Topics, Receive our Newsletter, subscribe to threads and access many other special features. Registration is Quick and Simple. Get unlimited access to Oracle Tutorials, Articles, eBooks, Tools and Tips .
Email From Oracle PL/SQL
Email From Oracle PL/SQL
|
01-06-2013, 06:32 PM,
|
|||
|
|||
Email From Oracle PL/SQL
Emails: In it’s simplest form a single string or variable can be sent as the message body using the following procedure. In this case we have not included any header information or subject line in the message, so it is not very useful, but it is small. CREATE OR REPLACE PROCEDURE send_mail (p_to IN VARCHAR2, p_from IN VARCHAR2, p_message IN VARCHAR2, p_smtp_host IN VARCHAR2, p_smtp_port IN NUMBER DEFAULT 25) AS l_mail_conn UTL_SMTP.connection; BEGIN l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port); UTL_SMTP.helo(l_mail_conn, p_smtp_host); UTL_SMTP.mail(l_mail_conn, p_from); UTL_SMTP.rcpt(l_mail_conn, p_to); UTL_SMTP.data(l_mail_conn, p_message || UTL_TCP.crlf || UTL_TCP.crlf); UTL_SMTP.quit(l_mail_conn); END; / The code below shows how the procedure is called. BEGIN send_mail(p_to => ‘me@mycompany.com’, p_from => ‘admin@mycompany.com’, p_message => ‘This is a test message.’, p_smtp_host => ‘smtp.mycompany.com’); END; / Multi-Line Emails Multi-line messages can be written by expanding the UTL_SMTP.DATA command using the UTL_SMTP.WRITE_DATA command as follows. This is a better method to use as the total message size is no longer constrained by the 32K limit on a VARCHAR2 variable. In the following example the header information has been included in the message also. CREATE OR REPLACE PROCEDURE send_mail (p_to IN VARCHAR2, p_from IN VARCHAR2, p_subject IN VARCHAR2, p_message IN VARCHAR2, p_smtp_host IN VARCHAR2, p_smtp_port IN NUMBER DEFAULT 25) AS l_mail_conn UTL_SMTP.connection; BEGIN l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port); UTL_SMTP.helo(l_mail_conn, p_smtp_host); UTL_SMTP.mail(l_mail_conn, p_from); UTL_SMTP.rcpt(l_mail_conn, p_to); UTL_SMTP.open_data(l_mail_conn); UTL_SMTP.write_data(l_mail_conn, ‘Date: ‘ || TO_CHAR(SYSDATE, ‘DD-MON-YYYY HH24:MI:SS’) || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘To: ‘ || p_to || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘From: ‘ || p_from || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Subject: ‘ || p_subject || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Reply-To: ‘ || p_from || UTL_TCP.crlf || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, p_message || UTL_TCP.crlf || UTL_TCP.crlf); UTL_SMTP.close_data(l_mail_conn); UTL_SMTP.quit(l_mail_conn); END; / The code below shows how the procedure is called. BEGIN send_mail(p_to => ‘me@mycompany.com’, p_from => ‘admin@mycompany.com’, p_subject => ‘Test Message’, p_message => ‘This is a test message.’, p_smtp_host => ‘smtp.mycompany.com’); END; / HTML Emails The following procedure builds on the previous version, allowing it include plain text and/or HTML versions of the email. The format of the message is explained here. CREATE OR REPLACE PROCEDURE send_mail (p_to IN VARCHAR2, p_from IN VARCHAR2, p_subject IN VARCHAR2, p_text_msg IN VARCHAR2 DEFAULT NULL, p_html_msg IN VARCHAR2 DEFAULT NULL, p_smtp_host IN VARCHAR2, p_smtp_port IN NUMBER DEFAULT 25) AS l_mail_conn UTL_SMTP.connection; l_boundary VARCHAR2(50) := ‘—-=*#abc1234321cba#*=’; BEGIN l_mail_conn := UTL_SMTP.open_connection(p_smtp_host, p_smtp_port); UTL_SMTP.helo(l_mail_conn, p_smtp_host); UTL_SMTP.mail(l_mail_conn, p_from); UTL_SMTP.rcpt(l_mail_conn, p_to); UTL_SMTP.open_data(l_mail_conn); UTL_SMTP.write_data(l_mail_conn, ‘Date: ‘ || TO_CHAR(SYSDATE, ‘DD-MON-YYYY HH24:MI:SS’) || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘To: ‘ || p_to || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘From: ‘ || p_from || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Subject: ‘ || p_subject || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Reply-To: ‘ || p_from || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘MIME-Version: 1.0′ || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Content-Type: multipart/alternative; boundary=”‘ || l_boundary || ‘”‘ || UTL_TCP.crlf || UTL_TCP.crlf); IF p_text_msg IS NOT NULL THEN UTL_SMTP.write_data(l_mail_conn, ‘–’ || l_boundary || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Content-Type: text/plain; charset=”iso-8859-1″‘ || UTL_TCP.crlf || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, p_text_msg); UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf); END IF; IF p_html_msg IS NOT NULL THEN UTL_SMTP.write_data(l_mail_conn, ‘–’ || l_boundary || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, ‘Content-Type: text/html; charset=”iso-8859-1″‘ || UTL_TCP.crlf || UTL_TCP.crlf); UTL_SMTP.write_data(l_mail_conn, p_html_msg); UTL_SMTP.write_data(l_mail_conn, UTL_TCP.crlf || UTL_TCP.crlf); END IF; UTL_SMTP.write_data(l_mail_conn, ‘–’ || l_boundary || ‘–’ || UTL_TCP.crlf); UTL_SMTP.close_data(l_mail_conn); UTL_SMTP.quit(l_mail_conn); END; / The code below shows how the procedure is called. DECLARE l_html VARCHAR2(32767); BEGIN l_html := ‘ This is a HTML version of the test message.
|
|||
« Next Oldest | Next Newest »
|
Users browsing this thread: 2 Guest(s)