|
| 1 | +# Function that sends an email using OCI Email Delivery |
| 2 | +This function sends an email using the OCI Email Delivery Service. |
| 3 | + |
| 4 | +As you make your way through this tutorial, look out for this icon . |
| 5 | +Whenever you see it, it's time for you to perform an action. |
| 6 | + |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | +Before you deploy this sample function, make sure you have run step A, B and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html) |
| 10 | +* A - Set up your tenancy |
| 11 | +* B - Create application |
| 12 | +* C - Set up your Cloud Shell dev environment |
| 13 | + |
| 14 | + |
| 15 | +## List Applications |
| 16 | +Assuming your have successfully completed the prerequisites, you should see your |
| 17 | +application in the list of applications. |
| 18 | +``` |
| 19 | +fn ls apps |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +## Review and customize the function |
| 24 | +Review the following files in the current folder: |
| 25 | +* the code of the function, [SendEmail.cs](./SendEmail.cs) |
| 26 | +* its dependencies, [SendEmail.csproj](./SendEmail.csproj) |
| 27 | +* the function metadata, [func.yaml](./func.yaml) |
| 28 | + |
| 29 | + |
| 30 | +## Deploy the function |
| 31 | +In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image, |
| 32 | +push the image to OCIR, and deploy the function to Oracle Functions in your application. |
| 33 | + |
| 34 | + |
| 35 | +``` |
| 36 | +fn -v deploy --app <app-name> |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +## Configure the Email Delivery Service |
| 41 | + |
| 42 | + |
| 43 | +On the OCI console, navigate to *Email Delivery*. Click `Create Approved Sender`. |
| 44 | + |
| 45 | + |
| 46 | +Enter the email address of the approved sender and click `Create Approved Sender`. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +The creation of the *Approved Sender* takes several minutes. |
| 51 | + |
| 52 | +Navigate to *Email Delivery* > *Email Configuration* and write down the SMTP server name. |
| 53 | +Click `Manage SMTP Credentials` which takes you to your *User Details*, click on `Generate SMTP Credentials` and enter a description. Your SMTP Username and password will be displayed, write them down, you will need them to configure the function. |
| 54 | + |
| 55 | + |
| 56 | +## Set the function configuration values |
| 57 | +The function requires the following configuration values to be set: |
| 58 | +- smtp-username |
| 59 | +- smtp-password |
| 60 | +- smtp-host |
| 61 | +- smtp-port (25 or 587) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +Use the *fn CLI* to set the config value: |
| 66 | +``` |
| 67 | +fn config function <app-name> <function-name> smtp-username <SMTP-username> |
| 68 | +fn config function <app-name> <function-name> smtp-password <SMTP-password> |
| 69 | +fn config function <app-name> <function-name> smtp-host <SMTP-Server-name> |
| 70 | +fn config function <app-name> <function-name> smtp-port <SMTP-Port> |
| 71 | +``` |
| 72 | +e.g. |
| 73 | +``` |
| 74 | +fn config function myapp oci-email-send-dotnet smtp-username "ocid1.user.oc1..aaaaaaaause3s2zw3kn3qvxxc5c5ouc4pu6byfxiuplncjkzzxinijhmqj5q@ocid1.tenancy.oc1..aaaaaaaaydrjm77otncda2xn7qtv7l3hqnd3zxn2u6siwdhniibwfv4wwhta.7g.com" |
| 75 | +fn config function myapp oci-email-send-dotnet smtp-password '{$M$mWmvlN&P#o>!14F8' |
| 76 | +fn config function myapp oci-email-send-dotnet smtp-host "smtp.us-phoenix-1.oraclecloud.com" |
| 77 | +fn config function myapp oci-email-send-dotnet smtp-port 587 |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | +## Invoke the function |
| 82 | +The function requires the following keys in the payload to be invoked: |
| 83 | +- sender_email |
| 84 | +- sender_name |
| 85 | +- recipient |
| 86 | +- subject |
| 87 | +- body |
| 88 | + |
| 89 | + |
| 90 | +``` |
| 91 | +echo '{ "sender_email":"<approved-sender-email>", "sender_name":"<sender-name>", "recipient":"<recipient-email>", |
| 92 | +"subject":"<email-subject>", "body":"<email-body>" }' | fn invoke <app-name> oci-email-send-dotnet |
| 93 | +``` |
| 94 | +e.g.: |
| 95 | +``` |
| 96 | +echo '{ "sender_email":"[email protected]", "sender-name":"Test", "recipient":"[email protected]", |
| 97 | +"subject":"Hello!", "body":"This is a test email" }' | fn invoke myapp oci-email-send-dotnet |
| 98 | +``` |
| 99 | + |
| 100 | +Upon success, the function will return "Email successfully sent!" and the recipient will receive an email. |
| 101 | + |
| 102 | + |
| 103 | +## Monitoring Functions |
| 104 | + |
| 105 | +Learn how to configure basic observability for your function using metrics, alarms and email alerts: |
| 106 | +* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md) |
| 107 | + |
0 commit comments