Sending emails from an APEX application in the cloud

Saurabh Sinha Sep 29, 2022 11:52:23 AM

Configuring and sending emails from an APEX application running in the cloud involves multiple steps and can sometimes be confusing.

Therefore, I decided to write this blog to give step-by-step instructions on how to do it for an APEX application hosted in OCI. You can find out more about our APEX services here.

Pre-requisite

  1. You have already set up and configured an Oracle Cloud instance running an Autonomous Database (ATP).
  2. You have created an APEX workspace in your OCI environment.

Summary

Sending emails from your APEX application on your OCI ATP environment involves four simple steps.

  1. Generate SMTP credentials for Email Delivery.
  2. Create an approved sender for Email Delivery.
  3. Configure the SMTP parameters within the APEX instance.
  4. Set up and send a test email using SQL Workshop within APEX.

Step by Step Guide

Step 1: Generate SMTP Credentials

In this step, you will generate the SMTP credentials to use when sending emails from your application.

1. Sign in to your OCI environment

2. Navigate to Identity & Security -> Users and click the username for which you want to create SMTP credentials.

Identity and Security

3. Here you'll find a list of users. Choose the one you would like to use for the SMTP credentials.

Users

4. You can select one of the existing users or create a new one.

5. Select the username you wish to use and click on SMTP Credentials, which you will find under the Resources menu. Next, click on Generate SMTP Credential.

SMTP Credentials

6. Enter a Description and click Generate SMTP Credentials again.

Generate SMTP Credentials

7. Copy the username/password and keep them safe because you will need them later.

Username, Password

Step 2: Creating an Approved Sender for Email Delivery

You will set up an approved sender for email delivery in this step.

1. In the OCI console, search for Email Delivery and then click on the option within the search results.

Email Delivery

2. Under Email Delivery, select Approved Senders and then click on the Create Approved Sender button.

Approved Senders

3. After that, enter a valid email address and click on Create Approved Sender.

Create Approved Sender

4. You also need to identify the SMTP connection endpoint for email delivery.

5. Under Email delivery, click on Configuration and then copy the value for Public Endpoint. You will need this later in the process.

Configuration

Step 3: Configure the SMTP parameters within the APEX instance

Now you have generated the SMTP credentials, you need to let the APEX instance know what they are.

 1. First, you need to connect to the Autonomous Database using a SQL client (for example, SQL Developer Web) and set the SMTP parameters using the APEX Instance Admin set parameter.

 2. For the following, PL/SQL uses the apex_instance_admin API to set the required parameters for the host address, username and password.

apex_instance_admin

3. In the left side menu, navigate to Oracle Database and then Autonomous Transaction Processing.

Oracle Database

4. Click the relevant Instance Name.

Autonomous Databases

5. After that, click on Database Actions.

Database Actions

6. Right-click on the SQL option and open in new tab to open SQL Developer Web in a new browser window.

SQL

7. In SQL Worksheet, enter your PL/SQL code based on the example in step 2 and run the statement.

PL, SQL Code

Step 4: Sending a test email using Oracle APEX > SQL Workshop

Now set all the required parameters, you can finally test sending an email from your APEX application using the API apex_mail.

 1. To send an email using the API, you need to specify 4 values as a minimum.

  • The approved sender email address that you created in OCI
  • Any valid email address as the recipient
  • A subject 
  • Some text for the email body

apex_mail

2. Navigate to your instance details page, and under the Tools tab, click on the Open APEX button within the Oracle APEX card.

Open APEX

3. Enter your existing application's Workspace, Username, and Password and sign in.

Sign In

4. Navigate to SQL Workshop and select the SQL Commands option. 

5. Enter the PL/SQL code based on the example from step 1 and run it.

SQL Workshop

6. Now you can Query the APEX_MAIL_LOG view to verify if the email has been successfully delivered or not.

apex_mail_log

7. Assuming your email has been successfully delivered, you can now check your inbox to confirm.

Inbox

Conclusion

So, after following all the above steps, we can send the email from our Oracle APEX applications hosted in OCI.

If you would like to speak to one of our Oracle APEX experts, get in touch through enquiries@dsp.co.uk or book a meeting...

Book a Meeting Today