Lorenzo's blog

Technical reference about work stuff

Send emails from CLI using Oracle Cloud SMTP service

🗓️ Date: 2025-02-16 · 🗺️ Word count: 678 · ⏱️ Reading time: 4 Minute

Being able to send emails within an automation script can be very useful, especially for notifying the server admin for issues.

In many unix systems, the mail utility can be configured both with a local and an external SMTP server.

In this guide, we’ll see how to use the Email Delivery service in Oracle Cloud and configure the mail utility to deliver emails through it.

Prerequisites

  • mail CLI utility installed in your system. The package name depends on the system. For instance, it can be mailx, s-nail or others.
  • an Oracle Cloud tenancy
  • access to the Email Delivery service in the Oracle Cloud tenancy
  • a registered domain name on a DNS registrar

Configure the Oracle Cloud Email Delivery service

1. Create an “Email Domain” in the Oracle Cloud “Email Delivery” service.

This needs to be the same as the domain in the sender address.

Email Domain

2. Create an “Approved Sender” for the domain.

This needs to be same as the sender address that will be used when sending emails.

Approved Sender

Make sure it is present in the domain configuration.

Approved Sender

3. Configure DKIM for the domain.

This is necessary to make sure the emails delivered by the service won’t be flagged as spam.

Click the “Add DKIM” button, then choose a string as the DKIM Selector. This can be your choice, for instance dkim-prefix. A key and a value will be produced.

DKIM

Insert these in a new CNAME record on your DNS registrar.

Wait for the validation, until the “DKIM Signing Status” becomes active.

DKIM

4. Take note of the SMTP configuration.

The values will change according to the OCI region in use.

DKIM

5. Create a SMTP credential for a user of the OCI tenancy.

Go to the user’s profile, in the SMTP credentials section. Click on “Generate credentials” and assign a name.

A user string and a password will be generated.

Configure the mail utility

Edit the configuration file

Usually, the mail utility is configured via the ~/.mailrc file.

Append the lines below at the end of the configuration file. For the first part of the file, see the Appendix.

set from=no-reply@yourdomain.com # this is the approved sender address
set smtp-use-starttls
set smtp=smtp.email.eu-zurich-1.oci.oraclecloud.com:587 # SMTP Public Endpoint from the configuration
set smtp-auth=plain
set smtp-auth-user=<oracle-cloud provided>        # SMTP user credentials
set smtp-auth-password=<oracle-cloud provided>    # SMTP user password
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

Send mail

echo "This is the email body" | mail -v -s "This is the email subject" destination@domain.com

Appendix

First part of the .mailrc file.

# Do not forward to mbox by default since this is likely to be
# irritating for most users today.
set hold

# Append rather than prepend when writing to mbox automatically.
# This has no effect unless 'hold' is unset again.
set append

# Ask for a message subject.
set ask

# Assume a CRT-like terminal and invoke a pager.
set crt

# Messages may be terminated by a dot.
set dot

# Do not remove empty mail folders in the spool directory.
# This may be relevant for privacy since other users could
# otherwise create them with different permissions.
set keep

# Do not remove empty private mail folders.
set emptybox

# Quote the original message in replies by "> " as usual on the Internet.
set indentprefix="> "

# Automatically quote the text of the message that is responded to.
set quote

# Outgoing messages are sent in ISO-8859-1 if all their characters are
# representable in it, otherwise in UTF-8.
set sendcharsets=iso-8859-1,utf-8

# Display sender's real names in header summaries.
set showname

# Display the recipients of messages sent by the user himself in
# header summaries.
set showto

# Automatically check for new messages at each prompt, but avoid polling
# of IMAP servers or maildir folders.
set newmail=nopoll

# If threaded mode is activated, automatically collapse thread.
set autocollapse

# Mark messages that have been answered.
set markanswered

# Hide some header fields which are uninteresting for most human readers.
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding

# For Linux and BSD, this should be set.
set bsdcompat