Tuesday, March 14, 2023

Create serverless OCI Oracle Functions to send emails.

This is a 2 part blog.
Part 1 will discusses how we can configure OCI Functions to send gmail.
The Part2 of this will discuss how you can call invoke the function from Oracle Autonomous Database.
OCI Functions is a serverless platform that allows developers to build and run code without having to worry about managing the infrastructure. In this blog, we will explore how to use OCI Functions and Python to send emails using the smtplib module. In this example, we are using Gmail as the email provider.

Setup environment.

Step 1: Setting up the Environment Before we start coding, we need to set up our environment. We will use Oracle Cloud shell to configure and run
If you’re new to OCI Functions, please take a look at the Functions QuickStart on Cloud Shell.
We’re going to use one of the publicly available OCI Functions, oci-email-send-python, from the OCI Functions Samples GitHub Repository.

Deploy the oci-email-send-python Function

  • From the Developer Services click on Applications then on Create Application where you give it a name and you select an existing VCN and a Subnet for the Application (for simplicity, select a Public Subnet)
  • Follow the steps from the Cloud Shell Setup displayed in your new application for easy setup
  •  using the OCI Cloud Shell. and run the sample function from "getting started"
  • Navigate to the email delivery function
cd oracle-functions-samples/samples/oci-email-send-python
  • Deploy the function to your newly created Application
fn -v deploy --app <your_app_name>




ake sure you create the policies for OCI Functions. You can easily use the Functions use case from the Policy Builder. You just need to point it to your Group and Compartment:

Generate app-specific password for gmail.

If you have enabled 2-factor authentication on your Google account then you can't use your regular password to access mail through code. You need to generate an app-specific password and use that instead your actual password.

Do the following steps to generate app specific password,

  • Log in to your Google account or use this link https://security.google.com/settings/security/apppasswords
  • Go to My Account > Sign-in & Security > App Passwords
  • Scroll down to Select App in the Password & sign-in methds and choose Other (custom name) Give this app password a name, Eg: "automailer"
  • Choose Generate Copy the long generated password and paste it into your script instead of your actual Gmail password.


Config the Function

You can use configuration values to store environment-specific information such as smtp username, password host and port. endpoint URLs, and other settings that may vary across environments. By using configuration values, you can ensure that your function works consistently across different environments, such as development, staging, and production. Also, this will ensure you can change the config information without having to redeploy the function when you have to change the configuration.

fn config function oci-send-email oci-email-send-python smtp-username "vijay.hogan@gmail.com"
fn config function oci-send-email oci-email-send-python smtp-password "'bxttdqbrmsgliciw'"
fn config function oci-send-email oci-email-send-python smtp-host "smtp.gmail.com"
fn config function oci-send-email oci-email-send-python smtp-port 587

Test the Function





echo '{ "sender-email":"vijay.hogan@gmail.com", "sender-name":"Test", "recipient":"vijay.balebai@oracle.com",
"subject":"Hello subject", "body":"This is a test email" }' | fn invoke myapp oci-email-send-python


Now check out the The Part2 of this blog which discusses how to invoke and send email from ADB.


No comments:

Post a Comment

Feedback welcome