table_to_html_mail
CREATE OR REPLACE PACKAGE BODY DDSBASE.PKG_TABLE_TO_HTML
AS
-- week cursor for fetching row
TYPE REFCUR IS REF CURSOR;
v_job_module VARCHAR2(50) := 'REPORTS'; -- Job Module Name :
v_pkgc VARCHAR2(50) := 'PKG_TABLE_TO_HTML'; -- PLSQL Package Name
v_job_owner VARCHAR2(50) := 'DDSBASE'; -- Owner of the Job
v_proc VARCHAR2(3000); -- Procedure Name
c_delim VARCHAR2(10) := ' : '; -- Delimiter Used In Logging
-- get the cursor id and concatenate fetched row as html row
FUNCTION CONCATENATE_ROW (ICURNUM INTEGER)
RETURN CLOB
IS
CLOBTEMP CLOB;
CLOBDATA CLOB := NULL;
ICOUNT INTEGER;
ICOLCOUNT INTEGER;
DESCTABREC DBMS_SQL.DESC_TAB;
DTEMP DATE;
NTEMP NUMBER;
BEGIN
-- to get columns type and columns count
DBMS_SQL.DESCRIBE_COLUMNS (ICURNUM, ICOLCOUNT, DESCTABREC);
-- loop every column and concatenate
FOR ICOUNT IN DESCTABREC.FIRST .. ICOLCOUNT
LOOP
IF DESCTABREC (ICOUNT).COL_TYPE = 1
THEN -- varchar2
DBMS_SQL.COLUMN_VALUE (ICURNUM, ICOUNT, CLOBTEMP);
ELSIF DESCTABREC (ICOUNT).COL_TYPE = 2
THEN -- number
DBMS_SQL.COLUMN_VALUE (ICURNUM, ICOUNT, NTEMP);
CLOBTEMP := TO_CHAR (NTEMP);
ELSIF DESCTABREC (ICOUNT).COL_TYPE = 12
THEN -- date
DBMS_SQL.COLUMN_VALUE (ICURNUM, ICOUNT, DTEMP);
CLOBTEMP := TO_CHAR (DTEMP);
END IF;
CLOBDATA := CLOBDATA || ' ' || HTF.TABLEDATA (CLOBTEMP, 'CENTER');
END LOOP;
RETURN (CLOBDATA);
END;
PROCEDURE DEFINE_COLUMNS (ICURNUM INTEGER)
IS
ICOLCOUNT INTEGER;
DESCTABREC DBMS_SQL.DESC_TAB;
CLOBTEMP CLOB;
DTEMP DATE;
NTEMP NUMBER;
BEGIN
-- Initialize Log Variables
v_proc := 'DEFINE_COLUMNS';
plib.o_log := log_type.initialize('YES', v_job_module, v_job_owner, v_pkgc , v_proc);
-- to get columns type and columns count
DBMS_SQL.DESCRIBE_COLUMNS (ICURNUM, ICOLCOUNT, DESCTABREC);
-- loop every column and define type
FOR ICOUNT IN DESCTABREC.FIRST .. ICOLCOUNT
LOOP
IF DESCTABREC (ICOUNT).COL_TYPE = 1
THEN -- varchar2
DBMS_SQL.DEFINE_COLUMN (ICURNUM, ICOUNT, CLOBTEMP);
ELSIF DESCTABREC (ICOUNT).COL_TYPE = 2
THEN -- number
DBMS_SQL.DEFINE_COLUMN (ICURNUM, ICOUNT, NTEMP);
ELSIF DESCTABREC (ICOUNT).COL_TYPE = 12
THEN -- date
DBMS_SQL.DEFINE_COLUMN (ICURNUM, ICOUNT, DTEMP);
END IF;
END LOOP;
plib.o_log.log(1, 4, 'Completed', v_pkgc || '.' || v_proc, NULL, ICURNUM);
EXCEPTION
WHEN OTHERS THEN
plib.o_log.log(SQLCODE, 1, SQLERRM, v_proc, NULL, ICURNUM);
raise_application_error(SQLCODE, SQLERRM);
END;
FUNCTION CREATE_HTML (CLOBMESSAGE CLOB, ICURNUM INTEGER)
RETURN CLOB
IS
DESCTABREC DBMS_SQL.DESC_TAB;
ICOLCOUNT INTEGER;
ICOUNT INTEGER;
CLOBHTML CLOB;
CLOBTEMP CLOB;
BEGIN
-- Initialize Log Variables
v_proc := 'CREATE_HTML';
plib.o_log := log_type.initialize('YES', v_job_module, v_job_owner, v_pkgc , v_proc);
DBMS_SQL.DESCRIBE_COLUMNS (ICURNUM, ICOLCOUNT, DESCTABREC);
-- set title of html
CLOBHTML := HTF.TITLE (TO_CHAR (SYSDATE, 'DD/MM/YYYY HH24:MI:SS')) || CHR (10);
-- add custom message to header
CLOBHTML := CLOBHTML || HTF.HEADER (3, TO_CHAR (CLOBMESSAGE), 'CENTER') || CHR (10);
CLOBHTML := CLOBHTML || HTF.HEADER (3, 'Tarih: ' || TO_CHAR (SYSDATE, 'DD/MM/YYYY'), 'CENTER') || CHR (10);
-- open table
CLOBHTML :=
CLOBHTML || HTF.TABLEOPEN ('BORDER=1',
'CENTER',
NULL,
NULL,
'CELLPADDING=0') || CHR (10);
-- new row for table header
CLOBHTML := CLOBHTML || HTF.TABLEROWOPEN || CHR (10);
-- loop all columns and set table headers
FOR ICOUNT IN DESCTABREC.FIRST .. ICOLCOUNT
LOOP
CLOBHTML := CLOBHTML || HTF.TABLEDATA (HTF.STRONG (DESCTABREC (ICOUNT).COL_NAME), 'CENTER') || CHR (10);
END LOOP;
-- close row for table header
CLOBHTML := CLOBHTML || HTF.TABLEROWCLOSE || CHR (10);
-- fetch all rows in the table and prepare table
LOOP
ICOUNT := DBMS_SQL.FETCH_ROWS (ICURNUM);
EXIT WHEN ICOUNT = 0;
CLOBHTML := CLOBHTML || HTF.TABLEROWOPEN || CHR (10);
CLOBTEMP := CONCATENATE_ROW (ICURNUM);
CLOBHTML := CLOBHTML || CLOBTEMP || HTF.TABLEROWCLOSE || CHR (10);
END LOOP;
-- close table
CLOBHTML := CLOBHTML || HTF.TABLECLOSE;
plib.o_log.log(1, 4, 'Completed', v_pkgc || '.' || v_proc, NULL, ICURNUM);
RETURN CLOBHTML;
EXCEPTION
WHEN OTHERS THEN
plib.o_log.log(SQLCODE, 1, SQLERRM, v_proc, NULL, ICURNUM);
raise_application_error(SQLCODE, SQLERRM);
END;
FUNCTION TABLE_TO_HTML (VTABLENAME VARCHAR2, CLOBMESSAGE CLOB)
RETURN CLOB
IS
CLOBDATA CLOB := NULL;
BEGIN
CLOBDATA := SQL_TO_HTML ('SELECT /*+ PARALLEL */ * FROM ' || VTABLENAME , CLOBMESSAGE);
RETURN CLOBDATA;
END;
FUNCTION SQL_TO_HTML (VSQLSTATEMENT VARCHAR2, CLOBMESSAGE CLOB)
RETURN CLOB
IS
ICURNUM INTEGER;
CUROBJ REFCUR;
CLOBDATA CLOB := NULL;
BEGIN
OPEN CUROBJ FOR VSQLSTATEMENT;
ICURNUM := DBMS_SQL.TO_CURSOR_NUMBER (CUROBJ);
DEFINE_COLUMNS (ICURNUM);
CLOBDATA := CREATE_HTML (CLOBMESSAGE, ICURNUM);
RETURN CLOBDATA;
END;
PROCEDURE SEND_MAIL_HTML (P_TO IN VARCHAR2,
P_FROM IN VARCHAR2,
P_SUBJECT IN VARCHAR2,
P_HTML_MSG IN CLOB)
AS
L_MAIL_CONN UTL_SMTP.CONNECTION;
L_BOUNDARY VARCHAR2 (50) := '----=*#abc1234321cba#*=';
P_SMTP_HOST VARCHAR2 (50) := '10.200.aaa.bbb';
P_SMTP_PORT NUMBER := 25;
I_CNT NUMBER;
V_TO VARCHAR2 (200);
V_TO_STR VARCHAR2 (4000);
N_S_IND NUMBER;
N_E_IND NUMBER;
N_OFFSET NUMBER;
N_AMOUNT NUMBER := 1900;
BEGIN
-- Initialize Log Variables
v_proc := 'SEND_MAIL_HTML(' || P_TO || '/' || P_FROM || ')';
plib.o_log := log_type.initialize('YES', v_job_module, v_job_owner, v_pkgc , v_proc);
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);
I_CNT := LENGTH (P_TO) - LENGTH (REPLACE (P_TO, ',', ''));
IF I_CNT = 0
THEN
V_TO := TRIM (P_TO);
V_TO_STR := TRIM (P_TO);
UTL_SMTP.RCPT (L_MAIL_CONN, TRIM (P_TO));
ELSE
FOR I IN 1 .. I_CNT + 1
LOOP
IF N_S_IND IS NULL
THEN
N_S_IND := 1;
N_E_IND :=
INSTR (P_TO,',',1,I);
ELSIF I = I_CNT + 1
THEN
N_S_IND :=INSTR (P_TO,',',1,I - 1)+ 1;
N_E_IND := LENGTH (P_TO) + 1;
ELSE
N_S_IND :=INSTR (P_TO,',',1,I - 1)+ 1;
N_E_IND :=INSTR (P_TO,',',1,I);
END IF;
V_TO := TRIM (SUBSTR (P_TO, N_S_IND, N_E_IND - N_S_IND));
V_TO_STR := V_TO_STR || V_TO || '; ';
UTL_SMTP.RCPT (L_MAIL_CONN, V_TO);
END LOOP;
V_TO_STR := SUBSTR (V_TO_STR, 1, LENGTH (V_TO_STR) - 2);
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: ' || V_TO_STR || 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_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-9"' || UTL_TCP.CRLF || UTL_TCP.CRLF);
N_OFFSET := 1;
WHILE N_OFFSET < DBMS_LOB.GETLENGTH (P_HTML_MSG)
LOOP
UTL_SMTP.WRITE_DATA (L_MAIL_CONN, DBMS_LOB.SUBSTR (P_HTML_MSG, N_AMOUNT, N_OFFSET));
N_OFFSET := N_OFFSET + N_AMOUNT;
END LOOP;
--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);
plib.o_log.log(1, 4, 'Completed', v_pkgc || '.' || v_proc, NULL, NULL);
EXCEPTION
WHEN OTHERS THEN
plib.o_log.log(SQLCODE, 1, SQLERRM, v_proc, NULL, NULL);
raise_application_error(SQLCODE, SQLERRM);
END;
PROCEDURE SEND_TABLE_AS_HTML_MAIL (VTOPERSON VARCHAR2,
VFROMPERSON VARCHAR2,
VSUBJECT VARCHAR2,
VTABLENAME VARCHAR2,
CLOBMESSAGE CLOB)
IS
BEGIN
-- Initialize Log Variables
v_proc := 'SEND_TABLE_AS_HTML_MAIL';
plib.o_log := log_type.initialize('YES', v_job_module, v_job_owner, v_pkgc , v_proc);
SEND_MAIL_HTML (VTOPERSON,
VFROMPERSON,
VSUBJECT,
TABLE_TO_HTML (VTABLENAME, CLOBMESSAGE));
EXCEPTION
WHEN OTHERS THEN
plib.o_log.log(SQLCODE, 1, SQLERRM, v_proc, NULL, VTOPERSON || '/' || VFROMPERSON || '/' || VTABLENAME);
raise_application_error(SQLCODE, SQLERRM);
END;
PROCEDURE SEND_SQL_AS_HTML_MAIL (VTOPERSON VARCHAR2,
VFROMPERSON VARCHAR2,
VSUBJECT VARCHAR2,
VSQLSTATEMENT VARCHAR2,
CLOBMESSAGE CLOB)
IS
BEGIN
-- Initialize Log Variables
v_proc := 'SEND_SQL_AS_HTML_MAIL';
plib.o_log := log_type.initialize('YES', v_job_module, v_job_owner, v_pkgc , v_proc);
SEND_MAIL_HTML (VTOPERSON,
VFROMPERSON,
VSUBJECT,
SQL_TO_HTML (VSQLSTATEMENT, CLOBMESSAGE));
EXCEPTION
WHEN OTHERS THEN
plib.o_log.log(SQLCODE, 1, SQLERRM, v_proc, NULL, VTOPERSON || '/' || VFROMPERSON || '/' || VSQLSTATEMENT);
raise_application_error(SQLCODE, SQLERRM);
END;
END PKG_TABLE_TO_HTML;
/
Yorumlar
BEGIN
DDSBASE.PKG_TABLE_TO_HTML.SEND_TABLE_AS_HTML_MAIL (
'eyup.ozkara@', --to_list
'eyup.ozkara@', --from list
'Comparison', --mail subject
'DDSBASE.MV_CALL_NETWORK_TP_SK', --view/table name
''); --mail html header
END;