Saltar al contenido
Codifíca.me | Desarrollo web | Programación

Enviar emails utilizando el paquete UTL_HTTP de Oracle

28 mayo, 2013

Función para enviar emails utilizando el paquete UTL_HTTP de Oracle.
La función es básicamente esta, en la que tenéis que cambiar los datos de los destinatarios, del envío, adjuntos, y el link_mail desde dónde se realizará.

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
FUNCTION ejemplo_mail (envelope CLOB, link_mail VARCHAR2) RETURN CLOB IS
      vresult            VARCHAR2 (512);
      verror             VARCHAR2 (10);
 
      soapenvelope       VARCHAR2 (32767);
      soapenvio   		 VARCHAR2 (32767);
      soapcompleto       CLOB;
      http_req           UTL_HTTP.req;
      http_resp          UTL_HTTP.resp;
      xmldoc             XMLTYPE;
      xmlresp            XMLTYPE;
   BEGIN
 
      soapenvelope := '<?xml version="1.0" encoding="utf-8"?>
                <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <SOAP-ENV:Header>
                <SOAP-ENV:Body>
                               <mail:sendMail xmlns:mail="http://mail.example/">
                               <from>desde@mail.com</from>
                               <to>para@mail.com</to>    
                               <cc>concopia@mail.com</cc>    
                               <bcc>concopiaoculta@mail.com</bcc>   
                               <subject>Asunto del Mail</subject>    
                               <content>Este es un mail de prueba</content>    
                               <attachmentFileName>fichero_adjunto.txt</attachmentFileName>                           
                               <attachMentContent></attachMentContent>
                </mail:sendMail>    
                </SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
 
 
      -- La conversión a UTF8 nos daba problemas, así que remplazamos los carácteres
	  -- Evitamos el error "Invalid byte 2 of 2-byte UTF-8 sequence"
 
      soapenvelope := REPLACE(soapenvelope, 'Ñ', '&#xF1;');
	  soapenvelope := REPLACE(soapenvelope, 'á', '&#xE1;');
	  soapenvelope := REPLACE(soapenvelope, 'é', '&#xE9;');
	  soapenvelope := REPLACE(soapenvelope, 'í', '&#xED;');
	  soapenvelope := REPLACE(soapenvelope, 'ó', '&#xEF;');
 
 
 
      UTL_HTTP.set_persistent_conn_support (TRUE);
      UTL_HTTP.set_transfer_timeout (180);
      BEGIN
         http_req := UTL_HTTP.begin_request (link_mail, 'POST', 'HTTP/1.1');
		 -- link_sw = http://enviarmail.com:7001/sendMail
         UTL_HTTP.set_header (http_req, 'Content-Type', 'text/xml');
         UTL_HTTP.set_header (http_req, 'Content-Length', LENGTH (soapenvelope));
         UTL_HTTP.set_header (http_req, 'SOAPAction', '');
         UTL_HTTP.write_text (http_req, soapenvelope);       
         http_resp := UTL_HTTP.get_response (http_req);
         soapcompleto := '';
 
         LOOP
            UTL_HTTP.read_text (http_resp, soapenvio);
            soapcompleto := soapcompleto || soapenvio;
         END LOOP;
 
         UTL_HTTP.end_response (http_resp);
      EXCEPTION
         WHEN UTL_HTTP.end_of_body
         THEN
            UTL_HTTP.end_response (http_resp);
      END;
 
      RETURN (soapcompleto);  
    END ;