Skip to Main Content

Oracle Database Discussions

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Email attachments not showing as attachments

temmAug 30 2011 — edited Aug 30 2011
Hi,

i'm using the following code to attach a pdf blob to an email
CREATE OR REPLACE PROCEDURE sendPDFBLOB (lv_server varchar2, lv_from varchar2, lv_rcpt varchar2, lv_cc varchar2,lv_bcc varchar2, lv_subject varchar2
,  lv_message varchar2, i_blob blob) IS
  l_conn UTL_SMTP.CONNECTION;
  l_len number :=0 ;
  l_idx integer := 1;
  l_buff_size integer := 57;
  l_boundary varchar2(32) := sys_guid();
  l_raw raw(57);
  l_blob blob := to_blob('1');
  l_attachment_name varchar2(100);
BEGIN


-- Connect
	  l_conn := utl_smtp.open_connection(lv_server);
	  utl_smtp.helo( l_conn, lv_server);
	  utl_smtp.mail( l_conn, lv_from );
	  utl_smtp.rcpt( l_conn, lv_rcpt );
	  utl_smtp.open_data(l_conn);
	 
	  -- Header
	  utl_smtp.write_data( l_conn, 'From: ' || lv_from || utl_tcp.crlf );
	  utl_smtp.write_data( l_conn, 'To: ' || lv_rcpt || utl_tcp.crlf );
	  utl_smtp.write_data( l_conn, 'Subject: ' || lv_subject ||  utl_tcp.crlf );
   
	  utl_smtp.write_data( l_conn, 'MIME-Version: 1.0' || utl_tcp.crlf );
	  utl_smtp.write_data( l_conn, 'Content-Type: multipart/mixed' ||  utl_tcp.crlf );
	  utl_smtp.write_data( l_conn, ' boundary= "' || l_boundary || '"' || utl_tcp.crlf );
	  utl_smtp.write_data( l_conn, utl_tcp.crlf );
	 
	  -- Body
	  utl_smtp.write_data( l_conn, '--' || l_boundary || utl_tcp.crlf );
      utl_smtp.write_data( l_conn, 'Content-Type: text/html;US-ASCII' || UTL_TCP.CRLF);
      utl_smtp.write_data( l_conn, utl_tcp.crlf );
	  utl_smtp.write_data( l_conn, utl_tcp.crlf );
	 


   --SEND PDF BLOB
    l_attachment_name := 'invoice.pdf';

	utl_smtp.write_data( l_conn, '--' || l_boundary || utl_tcp.crlf );
	utl_smtp.write_data( l_conn, 'Content-Type: text/plain;US-ASCII; name="' || l_attachment_name || '"' || utl_tcp.crlf );
	utl_smtp.write_data( l_conn, 'Content-Disposition: attachment; filename="' || l_attachment_name || '"' || utl_tcp.crlf );
	utl_smtp.write_data( l_conn, 'Content-Transfer-Encoding: base64' || utl_tcp.crlf );
	utl_smtp.write_data( l_conn, utl_tcp.crlf );
     
  	-- Loop through the blob chuck it up into 57-byte pieces and base64 encode it and write it into the mail buffer
    l_idx := 1;
   	l_len := dbms_lob.getlength(i_blob);
   	while l_idx < l_len loop
   	   dbms_lob.read( i_blob, l_buff_size, l_idx, l_raw );
   	   utl_smtp.write_raw_data( l_conn, utl_encode.base64_encode(l_raw) );
 	   utl_smtp.write_data( l_conn, utl_tcp.crlf );
   	   l_idx := l_idx + l_buff_size;
   	end loop;
   	utl_smtp.write_data( l_conn, utl_tcp.crlf );
         
	 
    -- Close Email
	utl_smtp.write_data( l_conn, '--' || l_boundary || '--' || utl_tcp.crlf );
	utl_smtp.write_data( l_conn, utl_tcp.crlf || '.' || utl_tcp.crlf );
	utl_smtp.close_data( l_conn );
	utl_smtp.quit( l_conn );
	 
	exception
	  -- smtp errors, close connection and reraise
	  when utl_smtp.transient_error or
	       utl_smtp.permanent_error then
	    utl_smtp.quit( l_conn );
	    raise;  
  
END;
This is working, but the problem that i have is that the attachment differs depending on the mail client.

Using thunderbird i can see in the header of the mail that there is an attachment and when opening the message i can see the attached file, but when i send this to an gmail account the header doesn't show an attachment and the body of the mail contains a file 'noname'.

If i send multple attachments these are viewed correctly from thunderbird but the gmail account tends to show them as one 'noname' file which is the whole block of the email.

Teo
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 27 2011
Added on Aug 30 2011
3 comments
431 views