31.2 ADD_ATTACHMENT Procedure Signature 1

This procedure adds an attachment of type BLOB to an outbound email message. To add multiple attachments to a single email, APEX_MAIL.ADD_ATTACHMENT can be called repeatedly for a single email message.

Syntax

APEX_MAIL.ADD_ATTACHMENT (
    p_mail_id           IN    NUMBER,
    p_attachment        IN    BLOB,
    p_filename          IN    VARCHAR2,
    p_mime_type         IN    VARCHAR2
    p_content_id        IN    VARCHAR2    DEFAULT NULL );

Parameters

Table 31-1 ADD_ATTACHMENT Parameters

Parameter Description
p_mail_id The numeric ID associated with the email. This is the numeric identifier returned from the call to APEX_MAIL.SEND to compose the email body.
p_attachment A BLOB variable containing the binary content to be attached to the email message.
p_filename The filename associated with the email attachment.
p_mime_type A valid MIME type (or Internet media type) to associate with the email attachment.
p_content_id

An optional identifier for the attachment. If non-null, then the file attaches inline. That attachment may then be referenced in the HTML of the email body by using the cid.

Note: Be aware that automatic displaying of inlined images may not be supported by all e-mail clients.

Example 1

The following example demonstrates how to access files stored in APEX_APPLICATION_FILES and add them to an outbound email message

DECLARE
    l_id NUMBER;
BEGIN
    l_id := APEX_MAIL.SEND(
        p_to        => 'fred@flintstone.com',
        p_from      => 'barney@rubble.com',
        p_subj      => 'APEX_MAIL with attachment',
        p_body      => 'Please review the attachment.',
        p_body_html => '<b>Please</b> review the attachment');
    FOR c1 IN (SELECT filename, blob_content, mime_type 
        FROM APEX_APPLICATION_FILES
        WHERE ID IN (123,456)) LOOP

        APEX_MAIL.ADD_ATTACHMENT(
            p_mail_id    => l_id,
            p_attachment => c1.blob_content,
            p_filename   => c1.filename,
            p_mime_type  => c1.mime_type);
        END LOOP;
    COMMIT;
END;
/

Example 2

This example shows how to attach a file inline, by using a content identifier, and how to refer to that attachment in the HTML of the email.

DECLARE
  l_id number;
  l_body clob;
  l_body_html clob;
  l_content_id varchar2(100) := 'my-inline-image';
  l_filename varchar2(100);
  l_mime_type varchar2(100);
  l_image blob;
BEGIN
  l_body := 'To view the content of this message, please use an HTML enabled mail client.' || utl_tcp.crlf;

  l_body_html := '<html><body>' || utl_tcp.crlf ||
                 '<p>Here is the image you requested.</p>' || utl_tcp.crlf ||
                 '<p><img src="cid:' || l_content_id || '" alt="Requested Image"></p>' || utl_tcp.crlf ||
                 '<p>Thanks,<br />' || utl_tcp.crlf ||
                 'The Application Express Dev Team<br />' || utl_tcp.crlf ||
                 '</body></html>';
  l_id := apex_mail.send (
    p_to => 'some_user@somewhere.com', -- change to your email address
    p_from => 'some_sender@somewhere.com', -- change to a real senders email address
    p_body => l_body,
    p_body_html => l_body_html,
    p_subj => 'Requested Image' );

  select filename, mime_type, blob_content
    into l_filename, l_mime_type, l_image
    from apex_application_files
   where id = 123;

  apex_mail.add_attachment(
    p_mail_id => l_id,
    p_attachment => l_image,
    p_filename => l_filename,
    p_mime_type => l_mime_type,
    p_content_id => l_content_id );

  COMMIT;
END;