Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







Oracle e-mail attachment tips

Oracle Database Tips by Donald BurlesonNovember 18, 2015

Question:  I am using the Oracle utl_mail utility to send e-mail from Oracle, but now I want to be able to include an attachment to the e-mail.  How can I send an e-mail attachment?  Is there a difference between attaching a text file and attaching a PDF to an Oracle e-mail?

Answer:  There are several ways to send an attachment for an Oracle e-mail.  First, see my notes on create_ a PDF file from Oracle PL/SQL. The PDF can become an e-mail attachment in several ways.

- This is an alleged working example of using utl_smtp.write_raw_data to attach a PDF to an e-mail

- See this forum discussion on attaching a PDF to an Oracle e-mail

- You can see my notes on how to generate an e-mail from Oracle using PL/SQL 

- The utl_smtp utility allows attachments, see my notes on e-mail using utl_smtp

- You can also send an e-mail with an attachment if you have Oracle BI Publisher

- You can also attach a PDF using the Excel-DB tool.

There is a free PL/SQL library for generating PDFs from Oracle, called PL_FPDF.

For information on storing a PDF as a BFILE or within a BLOB column of a table, check here.

Here is an example of using using utl_smtp.write_raw_data to set an attachment to a file as an Oracle BLOB datatype:

procedure send_blob (
      p_sender varchar2,
      p_recipient varchar2,
      p_subject varchar2,
      p_filename varchar2,
      p_blob blob) is     

    c utl_smtp.connection;
    v_raw raw(57);
    v_length integer := 0;
    v_buffer_size integer := 57;
    v_offset integer := 1;     


    common(p_sender, p_recipient, p_subject, c);     

    utl_smtp.write_data( c, 'Content-Disposition: attachment; filename="' || p_filename || '"' || utl_tcp.crlf);
    utl_smtp.write_data( c, 'Content-Transfer-Encoding: base64' || utl_tcp.crlf );
    utl_smtp.write_data( c, utl_tcp.crlf );     

    v_length := dbms_lob.getlength(p_blob);     

    while v_offset < v_length loop p_blob, v_buffer_size, v_offset, v_raw );
      utl_smtp.write_raw_data( c, utl_encode.base64_encode(v_raw) );
      utl_smtp.write_data( c, utl_tcp.crlf );
      v_offset := v_offset + v_buffer_size;
    end loop while_loop;     

    utl_smtp.write_data( c, utl_tcp.crlf );
    utl_smtp.close_data( c );
    utl_smtp.quit( c );     

    when utl_smtp.transient_error or utl_smtp.permanent_error then
      utl_smtp.quit( c );
    when others then
  end send_blob;
* Here is another example of sending an Oracle e-mail with an attachment
  PROCEDURE begin_attachment(conn         IN OUT NOCOPY utl_smtp.connection,
			     mime_type    IN VARCHAR2 DEFAULT 'text/plain',
			     inline       IN BOOLEAN  DEFAULT TRUE,
			     filename     IN VARCHAR2 DEFAULT NULL,
			     transfer_enc IN VARCHAR2 DEFAULT NULL) IS
    write_mime_header(conn, 'Content-Type', mime_type);

    IF (filename IS NOT NULL) THEN
       IF (inline) THEN
	  write_mime_header(conn, 'Content-Disposition',
	    'inline; filename="'||filename||'"');
	  write_mime_header(conn, 'Content-Disposition',
	    'attachment; filename="'||filename||'"');
       END IF;
    END IF;

    IF (transfer_enc IS NOT NULL) THEN
      write_mime_header(conn, 'Content-Transfer-Encoding', transfer_enc);
    END IF;
    utl_smtp.write_data(conn, utl_tcp.CRLF);

  PROCEDURE end_attachment(conn IN OUT NOCOPY utl_smtp.connection,
    utl_smtp.write_data(conn, utl_tcp.CRLF);
    IF (last) THEN
      write_boundary(conn, last);
    END IF;

PROCEDURE begin_mail_in_session(conn       IN OUT NOCOPY utl_smtp.connection,
				  sender     IN VARCHAR2,
				  recipients IN VARCHAR2,
				  subject    IN VARCHAR2,
				  mime_type  IN VARCHAR2  DEFAULT 'text/plain',
    my_recipients VARCHAR2(32767) := recipients;
    my_sender     VARCHAR2(32767) := sender;

    -- Specify sender's address (our server allows bogus address
    -- as long as it is a full email address (
    utl_smtp.mail(conn, get_address(my_sender));

    -- Specify recipient(s) of the email.
    WHILE (my_recipients IS NOT NULL) LOOP
      utl_smtp.rcpt(conn, get_address(my_recipients));

    -- Start body of email

    -- Set "From" MIME header
    write_mime_header(conn, 'From', sender);

    -- Set "To" MIME header
    write_mime_header(conn, 'To', recipients);

    -- Set "Subject" MIME header
    write_mime_header(conn, 'Subject', subject);

    -- Set "Content-Type" MIME header
    write_mime_header(conn, 'Content-Type', mime_type);

    -- Set "X-Mailer" MIME header
    write_mime_header(conn, 'X-Mailer', MAILER_ID);

    -- Set priority:
    --   High      Normal       Low
    --   1     2     3     4     5
    IF (priority IS NOT NULL) THEN
      write_mime_header(conn, 'X-Priority', priority);
    END IF;

    -- Send an empty line to denotes end of MIME headers and
    -- beginning of message body.
    utl_smtp.write_data(conn, utl_tcp.CRLF);

    IF (mime_type LIKE 'multipart/mixed%') THEN
      write_text(conn, 'This is a multi-part message in MIME format.' ||
    END IF;



Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.



Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster