Note:

Integrate OCI Email Delivery Service with OCI Functions, OCI Object Storage, OCI Vault, and OCI Database with PostgreSQL

Introduction

Businesses rely on effective email communication for various purposes, including automatic notifications, application updates, and marketing campaigns. To meet these needs, Oracle Cloud Infrastructure (OCI) offers a robust Email Delivery service designed to provide a fast and reliable managed solution for sending secured, high-volume marketing and transactional emails.

This tutorial illustrates how the OCI Email Delivery service is employed for streamlined email sending, seamlessly integrated with OCI Functions, OCI Vault, OCI Object Storage, and OCI Database with PostgreSQL to enhance automation and reliability in email dispatch processes and it also offers a flexible platform for creating reliable email delivery solutions. The provided code serves as a starting point for developers to build customized email delivery pipelines according to their specific needs.

When invoked, the function created using OCI Functions service will retrieve an email template from OCI Object Storage, connect to the OCI Database with PostgreSQL database to retrieve recipient email addresses, and will send the email to all these recipients.

Objectives

Prerequisites

Task 1: Set up the OCI Email Delivery Service

To configure the OCI Email Delivery service, see Step-by-step instructions to send email with OCI Email Delivery and Video: How to configure Email Delivery in OCI.

After completing the configuration, you should have at least one approved email sender and Simple Mail Transfer Protocol (SMTP) credentials for your OCI user.

Task 2: Upload the Email Template in OCI Object Storage Bucket

Download the email template from here: Email template and upload it to the existing bucket.

Bucket containing the template of the email to be sent

Task 3: Set up Recipient Addresses in OCI Database with PostgreSQL Database

  1. Connect to the OCI Database with PostgreSQL database system. For more information, see Connecting to an OCI Database with PostgreSQL with the PostgreSQL CLI.

  2. Create a table for the recipient email addresses in the OCI Database with PostgreSQL database.

    CREATE TABLE emails (
        address VARCHAR(255) PRIMARY KEY
    );
    
  3. Insert values into the table.

    INSERT INTO emails (address) VALUES
        ('email1@example.com'),
        ('email2@example.com'),
        ('email3@example.com');
    

Task 4: Create Secret(s) in OCI Vault

Create secrets in your vault for the sensitive values you will be using, for example the SMTP username and password (used by the OCI Email Delivery service), the OCI Database with PostgreSQL database username and password. These secrets will be used later in OCI Functions.

Vault secrets

Task 5: Set up OCI Functions

  1. Create a function. For more information, see Functions: Get Started using the CLI.

    Note: In this tutorial, we use a Python function.

    In this tutorial, the application is named email-delivery-function and the function deployed is named email_function.

    Application-function

  2. After the original basic function set up, you will have the following files in your function folder.

    • func.py: To place the custom Python function code.
    • func.yaml: Configuration data for your function.
    • requirements.txt: Lists dependencies required by the function code.
  3. Download the function code from here: Python script and replace the code in the func.py file.

    Summary of what the script func.py does:

    • Imports necessary libraries for sending emails, working with JSON data, accessing OCI services, and interacting with an OCI Database with PostgreSQL database.

    • Defines functions for sending emails (sendEmail) and decoding secrets stored in OCI Vault (decodeSecret).

    • Defines a main handler function (handler) that:

      • Retrieves SMTP credentials, SMTP host, SMTP port, and other configuration values from the function context.

      • Retrieves email template content from OCI Object Storage.

      • Retrieves sender information and subject from the incoming JSON payload.

      • Connects to an OCI Database with PostgreSQL database using credentials obtained from OCI Vault.

      • Executes a query to retrieve email addresses from a table in the database.

      • Iterates through the retrieved email addresses and sends emails using the sendEmail function.

      • Returns a success message indicating the email addresses to which emails were sent.

    Note:

    • Replace the host in the script with your own OCI Database with PostgreSQL database system endpoint located under the OCI Database with PostgreSQL connection section. You can also set it up as another secret in the OCI Vault or take it from the payload.

    • If you want to run the function from another file which is not named as func.py, place that file in the function’s folder, and edit the func.yaml file accordingly.

  4. Edit the requirements.txt file to include the extra libraries on which the new Python code relies:

    • oci: Ensures that the OCI Software Development Kit (SDK) is installed in the function’s runtime environment when the function is deployed to OCI Functions.

    • psycopg2-binary: Allows the function code to connect to and manipulate OCI Database with PostgreSQL database.

    $ cat requirements.txt
    fdk
    oci
    psycopg2-binary
    
  5. Add the following configuration to the function context. Replace the Oracle Cloud Identifier (OCID) of the vault and the address of the OCI Database with PostgreSQL database system endpoint.

    fn config function email-delivery-function email_function db-host "..postgres_endpoint_IP.."
    fn config function email-delivery-function email_function vault-ocid "ocid1.vault.oc1.eu-frankfurt-1..."
    fn config function email-delivery-function email_function smtp-host "smtp.email.eu-frankfurt-1.oci.oraclecloud.com"
    fn config function email-delivery-function email_function smtp-port 587
    fn config function email-delivery-function email_function smtp-password "xxxxxxxxxx"
    fn config function email-delivery-function email_function smtp-username "smtp_username"
    fn config function email-delivery-function email_function db-username "dbuser"
    fn config function email-delivery-function email_function db-password "xxxxxxxxxx"
    
  6. Deploy the function.

    Note: Replace email-delivery-function with the name of your application.

    fn -v deploy --app email-delivery-function
    
  7. Invoke the function. The function expects a payload which should include:

    • sender-email: The approved sender from the OCI Email Delivery service.
    • sender-name: The name of the sender, as displayed to the recipient.
    • subject: Subject of the email.

    If needed, replace the application name and function name.

    echo '{ "sender-email":"...approved_sender_address...", "sender-name":"Company Test", "subject":"test email" }' | fn invoke email-delivery-function email_function
    

    The function invocation should return the message:

    Email successfully sent to ['email1@example.com', 'email2@example.com', 'email3@example.com']!
    

Acknowledgments

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.