PROCEDURE Send_Email (
efrom IN VARCHAR2,
eto IN VARCHAR2,
esubject IN VARCHAR2,
ebody IN VARCHAR2,
ecc IN CHAR := NULL,
ebcc IN CHAR := NULL,
p_host IN VARCHAR2,
p_port IN NUMBER
)
IS
---------------------------
-- PRIVATE VARIABLES
---------------------------
l_proc_name VARCHAR2(50) := 'Send_Email';
BEGIN
DECLARE
msg VARCHAR2 (14000);
conn UTL_SMTP.connection;
crlf VARCHAR2 (2) := CHR (13) || CHR (10);
TYPE registro IS RECORD (
NAME VARCHAR2 (200),
email VARCHAR2 (200)
);
TYPE addresslist_tab IS TABLE OF registro
INDEX BY BINARY_INTEGER;
addrcnt BINARY_INTEGER := 0;
myfrom addresslist_tab;
myto addresslist_tab;
mycc addresslist_tab;
mybcc addresslist_tab;
cmyfrom BINARY_INTEGER := 0;
cmyto BINARY_INTEGER := 0;
cmycc BINARY_INTEGER := 0;
cmybcc BINARY_INTEGER := 0;
mes NUMBER (2);
nmes CHAR (3);
rawdata RAW (32767);
msgaux VARCHAR2 (14000);
aux VARCHAR2 (4000);
-- following is the function used to seperate the email addresses from the efrom and eto string of multiple addresses.
FUNCTION separate (inemailx VARCHAR2)
RETURN addresslist_tab
IS
l_func_name VARCHAR2(50) := 'separate';
BEGIN
DECLARE
inemail VARCHAR2 (4000);
p NUMBER;
rr addresslist_tab;
p1 VARCHAR2 (512);
p2 VARCHAR2 (512);
n1 VARCHAR2 (100);
n2 VARCHAR2 (100);
PROCEDURE getname (inadd VARCHAR2)
IS
BEGIN
DECLARE
pp NUMBER;
qq NUMBER;
BEGIN
pp := INSTR (inadd, '<');
qq := INSTR (inadd, '>');
IF pp = 0
THEN
n1 := NULL;
n2 := inadd;
ELSE
n1 := LTRIM (RTRIM (SUBSTR (inadd, 1, pp - 1)));
n2 := LTRIM (RTRIM (SUBSTR (inadd, pp + 1, qq - pp - 1)));
END IF;
END;
END;
BEGIN
addrcnt := 0;
IF inemailx IS NULL
THEN
RETURN (rr);
END IF;
inemail := REPLACE (inemailx, ';', ',');
p := INSTR (inemail, ',');
IF p = 0
THEN
p := INSTR (inemail, ';');
END IF;
IF p = 0
THEN
getname (inemail);
rr (1).NAME := n1;
rr (1).email := n2;
addrcnt := 1;
ELSE
p2 := inemail;
LOOP
p1 := LTRIM (RTRIM (SUBSTR (p2, 1, p - 1)));
IF NOT p1 IS NULL
THEN
addrcnt := addrcnt + 1;
getname (p1);
rr (addrcnt).NAME := n1;
rr (addrcnt).email := n2;
END IF;
p2 := SUBSTR (p2, p + 1, 2000);
IF NOT p2 IS NULL
THEN
p := INSTR (p2, ',');
IF p = 0
THEN
p := INSTR (p2, ';');
END IF;
IF p = 0
THEN
addrcnt := addrcnt + 1;
getname (LTRIM (RTRIM (p2)));
rr (addrcnt).NAME := n1;
rr (addrcnt).email := n2;
EXIT;
END IF;
ELSE
EXIT;
END IF;
END LOOP;
END IF;
RETURN (rr);
END;
EXCEPTION
WHEN OTHERS THEN
l_error := SQLERRM;
FND_FILE.PUT_LINE (fnd_file.LOG,'Error in '||g_package_name||'.'||l_proc_name||'.'||l_func_name||' function due to:: '||l_error);
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name||'.'||l_func_name,'Error in function due to :: '||l_error);
END IF;
END;
-- Following code is used to send the email using UTL_SMTP package
BEGIN
msg := NULL;
myfrom := separate (efrom);
cmyfrom := addrcnt;
myto := separate (eto);
cmyto := addrcnt;
-- if ecc is not null and trim(ecc)'' then mycc:=separate(ecc); cmycc:=addrcnt; else cmycc:=0; end if;
--if ebcc is not null and trim(ebcc)'' then mybcc:=separate(ebcc); cmybcc:=addrcnt; else cmybcc:=0; end if;
IF ecc IS NOT NULL AND LENGTH (TRIM (ecc)) > 0
THEN
mycc := separate (ecc);
cmycc := addrcnt;
ELSE
cmycc := 0;
END IF;
IF ebcc IS NOT NULL AND LENGTH (TRIM (ebcc)) > 0
THEN
mybcc := separate (ebcc);
cmybcc := addrcnt;
ELSE
cmybcc := 0;
END IF;
conn := UTL_SMTP.open_connection (p_host,p_port);
UTL_SMTP.helo (conn, p_host);
UTL_SMTP.mail (conn, myfrom (1).email);
FOR x IN 1 .. cmyto
LOOP
UTL_SMTP.rcpt (conn, myto (x).email);
END LOOP;
IF cmycc = 0
THEN
FOR x IN 1 .. cmycc
LOOP
UTL_SMTP.rcpt (conn, mycc (x).email);
END LOOP;
END IF;
DBMS_OUTPUT.put_line (' hola 1');
IF cmybcc = 0
THEN
FOR x IN 1 .. cmybcc
LOOP
UTL_SMTP.rcpt (conn, mybcc (x).email);
END LOOP;
END IF;
DBMS_OUTPUT.put_line (' hola 2');
-- mes := TO_CHAR (SYSDATE, 'mm');
-- nmes :=
-- SUBSTR ('JanFebMarAprMayJunJulAugSepOctNovDec', (mes - 1) * 3 + 1,
-- 3);
-- msg :=
-- 'Date: '
-- || TO_CHAR (SYSDATE, 'dd ')
-- || nmes
-- || TO_CHAR (SYSDATE, ' yyyy hh24:mi:ss')
-- || ' -0500'
-- || crlf;
IF cmyfrom <> 0
THEN
IF myfrom (1).NAME IS NOT NULL
THEN
msg :=
msg
|| 'From: '
|| CHR (34)
|| myfrom (1).NAME
|| CHR (34)
|| ' <'-->'
|| crlf;
ELSE
IF myfrom (1).email IS NOT NULL
THEN
msg := msg || 'From: ' || myfrom (1).email || crlf;
ELSE
msg :=
msg
|| 'From: '
|| CHR (34)
|| 'Default'
|| CHR (34)
|| '
|| crlf;
END IF;
END IF;
ELSE
msg :=
msg
|| 'From: '
|| CHR (34)
|| 'Default'
|| CHR (34)
|| '
|| crlf;
END IF;
FOR x IN 1 .. cmyto
LOOP
IF myto (x).NAME IS NOT NULL
THEN
msg :=
msg
|| 'To: '
|| CHR (34)
|| myto (x).NAME
|| CHR (34)
|| ' <'-->'
|| crlf;
ELSE
msg := msg || 'To: ' || myto (x).email || crlf;
END IF;
END LOOP;
DBMS_OUTPUT.put_line (' hola 3');
IF cmycc = 0
THEN
FOR x IN 1 .. cmycc
LOOP
IF mycc (x).NAME IS NOT NULL
THEN
-- msg:=msg||'To: '||chr(34)||mycc(x).name||chr(34)||' <'>'||crlf;
msg :=
msg
|| 'Cc: '
|| CHR (34)
|| mycc (x).NAME
|| CHR (34)
|| ' <'-->'
|| crlf;
ELSE
msg := msg || 'Cc: ' || mycc (x).email || crlf;
END IF;
END LOOP;
DBMS_OUTPUT.put_line (' hola 4');
END IF;
IF cmybcc = 0
THEN
FOR x IN 1 .. cmybcc
LOOP
IF mybcc (x).NAME IS NOT NULL
THEN
-- msg:=msg||'To: '||chr(34)||mybcc(x).name||chr(34)||' <'>'||crlf;
msg :=
msg
|| 'Bcc: '
|| CHR (34)
|| mybcc (x).NAME
|| CHR (34)
|| ' <'-->'
|| crlf;
ELSE
msg := msg || 'Bcc: ' || mybcc (x).email || crlf;
END IF;
END LOOP;
END IF;
DBMS_OUTPUT.put_line (' hola 5');
msg := msg || 'Precedence: bulk' || crlf;
msg := msg || 'Subject: ' || esubject || crlf;
--
--msg:=msg||'MIME-Version: 1.0'||crlf;
--msg:=msg||'Content-Type: text/plain; charset=utf-8'||crlf;
--msg:=msg||'Content-Transfer-Encoding: 8bit'||crlf;
--
msg := msg || '' || crlf;
/* 12/11/2007*/
msgaux := msg || ebody || ' ' || crlf;
DBMS_OUTPUT.put_line (' hola 6');
UTL_SMTP.open_data (conn);
DBMS_OUTPUT.put_line (' hola 7');
UTL_SMTP.write_data (conn, msg);
msg := ebody || ' ' || crlf;
rawdata := UTL_RAW.cast_to_raw (msg);
DBMS_OUTPUT.put_line (' hola 8');
--utl_smtp.write_data(conn, msg);
UTL_SMTP.write_raw_data (conn, rawdata);
DBMS_OUTPUT.put_line (' hola 9');
UTL_SMTP.close_data (conn);
--msg:=ebody;
--utl_smtp.data(conn, msg);
UTL_SMTP.quit (conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error
THEN
l_error := SQLERRM;
dbms_output.put_line('Failed to send mail due to the following error: ' || l_error);
FND_FILE.PUT_LINE (fnd_file.LOG,'Failed to send mail due to the following error:: '||l_error);
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name,'Failed to send mail due to the following error :: '||l_error);
END IF;
BEGIN
utl_smtp.quit(conn);
EXCEPTION
WHEN utl_smtp.transient_error OR utl_smtp.permanent_error
THEN
l_error := SQLERRM;
dbms_output.put_line('Failed to send mail due to the following error: ' || l_error);
FND_FILE.PUT_LINE (fnd_file.LOG,'Failed to send mail due to the following error:: '||l_error);
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name,'Failed to send mail due to the following error :: '||l_error);
END IF;
WHEN OTHERS THEN
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name,'Failed to send mail due to the following error :: '||l_error);
END IF;
END;
WHEN OTHERS
THEN
BEGIN
BEGIN
msgaux :=
'From: '
|| efrom
|| crlf
|| 'To: '
|| eto
|| crlf
|| 'Subject :'
|| esubject
|| crlf
|| 'Body: '
|| ebody
|| crlf
|| 'CC: '
|| NVL (ecc, ' ')
|| crlf
|| 'BCC: '
|| NVL (ebcc, ' ');
aux := TO_CHAR (SQLCODE) || ' - ' || SQLERRM;
dbms_output.put_line('Error: ' || aux);
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
l_error := SQLERRM;
FND_FILE.PUT_LINE (fnd_file.LOG,'Failed to send mail due to the following error :: '||l_error);
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name,'Failed to send mail due to the following error :: '||l_error);
END IF;
END;
UTL_SMTP.quit (conn);
EXCEPTION
WHEN OTHERS
THEN
l_error := SQLERRM;
FND_FILE.PUT_LINE (fnd_file.LOG,'Failed to send mail due to the following error :: '||l_error);
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name,'Failed to send mail due to the following error :: '||l_error);
END IF;
END;
END;
EXCEPTION
WHEN OTHERS THEN
l_error := SQLERRM;
FND_FILE.PUT_LINE (fnd_file.LOG,'Failed to send mail due to the following error :: '||l_error);
IF g_debug_flag = 'Y'
THEN
XX_OE_NEW_CREDIT_CHECK_PKG.DEBUG(g_package_name||'.'||l_proc_name,'Failed to send mail due to the following error :: '||l_error);
END IF;
END Send_Email;
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.