Sending email from Oracle with utl_smtp via Amazon Simple Email Service (SES)
Note: This article was updated following Symantec Trust issues earlier in 2018.
Sometimes I want to send email from my own databases for testing purposes.
Lacking a mail server, I opted to use Amazon’s Simple Email Service (SES). SES is a paid product, with a free usage tier.
For my purposes however SES is a bargain at $0.0001 per email. If you want to perform this exercise yourself, you will need the following:
–An Amazon AWS account
My tests were performed on Oracle Linux 6; your mileage may vary on other platforms.
When first configured the only allowed access is via the SES Sandbox. Being in the sandbox requires that you verify all senders and recipients. For testing SES functionality that works out fine however, as I just wanted to successfully send an email from an Oracle database so I would know it could be done, and what was required to make it work.
You can request to be moved out of the Sandbox - this option is available in the SES Console.
Throughout the article I will include some URL’s for AWS, but as AWS tend to change over time these links may not work in the future. If that is the case, just use google or search on the AWS console, as most things are easy to find.
Most everything related to SES can be accessed from the SES Home Page
The first step is to logon to your AWS account and go to the SES Console page.
If you navigate to the SES Home Page page and click on Getting Started, you will see Send an email using the Amazon SES console (AWS 10-minute tutorial). That article will walk you through verifying email addresses.
All new SES configurations are in the SES Sandbox. Initially you will need to verify both addresses you are sending from and addresses you are sending to. For testing purposes they can be the same addresses.
At this point it is a good idea to keep in mind this test will require Oracle 184.108.40.206+.
Earlier versions of Oracle do not support TLS, and this test simply will not work.
(If someone can prove otherwise, please share the details)
Next you will need to add an IAM Identity that allows access to the email server. Go back to the SES Console and click on Identity Management.
Follow the instructions to create an identity, and put the information somewhere for safe-keeping.There will be three items created - the IAM Identity, the username and the password.
Username and password will both be long names of random characters; you will not be able to remember them. These can be downloaded as a CSV file at the time of creation.
These AWS pages change from time to time, and will not be documented here.
In order to access the SES email server from Oracle with utl_smtp, an Oracle wallet will be required to enable this.
Digicert has an article on importing certificates into Oracle Wallets: Installation Instructions for Oracle Wallet Manager
As the article refers to using the GUI tool owm (oracle wallet manager) it will not be discussed further here. I found owm to be rather difficult to use. Some owm operations do not work, and the error messages are not useful, and the interface is clunky.
The command line tool orapki is much better, or at least I think so.
The following commands are used to manually create a wallet and import the certificates.
Note: Keep in mind that on RAC these files must be copied to other nodes as needed, or else be on shared storage such as ACFS or NFS.
It is not necessary to create the wallets in Oracle home, I just did so for convenience.
As I just used the default Virginia region for SES setup, the command line shown is somewhat different than that in the aforementioned documentation.
A list of the SES servers is found here SES SMTP Servers
It will be necessary to add server certificates to the Oracle wallet to allow sending email; there are at least a couple of methods to discover the certificates needed for the Oracle Wallet.
This Website can generate the exact certificates needed for the chain What’s My Chain Cert?
The email server name email-smtp.us-east-1.amazonaws.com was plugged into the text box, and the resulting cert saved as email-smtp.us-east-1.amazonaws.com.chain.crt
The following commands were then used to create the Oracle Wallet.[sourcecode language="bash"] mkdir -p $ORACLE_HOME/owm/wallets/oracle orapki wallet create -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses orapki wallet add -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses -trusted_cert -cert email-smtp.us-east-1.amazonaws.com.chain.crt $ orapki wallet display -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses Oracle PKI Tool : Version 220.127.116.11.0 Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. Requested Certificates: User Certificates: Trusted Certificates: Subject: CN=Amazon,OU=Server CA 1B,O=Amazon,C=US [/sourcecode]
Another method is to get the cert chain with the openssl and s_client programs.
The output is the complete chain of certificates for email-smtp.us-east-1.amazonaws.com.
The output is rather lengthy, and only partially reproduced here:[sourcecode language="bash"] $ openssl s_client -host email-smtp.us-east-1.amazonaws.com -port 587 -prexit -showcerts -crlf -starttls smtp CONNECTED(00000003) depth=4 C = US, O = "Starfield Technologies, Inc.", OU = Starfield Class 2 Certification Authority verify return:1 depth=3 C = US, ST = Arizona, L = Scottsdale, O = "Starfield Technologies, Inc.", CN = Starfield Services Root Certificate Authority - G2 verify return:1 depth=2 C = US, O = Amazon, CN = Amazon Root CA 1 verify return:1 depth=1 C = US, O = Amazon, OU = Server CA 1B, CN = Amazon verify return:1 depth=0 CN = email-smtp.us-east-1.amazonaws.com verify return:1 --- Certificate chain 0 s:/CN=email-smtp.us-east-1.amazonaws.com i:/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon -----BEGIN CERTIFICATE----- MIIEhTCCA22gAwIBAgIQBJSyqvZcwNT12er++XeOTTANBgkqhkiG9w0BAQsFADBG MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRUwEwYDVQQLEwxTZXJ2ZXIg ... rr/Nhhm2i6y9X6iIBB3dCT8kIYN5wJ6aeb4Dote2VFs4Ih0Q3+Y5vhqgbpZWq5pL nmTKJdIfH3BIe/POr9XE2MUPLQ0dPN8zPtXRg49xwiPeqDku5NiklXfhkzwmcjsA AxedQsGiLnEg -----END CERTIFICATE----- 1 s:/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon i:/C=US/O=Amazon/CN=Amazon Root CA 1 -----BEGIN CERTIFICATE----- MIIESTCCAzGgAwIBAgITBn+UV4WH6Kx33rJTMlu8mYtWDTANBgkqhkiG9w0BAQsF ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6 ... upRyzQ7qDn1X8nn8N8V7YJ6y68AtkHcNSRAnpTitxBKjtKPISLMVCx7i4hncxHZS yLyKQXhw2W2Xs0qLeC1etA+jTGDK4UfLeC0SF7FSi8o5LL21L8IzApar2pR/ -----END CERTIFICATE----- 2 s:/C=US/O=Amazon/CN=Amazon Root CA 1 i:/C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./CN=Starfield Services Root Certificate Authority - G2 -----BEGIN CERTIFICATE----- MIIEkjCCA3qgAwIBAgITBn+USionzfP6wq4rAfkI7rnExjANBgkqhkiG9w0BAQsF ADCBmDELMAkGA1UEBhMCVVMxEDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNj ... bRRYh5TmOTFffHPLkIhqhBGWJ6bt2YFGpn6jcgAKUj6DiAdjd4lpFw85hdKrCEVN 0FE6/V1dN2RMfjCyVSRCnTawXZwXgWHxyvkQAiSr6w10kY17RSlQOYiypok1JR4U akcjMS9cmvqtmg5iUaQqqcT5NJ0hGA== -----END CERTIFICATE----- 3 s:/C=US/ST=Arizona/L=Scottsdale/O=Starfield Technologies, Inc./CN=Starfield Services Root Certificate Authority - G2 i:/C=US/O=Starfield Technologies, Inc./OU=Starfield Class 2 Certification Authority -----BEGIN CERTIFICATE----- MIIEdTCCA12gAwIBAgIJAKcOSkw0grd/MA0GCSqGSIb3DQEBCwUAMGgxCzAJBgNV BAYTAlVTMSUwIwYDVQQKExxTdGFyZmllbGQgVGVjaG5vbG9naWVzLCBJbmMuMTIw ... 59vPr5KW7ySaNRB6nJHGDn2Z9j8Z3/VyVOEVqQdZe4O/Ui5GjLIAZHYcSNPYeehu VsyuLAOQ1xk4meTKCRlb/weWsKh/NEnfVqn3sF/tM+2MR7cwA130A4w= -----END CERTIFICATE----- --- Server certificate subject=/CN=email-smtp.us-east-1.amazonaws.com issuer=/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon --- No client certificate CA names sent Peer signing digest: SHA512 Server Temp Key: ECDH, P-256, 256 bits --- SSL handshake has read 5292 bytes and written 466 bytes [/sourcecode]
For some reason the cert chain is shown twice in the output, though it has been truncated here for this article.
If you were to compare the chain displayed by openssl s_client you to the output from What’s My Chain Cert?] you would find that the difference is the openssl command displayed both the Root Certificate and the certificate for the smtp server itself. Neither of these are necessary, but you can choose to include them.
I chose to just copy all the text from the beginning of the first certificate to the end of the last certificate.
There was no need to remove the data between the certs.[sourcecode language="bash"] Certificate chain 0 s:/CN=email-smtp.us-east-1.amazonaws.com i:/C=US/O=Amazon/OU=Server CA 1B/CN=Amazon -----BEGIN CERTIFICATE----- MIIEhTCCA22gAwIBAgIQBJSyqvZcwNT12er++XeOTTANBgkqhkiG9w0BAQsFADBG MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRUwEwYDVQQLEwxTZXJ2ZXIg ... 59vPr5KW7ySaNRB6nJHGDn2Z9j8Z3/VyVOEVqQdZe4O/Ui5GjLIAZHYcSNPYeehu VsyuLAOQ1xk4meTKCRlb/weWsKh/NEnfVqn3sF/tM+2MR7cwA130A4w= -----END CERTIFICATE----- [/sourcecode]
The file was saved as email-smtp-us-east-1.aws.openssl.crt, and a new Oracle wallet created.[sourcecode language="bash"] orapki wallet create -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses-test orapki wallet add -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses -trusted_cert -cert email-smtp-us-east-1.aws.openssl.crt $ orapki wallet display -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses-test Oracle PKI Tool : Version 18.104.22.168.0 Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved. Requested Certificates: User Certificates: Trusted Certificates: Subject: CN=Amazon,OU=Server CA 1B,O=Amazon,C=US [/sourcecode]
Both wallets were successfully used to send emails in subsequent tests.
Enable auto login on a wallet if you need to. The command is the same as for creating a wallet:[sourcecode language="bash"] orapki wallet create -wallet $ORACLE_HOME/owm/wallets/oracle/aws-ses -auto_login [/sourcecode]
To disable auto login, remove the cwallet file.
In this case it would be $ORACLE_HOME/owm/wallets/oracle/aws-ses/cwallet.sso.
It will be necessary to create an Oracle ACL for so we are allowed to access the mail server via UTL_SMTP.
Oracle Note _ 1209644.1_ has directions that may be followed to do create the ACL.
In this case I created a role SMTP, granted the ACL to the role, and granted the SMTP role to the DBA role.
(DBA by default cannot bypass the ACL restrictions)
At this time everything should be adequately configured to send an email.
Not having much experience with SMTP administration, some things that might seem obvious to others took me a little time to figure out.
One of the issues was which port to use. SES states there are three ports that may be used; 25, 465 and 587.
As the instructions at https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-smtp-client-command-line.html recommend port 587, that is the port I will use.
Oracle Support has several notes on using utl_smtp; I found the following two notes helpful.
- Basics on How to use UTL_SMTP (Doc ID 106513.1)
- How to Send an Email Using SMTP over an SSL Connection (Doc ID 1323140.1)
The following code is an amalgamation of the code from these Oracle support notes, and slightly modified. The SES username and password are obfuscated, as are the email addresses.
This code is for prototype purposes only, and not suitable for production use.
Here is the outout from sending a single email:[sourcecode language="sql"] ============ Starting TLS =============== Code: 220 Text: Ready to start TLS ============ Authorizing =============== Code: 235 Text: Authentication successful. ============ Helo =============== Code: 250 Text: email-smtp.amazonaws.com ============ Mail =============== Code: 250 Text: Ok ============ Rcpt =============== Code: 250 Text: Ok ============ Open Data =============== Code: 354 Text: End data with <CR><LF>.<CR><LF> ============ Close Data =============== Code: 250 Text: Ok 0100016684a64fdd-d138c4bf-6357-41d7-ad05-12636097c355-000000 ============ Quit =============== Code: 221 Text: Bye [/sourcecode]
The following references are helpful in recognizing a normal SMTP conversation
For some as yet unknown reason, this can be quite slow, with runtimes varying between 1.7 and 30 seconds. This may be due to still running in the SES Sandbox.
In my initial tests a year ago, the emails would appear in my inbox shortly after sending them via this PL/SQL program and AWS SES.
During current testing however I have yet to receive an email.
The AWS console shows that several emails have been sent, and that none are being bounced.
As per the [Delivery Problems] documentation, there are many possible causes of this.
Following is the email received in previous testing.[sourcecode language="bash"] Test email from Amazon SES 2017-09-20 19:54:10 This is a test message using SSL with SMTP. This test requires an Oracle Wallet be properly configured. Message-ID: &lt;firstname.lastname@example.org&gt; Date: Wed, 20 Sep 2017 23:54:10 +0000 X-SES-Outgoing: 2017.09.20-22.214.171.124 Feedback-ID: 1.us-east-1.8u4uk618KvmcX7PFgcf5RV8XdAP6EJBn2CuNLBx6me4=:AmazonSES [/sourcecode]
Maybe I should print this email, frame it and hang it on the wall, as getting to this point was a bit of a slog.
(Now I think I will hold off on that for an email from the new configuration )
If you have need for sending emails directly from an Oracle database, and do not have an available email server, SES may just be the answer you are looking for.