Sending Email Attachments in Oracle Application Express 3.1

Purpose

This tutorial shows you how to build a page that can send email attachments in Oracle Application Express 3.1.

Time to Complete

Approximately 45 minutes

Topics

This tutorial covers the following topics:

 Overview
 Prerequisites
 Create a Send Email Page and HTML Region
 Create Items to Store Email Information
 Create a Button and Process to Send Email
 Test Send Email Page
 Summary
 Related Information

Viewing Screenshots

 Place the cursor over this icon to load and view all screenshots for this tutorial. (Caution: This action loads all screenshots simultaneously, so response time may be slow depending on your Internet connection.)

Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.

Overview

In Oracle Application Express 3.1, you can now include attachments with emails sent using the Application Express mail API APEX_MAIL.ADD_ATTACHMENT.

To get more information about the 3.1 New Features, click here.

Back to Topic List

Prerequisites

Before you perform this tutorial, you should:

1.

Install Oracle Database 9.2 or above.

2.

Download and Configure Oracle Application Express 3.1.

3.

Download the OEHR Sample Objects Packaged Application here and import it into your Oracle Application Express 3.1 instance.

4.

Download and unzip the apexnf.zip file which contains files necessary to perform this tutorial

5.

To perform this tutorial, you need to perform the Building and Customizing an Interactive Report in Oracle Application Express 3.1 and Defining and Viewing BLOB Data in Oracle Application Express 3.1 tutorials.

Alternatively, you can import the OBE New Features application as a packaged application in Application Express. Download and unzip the apexnf.zip file and import the blobobe_packagedapp.sql packaged application file. In addition, you need to alter the OEHR_EMPLOYEES table and load the BLOB data. Alter the addblobdata.sql script to specify the path of the location of where the dog.gif file is stored so that when the directory PHOTO_LOC is created, the file can be located. From the location where you downloaded and unzipped the script files, open a SQL*Plus session and execute the following commands:

                                 
connect system
grant create any directory to <workspace name>;

connect <workspace name>
@addblobdata
                              

Note: The application numbers and page numbers may differ slightly from the screenshots.

Back to Topic List

Create a Send Email Page and HTML Region

You will create a page and region that allows a user to send an email with an attachment. Perform the following steps:

1.

Navigate to your New Features Application page. Click Create Page.

 

2.

Click Blank Page.

 

3.

Click Next.

 

4.

Enter Send Email Attachment for NAme and click Next.

 

5.

Accept the default and click Next.

 

6.

Click Finish.

 

7.

Your page was created. Now you need to add a region. Click Edit Page.

 

8.

Under Regions, click the Create  icon.

 

9.

Click the HTML icon.

 

10.

Click the HTML icon again.

 

11.

Enter Email Attachment for the Title and click Create.

 

12.

Your page and region have been created. In the next section you create the items you need to enter to send an email with an attachment.

 

Back to Topic List

Create Items to Store Email Information

You want to create all the necessary items needed to submit an email with an attachment. Perform the following steps:

1.

Under Items, click the Create  icon.

 

2.

Since you need to create multiple items, select the Create Multiple Items using Drag and Drop Layout link.

 

3.

Click Create Multiple Items >.

 

4.

Select the Text  icon and drag and drop it over to the first line. Enter P<#>_EMAIL for the Item Name and Email Address for the Label.

 

5.

Drag and drop the Text  icon to the second line. Enter P<#>_FROM for the Item Name and From for the Label.

 

6.

Create the rest of the items as shown below and then click Next.

Item Type Item Name Label
Text P<#>_SUBJECT Subject
Check box P<#>_IMAGE Attachment Images
Textarea P<#>_BODY Message Body

 

7.

Click Apply Changes.

 

8.

Select the P<#>_IMAGE link.

 

9.

Select the LOV tab.

 

10.

You want to supply a list of employee pictures that will then be selected and emailed as an attachement. Enter the following List of Values definition and click Apply Changes. Note: the code below checks to see if the length of the photo column is not equal to 0, if so, it selects the blob from the photo column.

                               
select filename||'</br>'||
       decode(nvl(dbms_lob.getlength(photo),0),0,null,
              '<img src="'||apex_util.get_blob_file_src('P3_PHOTO',employee_id)
                ||'" height="128" width="128" />') img,
       employee_id r
 from oehr_employees 
where mimetype like 'image%'
                                
order by 1

 

11.

All the items have been defined. In the next section, you create a button and a process to send the email with the attachment to the recipient.

 

Back to Topic List

Create a Button and Process to Send Email

The button and process you create in this section handles the sending of the email with the attachment. Perform the following steps:

1.

Under Buttons, click the Create icon.

 

2.

Accept the default and click Next.

 

3.

Select Create a button displayed among this region's items and click Next.

 

4.

Enter P<#>_SEND_EMAIL for the Name, enter Send Email for the Label and EMAIL for Request. Select HTML Button for Button Style and click Create Button.

 

5.

Your button was created. Now you can create the process that is invoked when the button is pressed. Under Page Processing for Processes, click the Create icon.

 

6.

Click the PL/SQL process category.

 

7.

Enter EMAIL with Attachments for the Name and click Next.

 

8.

Enter the following code in the PL/SQL Page Process area. Note: If you are currently not working on page 4, change the 4 to what ever your page number is. For example, if you are working on page 5, change P4_IMAGE to P5_IMAGE. Then click Next.

                               
DECLARE
                                
l_id number;
l_index number;
l_vc_arr2 APEX_APPLICATION_GLOBAL.VC_ARR2;
BEGIN
l_id := APEX_MAIL.SEND(
p_to => :P4_EMAIL,
p_from => :P4_FROM,
p_subj => :P4_SUBJECT,
p_body => :P4_BODY);
l_vc_arr2 := APEX_UTIL.STRING_TO_TABLE(:P4_IMAGE);
FOR i_index IN 1..l_vc_arr2.count LOOP
FOR c1 in (select filename, photo, mimetype from oehr_employees where employee_id = l_vc_arr2(i_index)) loop
APEX_MAIL.ADD_ATTACHMENT( p_mail_id => l_id,
p_attachment => c1.photo,
p_filename => c1.filename,
p_mime_type => c1.mimetype);
END LOOP c1;
END LOOP i_index;
COMMIT;
END;

The PL/SQL code in this process uses the existing capability, APEX_MAIL.SEND, to create the main email but retrieves the id associated with the email for use with the attachments. The ids of the selected images (which is stored in :P4_IMAGES as a colon delimited string), converts the string to a table (using the built in APEX_UTIL.STRING_TO_TABLE) and loads that table into an array. More information is retrieved about each selected image from the base table (OEHR_EMPLOYEES) and one at a time, the new procedure (APEX_MAIL.ADD_ATTACHMENT) executes and attaches the selected image to the base email.

Note: you are just using the text body here however if the text was formatted html, it should be sent using p_body_html.

 

9.

Enter Success and Failure Messages and click Next.

 

10.

Select P4_SEND_EMAIL for When Button Pressed and click Create Process.

 

11.

You also need to modify the Branch to include a success message when the process completes. Note this option is not selected by default. Under Branches, select the Go to Page link.

 

12.

Click the include process success message check box and click Apply Changes.

 

13.

Your branch was modified successfully. In the next section, you run and test the page. Since you only added one picture in the database in the Defining and Viewing BLOB Data in Oracle Application Express 3.1 tutorial, you want to add another picture before you test the Send Email page. Select the page number (in most cases, 2) for Maintain Employee Information and click Go.

 

Back to Topic List

Test Send Email Page

Now you are ready to test the page and send an email with an attachment. Perform the following steps:

1.

Click Run to run this page.

 

2.

Select the Edit icon next to another employee to go to the Employee Details page.

 

3.

For Photo, click Browse...

 

4.

Select the flowers-01_small.gif file from the list of files you downloaded in the prerequisites apexnf.zip and click Open. Then click Apply Changes.

 

5.

Now you can run the Send Email Attachments page. Click the Application link in the developer toolbar.

 

6.

Select the Send Email Attachments page.

 

7.

Click Run Page.

 

8.

Enter your email address, a From: email address, a Subject:, select the dog.gif and flowers-01_small.gif check boxes and enter something in the message body. Then click Send Email.

 

9.

You should see that your email was sent successfully.

 

10.

The email received should look something like the image below.

 

Back to Topic List

Summary

In this tutorial, you learned how to create a page that contains all the necessary items to send an email with an attachment.

Back to Topic List

Related Information

To learn more about Application Express, you can review the following:

Related Documentation

Description

Online Help

To access this help, click the Help button displayed in the top right corner of Application Express. Search on topics of interest.

Release Notes

These notes contain important information not included in the Oracle Application Express documentation.

Installation Guide

This guide explains how to install and configure Oracle Application Express.

2 Day + Developer’s Guide

This guide shows you how to set up a development environment or access a hosted demonstration environment to use with this guide. It then walks you through building an initial application, modifying it, and previewing it.

User’s Guide

This guide describes how to use the Oracle Application Express development environment to build and deploy database-centric Web applications. Oracle Application Express turns a single Oracle database into a shared service by enabling multiple workgroups to build and access applications as if they were running in separate databases.

Advanced Tutorial

Tutorials with step-by-step instructions that explain how to create a variety of application components and entire applications using the Oracle Application Express development environment.

Back to Topic List

 Place the cursor over this icon to hide all screenshots.

 

 

 

Left Curve
Popular Downloads
Right Curve
Untitled Document