How to send emails using oracle ?
A. First create the package :
CREATE OR REPLACE PACKAGE TOTO."PAC_MANAGE_MAIL" AS
TYPE ARRAY IS TABLE OF VARCHAR2(255);
G_MAIL_CONN utl_smtp.connection;
G_MAILHOST VARCHAR2(64) := 'xxxxxx'; ------------------->email server IP
G_CRLF CHAR(2) DEFAULT CHR(13)||CHR(10);
PROCEDURE PRO_SEND_MAIL (PC_SENDER VARCHAR2,
PC_FROM VARCHAR2,
PC_TO ARRAY DEFAULT ARRAY(),
PC_CC ARRAY DEFAULT ARRAY(),
PC_BCC ARRAY DEFAULT ARRAY(),
PC_SUBJECT VARCHAR2,
PC_TEXT VARCHAR2);
PROCEDURE PRO_WRITE_DATA (PC_TEXT VARCHAR2);
FUNCTION FUN_ADDRESS_EMAIL ( PC_CHAINE VARCHAR2, PC_RECIPIENTS ARRAY)
RETURN VARCHAR2 ;
END;
/
CREATE OR REPLACE PACKAGE BODY TOTO.PAC_MANAGE_MAIL" AS
PROCEDURE PRO_SEND_MAIL (PC_SENDER VARCHAR2,
PC_FROM VARCHAR2,
PC_TO ARRAY DEFAULT ARRAY(),
PC_CC ARRAY DEFAULT ARRAY(),
PC_BCC ARRAY DEFAULT ARRAY(),
PC_SUBJECT VARCHAR2,
PC_TEXT VARCHAR2)IS
WC_TO_LIST LONG;
WC_CC_LIST LONG;
WC_BCC_LIST LONG;
BEGIN
G_MAIL_CONN := utl_smtp.open_connection(G_MAILHOST, 25);
utl_smtp.helo(G_MAIL_CONN, G_MAILHOST);
utl_smtp.mail(G_MAIL_CONN, PC_SENDER); WC_TO_LIST := FUN_ADDRESS_EMAIL('To:', PC_TO);
WC_CC_LIST := FUN_ADDRESS_EMAIL('Cc:', PC_CC);
WC_BCC_LIST := FUN_ADDRESS_EMAIL('Bcc:', PC_BCC); utl_smtp.open_data(G_MAIL_CONN);
PRO_WRITE_DATA ('Date : '||TO_CHAR( SYSDATE, 'dd Mon yy hh24:mi:ss' ));
PRO_WRITE_DATA ('From : '||NVL( PC_FROM, PC_SENDER ));
PRO_WRITE_DATA ('Subject : '||NVL( PC_SUBJECT, 'No subject' ));
PRO_WRITE_DATA (WC_TO_LIST);
PRO_WRITE_DATA (WC_CC_LIST); utl_smtp.write_data(G_MAIL_CONN, ' '||G_CRLF);
utl_smtp.write_data(G_MAIL_CONN, PC_TEXT);
utl_smtp.close_data(G_MAIL_CONN);
utl_smtp.quit(G_MAIL_CONN);
END; -- ***************************************************************************************
-- ***************************************************************************************
PROCEDURE PRO_WRITE_DATA (PC_TEXT VARCHAR2)IS
BEGIN
IF (PC_TEXT IS NOT NULL)THEN
utl_smtp.write_data(G_MAIL_CONN, PC_TEXT||G_CRLF);
END IF;
END; -- ***************************************************************************************
-- ***************************************************************************************
FUNCTION FUN_ADDRESS_EMAIL ( PC_CHAINE VARCHAR2,
PC_RECIPIENTS ARRAY) RETURN VARCHAR2 IS
WN_RECIPIENT LONG;
BEGIN FOR i IN 1 .. PC_RECIPIENTS.COUNT LOOP
utl_smtp.rcpt(G_MAIL_CONN, PC_RECIPIENTS(i) );
IF(WN_RECIPIENT IS NULL)THEN
WN_RECIPIENT := PC_CHAINE || PC_RECIPIENTS(i);
ELSE
WN_RECIPIENT := WN_RECIPIENT || ', '||PC_RECIPIENTS(i);
END IF;
END LOOP; RETURN WN_RECIPIENT;
END;END;
/
B. If you use 10g or 11g add some rigths to the user TOTO :
conn sys/...begin
dbms_network_acl_admin.create_acl (
acl => 'utl_mail.xml',
description => 'Allow mail to be send',
principal => 'TOTO',
is_grant => TRUE,
privilege => 'connect'
);
commit;
end;
begin
dbms_network_acl_admin.add_privilege (
acl => 'utl_mail.xml',
principal => 'TOTO',
is_grant => TRUE,
privilege => 'resolve'
);
commit;
end;
begin
dbms_network_acl_admin.assign_acl(
acl => 'utl_mail.xml',
host => 'xxxxx' ------------------->email server IP
);
commit;
end;
C. How to send emails ?
PAC_MANAGE_MAIL.PRO_SEND_MAIL (
PC_SENDER =>'xx@xx',
PC_FROM =>'xx@xx',
PC_TO =>('aa@bb','cc@dd'),
PC_SUBJECT =>'The subject',
PC_TEXT =>'The text') ;
A. First create the package :
CREATE OR REPLACE PACKAGE TOTO."PAC_MANAGE_MAIL" AS
TYPE ARRAY IS TABLE OF VARCHAR2(255);
G_MAIL_CONN utl_smtp.connection;
G_MAILHOST VARCHAR2(64) := 'xxxxxx'; ------------------->email server IP
G_CRLF CHAR(2) DEFAULT CHR(13)||CHR(10);
PROCEDURE PRO_SEND_MAIL (PC_SENDER VARCHAR2,
PC_FROM VARCHAR2,
PC_TO ARRAY DEFAULT ARRAY(),
PC_CC ARRAY DEFAULT ARRAY(),
PC_BCC ARRAY DEFAULT ARRAY(),
PC_SUBJECT VARCHAR2,
PC_TEXT VARCHAR2);
PROCEDURE PRO_WRITE_DATA (PC_TEXT VARCHAR2);
FUNCTION FUN_ADDRESS_EMAIL ( PC_CHAINE VARCHAR2, PC_RECIPIENTS ARRAY)
RETURN VARCHAR2 ;
END;
/
CREATE OR REPLACE PACKAGE BODY TOTO.PAC_MANAGE_MAIL" AS
PROCEDURE PRO_SEND_MAIL (PC_SENDER VARCHAR2,
PC_FROM VARCHAR2,
PC_TO ARRAY DEFAULT ARRAY(),
PC_CC ARRAY DEFAULT ARRAY(),
PC_BCC ARRAY DEFAULT ARRAY(),
PC_SUBJECT VARCHAR2,
PC_TEXT VARCHAR2)IS
WC_TO_LIST LONG;
WC_CC_LIST LONG;
WC_BCC_LIST LONG;
BEGIN
G_MAIL_CONN := utl_smtp.open_connection(G_MAILHOST, 25);
utl_smtp.helo(G_MAIL_CONN, G_MAILHOST);
utl_smtp.mail(G_MAIL_CONN, PC_SENDER); WC_TO_LIST := FUN_ADDRESS_EMAIL('To:', PC_TO);
WC_CC_LIST := FUN_ADDRESS_EMAIL('Cc:', PC_CC);
WC_BCC_LIST := FUN_ADDRESS_EMAIL('Bcc:', PC_BCC); utl_smtp.open_data(G_MAIL_CONN);
PRO_WRITE_DATA ('Date : '||TO_CHAR( SYSDATE, 'dd Mon yy hh24:mi:ss' ));
PRO_WRITE_DATA ('From : '||NVL( PC_FROM, PC_SENDER ));
PRO_WRITE_DATA ('Subject : '||NVL( PC_SUBJECT, 'No subject' ));
PRO_WRITE_DATA (WC_TO_LIST);
PRO_WRITE_DATA (WC_CC_LIST); utl_smtp.write_data(G_MAIL_CONN, ' '||G_CRLF);
utl_smtp.write_data(G_MAIL_CONN, PC_TEXT);
utl_smtp.close_data(G_MAIL_CONN);
utl_smtp.quit(G_MAIL_CONN);
END; -- ***************************************************************************************
-- ***************************************************************************************
PROCEDURE PRO_WRITE_DATA (PC_TEXT VARCHAR2)IS
BEGIN
IF (PC_TEXT IS NOT NULL)THEN
utl_smtp.write_data(G_MAIL_CONN, PC_TEXT||G_CRLF);
END IF;
END; -- ***************************************************************************************
-- ***************************************************************************************
FUNCTION FUN_ADDRESS_EMAIL ( PC_CHAINE VARCHAR2,
PC_RECIPIENTS ARRAY) RETURN VARCHAR2 IS
WN_RECIPIENT LONG;
BEGIN FOR i IN 1 .. PC_RECIPIENTS.COUNT LOOP
utl_smtp.rcpt(G_MAIL_CONN, PC_RECIPIENTS(i) );
IF(WN_RECIPIENT IS NULL)THEN
WN_RECIPIENT := PC_CHAINE || PC_RECIPIENTS(i);
ELSE
WN_RECIPIENT := WN_RECIPIENT || ', '||PC_RECIPIENTS(i);
END IF;
END LOOP; RETURN WN_RECIPIENT;
END;END;
/
B. If you use 10g or 11g add some rigths to the user TOTO :
conn sys/...begin
dbms_network_acl_admin.create_acl (
acl => 'utl_mail.xml',
description => 'Allow mail to be send',
principal => 'TOTO',
is_grant => TRUE,
privilege => 'connect'
);
commit;
end;
begin
dbms_network_acl_admin.add_privilege (
acl => 'utl_mail.xml',
principal => 'TOTO',
is_grant => TRUE,
privilege => 'resolve'
);
commit;
end;
begin
dbms_network_acl_admin.assign_acl(
acl => 'utl_mail.xml',
host => 'xxxxx' ------------------->email server IP
);
commit;
end;
C. How to send emails ?
PAC_MANAGE_MAIL.PRO_SEND_MAIL (
PC_SENDER =>'xx@xx',
PC_FROM =>'xx@xx',
PC_TO =>('aa@bb','cc@dd'),
PC_SUBJECT =>'The subject',
PC_TEXT =>'The text') ;
Aucun commentaire:
Enregistrer un commentaire