Emailing With Large Text
While using conventional PLSQL procedure, the Oracle UTL_SMTP.Write_Data have input parameter as varchar2 -- which has a limit of size, so while emailing large content through single Write_data, is not possible. so the solution is to pass the data to email through chunks.
Below is the old Procedure. (Having trouble with size)
CREATE OR REPLACE PROCEDURE SUPPORT_PORTAL.send_mail_html_new (
p_to IN VARCHAR2,
p_from IN VARCHAR2,
p_cc IN VARCHAR2,
p_bcc IN VARCHAR2,
p_subject IN VARCHAR2,
p_text_msg IN VARCHAR2 DEFAULT NULL,
p_attach_name IN VARCHAR2 DEFAULT NULL,
p_attach_mime IN VARCHAR2 DEFAULT NULL,
p_attach_blob IN BLOB 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#*=';
l_step PLS_INTEGER := 12000;
vrfy_msg UTL_SMTP.reply;
success_value NUMBER (4) := 252;
email_addr VARCHAR2 (50) := NULL;
from_addr VARCHAR2 (100) := p_from;
to_addr VARCHAR2 (1000) := p_to;
cc_addr VARCHAR2 (1000) := p_cc;
bcc_addr VARCHAR2 (1000) := p_bcc;
to_mail VARCHAR2 (1000) := NULL;
cc_mail VARCHAR2 (1000) := NULL;
bcc_mail VARCHAR2 (1000) := NULL;
no_addresses NUMBER (4) := 0;
temp VARCHAR2 (4000) := NULL;
temp2 VARCHAR2 (4000) := NULL;
-- make sure you set a multiple of 3 not higher than 24573
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);
to_addr := to_addr || ';';
cc_addr := cc_addr || ';';
bcc_addr := bcc_addr || ';';
to_mail := to_addr;
cc_mail := cc_addr;
bcc_mail := bcc_addr;
/*** Send mail to To: addresses ****/
WHILE (INSTR (to_addr, ';') != 0)
LOOP
email_addr := SUBSTR (to_addr, 1, INSTR (to_addr, ';') - 1);
vrfy_msg := UTL_SMTP.vrfy (l_mail_conn, email_addr);
UTL_SMTP.rcpt (l_mail_conn, email_addr);
-- IF vrfy_msg.code = success_value
-- THEN
-- UTL_SMTP.rcpt (l_mail_conn, email_addr);
-- END IF;
to_addr := SUBSTR (to_addr, INSTR (to_addr, ';') + 1);
email_addr := NULL;
END LOOP;
/*** Send mail to CC: addresses ****/
WHILE (INSTR (cc_addr, ';') != 0)
LOOP
email_addr := SUBSTR (cc_addr, 1, INSTR (cc_addr, ';') - 1);
vrfy_msg := UTL_SMTP.vrfy (l_mail_conn, email_addr);
UTL_SMTP.rcpt (l_mail_conn, email_addr);
-- IF vrfy_msg.code = success_value
-- THEN
-- UTL_SMTP.rcpt (l_mail_conn, email_addr);
-- END IF;
cc_addr := SUBSTR (cc_addr, INSTR (cc_addr, ';') + 1);
email_addr := NULL;
END LOOP;
/*** Send mail to BCC: addresses ****/
WHILE (INSTR (bcc_addr, ';') != 0)
LOOP
email_addr := SUBSTR (bcc_addr, 1, INSTR (bcc_addr, ';') - 1);
vrfy_msg := UTL_SMTP.vrfy (l_mail_conn, email_addr);
UTL_SMTP.rcpt (l_mail_conn, email_addr);
-- IF vrfy_msg.code = success_value
-- THEN
-- UTL_SMTP.rcpt (l_mail_conn, email_addr);
-- END IF;
bcc_addr := SUBSTR (bcc_addr, INSTR (bcc_addr, ';') + 1);
email_addr := NULL;
END LOOP;
--UTL_SMTP.rcpt (l_mail_conn, p_to);
--UTL_SMTP.rcpt (l_mail_conn, p_cc);
--UTL_SMTP.rcpt (l_mail_conn, p_bcc);
-- IF p_bcc IS NOT NULL
-- THEN
-- UTL_SMTP.rcpt (l_mail_conn, bcc_addr);
-- END IF;
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, 'Cc: ' || p_cc || UTL_TCP.crlf);
--UTL_SMTP.write_data (l_mail_conn, 'Bcc: ' || p_bcc || 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/mixed; 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/html; 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_attach_name 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: '
|| p_attach_mime
|| '; name="'
|| p_attach_name
|| '"'
|| UTL_TCP.crlf
);
UTL_SMTP.write_data (l_mail_conn,
'Content-Transfer-Encoding: base64' || UTL_TCP.crlf
);
UTL_SMTP.write_data (l_mail_conn,
'Content-Disposition: attachment; filename="'
|| p_attach_name
|| '"'
|| UTL_TCP.crlf
|| UTL_TCP.crlf
);
FOR i IN 0 .. TRUNC ((DBMS_LOB.getlength (p_attach_blob) - 1) / l_step)
LOOP
UTL_SMTP.write_data
(l_mail_conn,
UTL_RAW.cast_to_varchar2
(UTL_ENCODE.base64_encode (DBMS_LOB.SUBSTR (p_attach_blob,
l_step,
i * l_step + 1
)
)
)
);
END LOOP;
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;
/
Comments
Post a Comment