1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| PROCEDURE send (l_html IN VARCHAR2, l_priorite in VARCHAR2) IS
p_smtp_hostname VARCHAR2 (30);
p_smtp_portnum VARCHAR2 (4);
p_to varchar2 (150) := 'adresse@destinataire.fr';
p_from varchar2 (150) := 'adresse@expediteur.fr';
p_subject varchar2 (150) := 'Sujet de test';
p_text varchar2 (4000) := 'Ne pas répondre à ce mail';
p_html varchar2 (32767) := l_html;
p_priorite varchar2(6) := l_priorite; -- Level of priority. Value = 'High', 'Normal' or 'Low'
p_header varchar2 (32767) default null; -- this char will be the header
l_body_html clob := empty_clob; --This LOB will be the email message
l_offset number; -- use to keep the length of the email
l_ammount number; -- use to send the email in 1900 byte chunks
l_connection utl_smtp.connection;
BEGIN
-- select ip and port number for the smtp protocole
select substr(value,0,instr(value, ':')-1)
, substr(value,instr(value, ':')+1)
into p_smtp_hostname, p_smtp_portnum
from v$parameter
where upper(name) like '%SMTP%';
utl_smtp.helo( l_connection, p_smtp_hostname );
utl_smtp.mail( l_connection, p_from );
utl_smtp.rcpt( l_connection, p_to );
-- Building the header
p_header := p_header || 'MIME-Version: 1.0' || chr(13) || chr(10);
p_header := p_header || 'To: ' || p_to || chr(13) || chr(10);
p_header := p_header || 'From: ' || p_from || chr(13) || chr(10);
p_header := p_header || 'Subject: ' || p_subject || chr(13) || chr(10);
p_header := p_header || 'Reply-To: ' || p_from || chr(13) || chr(10);
p_header := p_header || 'Importance: '|| p_priorite || chr(13) || chr(10);
p_header := p_header || 'Content-Type: text/html; charset=iso-8859-1' || chr(13) || chr(10); -- charset must be modify with the correct value
-- Building the email itself
dbms_lob.createtemporary( l_body_html, false, 10 );
dbms_lob.write(l_body_html,length(p_header),1,p_header);
l_offset := dbms_lob.getlength(l_body_html) + 1;
dbms_lob.write(l_body_html,length(p_html),l_offset,p_html);
-- Send the email in 1900 byte chunks to UTL_SMTP
l_offset := 1;
l_ammount := 1900;
utl_smtp.open_data(l_connection);
while l_offset < dbms_lob.getlength(l_body_html) loop
utl_smtp.write_raw_data(l_connection,utl_raw.cast_to_raw(dbms_lob.substr(l_body_html,l_ammount,l_offset)));
l_offset := l_offset + l_ammount ;
l_ammount := least(1900,dbms_lob.getlength(l_body_html) - l_ammount);
end loop;
utl_smtp.close_data(l_connection);
utl_smtp.quit( l_connection );
dbms_lob.freetemporary(l_body_html);
EXCEPTION
WHEN NO_DATA_FOUND THEN
raise_application_error (-20100, 'Invalid Registration');
WHEN UTL_SMTP.invalid_operation THEN
DBMS_OUTPUT.put_line (' Invalid Operation in SMTP transaction.');
RAISE;
WHEN UTL_SMTP.transient_error THEN
DBMS_OUTPUT.put_line (' Temporary problems with sending email - try again later.');
RAISE;
WHEN UTL_SMTP.permanent_error THEN
DBMS_OUTPUT.put_line (' Errors in code for SMTP transaction.');
RAISE;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END send; |
Partager