Upload
massimo-cenci
View
178
Download
0
Embed Size (px)
DESCRIPTION
Oracle All-in-One (All you need in only one slide) OAiO1 - How to send mail with attach using Oracle plsql The goal is to have in a single image all the objects involved in a common need of a Data Warehouse in Oracle environment.
Citation preview
Oracle All-in-OneOracle All-in-One(All you need in only one slide)(All you need in only one slide)
OAiO1 - How to send mail with attach OAiO1 - How to send mail with attach using Oracle pl/sqlusing Oracle pl/sql
The goal is to have in a single image all the objects involvedThe goal is to have in a single image all the objects involvedin a common need of a Data Warehouse in Oracle environment.in a common need of a Data Warehouse in Oracle environment.
In this slide you can :In this slide you can :see how to access or configure these objectssee how to access or configure these objects
reproduce the solution with a simple copy and pastereproduce the solution with a simple copy and paste
leaves the reader with the security considerationsleaves the reader with the security considerationsleave to the reader the theoretical insightsleave to the reader the theoretical insights
Any feedback to make it more complete solution is acceptableAny feedback to make it more complete solution is acceptable
XML DB Technology
Email package(UTL_MAIL)
ACL view entries(DBA_NETWORK_ACLS)
ACL privileges view (DBA_NETWORK_ACL_PRIVILEGES)
Oracle Parameters
1 – Check XML DB Installation
sqlplus / as sysdbadescribe resource_view;Name Null? Type------- -------- ----------------------------RES SYS.XMLTYPE(XMLSchema "http: //xmlns.oracle.com/xdb/XDBRe source.xsd" Element "Resourc e")select * from all_users where username='XDB';(It must return 1 row. Both conditions must be true)
1a – Install XML DB
See Install Documentation
2 – Check existance
sqlplus / as sysdbaselect object_name,owner,object_type,status from dba_objects where object_name = 'UTL_MAIL';
2a - Create user (eventually)
Grant connect,resource,dba to &&1 identified by &&2;
2b – Install packages
@&&5\rdbms\admin\utlmail.sql@&&5\rdbms\admin\prvtmail.plbgrant execute on utl_mail to &&1;
4 – Check setting
SQLPLUS / AS SYSDBAsho parameters smtp_out_server;(It must be setted to &&4)
4a– Set the parameteralter system set smtp_out_server='&&4' scope=both;
Substitutions
&&1 = Oracle User that send the email&&2 = Oracle User password&&3 = An ACL name of your choice (e.g. utl_mail.xml)&&4 = Email Server (e.g. acme.ita.com)&&5 = Oracle Home path&&6 = Email receiver (e.g . [email protected])
Send email procedure(p_email)
call
send
5a – Create ACL entry5b – Assign ACL network
sqlplus / as sysdbabegin dbms_network_acl_admin.create_acl ( acl => '&&3', description => 'Allow mail to be send', principal => '&&1', is_grant => TRUE, privilege => 'connect'); commit;end;/begin dbms_network_acl_admin.assign_acl( acl => '&&3', host => '&&4'); commit; end;/
5 – Find values
sqlplus / as sysdbaselect * from dba_network_acls;
6 – Find values
sqlplus / as sysdbaselect * from dba_network_acl_privileges;(exists also user_network_acl_privileges)
6b – Delete ACL privilege
sqlplus / as sysdbabegin dbms_network_acl_admin.delete_privilege( acl => '&&3', principal => '&&1'); commit; end;/
5d – Deassign ACL network5c – DeleteACL entry
sqlplus / as sysdbabegin dbms_network_acl_admin.unassign_acl( host => '&&4'); commit; end;/begin dbms_network_acl_admin.drop_acl( acl => '&&3'); commit; end;/
7 – Create procedure
sqlplus &&1/&&2create or replace procedure p_email( p_sender varchar2 ,p_recipients varchar2 ,p_subject varchar2 ,p_message varchar2 ,p_dir varchar2 default null ,p_file varchar2 default null) is v_fh utl_file.file_type; v_rfile raw(32767); v_flen number; v_bsize number; v_ex boolean;begin if (p_dir is null) then utl_mail.send( sender => p_sender ,recipients => p_recipients ,subject => p_subject ,message => p_message ); else utl_file.fgetattr(p_dir, p_file, v_ex, v_flen, v_bsize); v_fh := utl_file.fopen(p_dir, p_file, 'r'); utl_file.get_raw(v_fh,v_rfile, v_flen); utl_file.fclose(v_fh); utl_mail.send_attach_raw( sender => p_sender ,recipients => p_recipients ,subject => p_subject ,message => p_message ,attachment => v_rfile ,att_inline => FALSE ,att_filename => p_file ); end if;end;/
8 – Run procedure
sqlplus &&1/&&2exec p_email('Dba','&&6','OAiO1_Test','Hello world', 'DATA_PUMP_DIR', 'dp.log');
OAiO1 - How to send mail with attach using Oracle pl/sqlOracle All-in-One(All you need in only one slide)
6a – Add ACL privilege
sqlplus / as sysdbabegin dbms_network_acl_admin.add_privilege ( acl => '&&3', principal => '&&1', is_grant => TRUE, privilege => 'resolve' ); commit; end;/(This privilege if you need host IP resolution)
DATA_PUMP_DIR
dp.log
3 – Access to directory
sqlplus / as sysdbagrant read,write on directory DATA_PUMP_DIR to &&1;(It is an example: the directory and the file dp.log are always present after installation)
send_attach_raw
Oracle All-in-One By Massimo Cenci