Oracle Cloud: Email Delivery using Mailx
Tags: oracle, email, mailx
Published: 2023-06-06 01:54:32

_How to send an email through the Oracle Cloud Email Delivery service via Mailx in the Terminal_

The Oracle Cloud Infrastructure provides an [Email Delivery](https://www.oracle.com/cloud/networking/email-delivery/) service that enables users to send bulk emails to their recipients. The official documentation to get started lies [here](https://docs.oracle.com/en-us/iaas/Content/Email/Reference/gettingstarted.htm).

This tutorial lays out the basic steps to accomplishing email sending using this service via Mailx referencing the official tutorial and the linked YouTube [video](https://youtu.be/p0hi--rNfiM) by OraTube.

## Cost

From that same website, we see the current cost of sending emails. The [FAQ](https://www.oracle.com/cloud/networking/email-delivery/faq/) states one email is equivalent to 2 MB or one recipient in the To, CC, or BCC fields.


![Screenshot of Oracle Cloud website that shows the current price of the email delivery at $0.00 for the first 3100 emails and $0.085 for every 1000 emails above that](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhf2i86mwzuhckin6h2j.png)

_Screenshot of Oracle Cloud website that shows the current price of the email delivery at $0.00 for the first 3100 emails and $0.085 for every 1000 emails above that_

## Set up Email Delivery service on OCI

First, you need to set up the Email Delivery service on OCI.

1\.  Create an SMTP Credential. Click your profile picture and click **Generate credentials** in **SMTP credentials**. Make sure to save the user and password info:

![Screenshot - OCI SMTP credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymfxybqa75vrh29qupnw.png)

![Screenshot - OCI SMTP credentials generated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qv30hkxq0fdrsg1l4eci.png)

2\. Go to the Email Delivery service

![Searching for email in OCI console search bar](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47ov2uarlgkeo3knzmvc.png)

3\. Create an Approved Sender using an email in a server you control or a random email that may not even exist (I use fdurrani@ulhqlkypwzshkgqhwqfb.com which does not exist). This will be your From email.

Note that sending emails from unverified domains may lead to email bounces, quarantine, or restrictions of the recipient from receiving your email:

![Creating OCI approved sender process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpig5hxkzsmmojpxkzlg.png)

4\. Navigate to the Configuration tab of Email Delivery and copy the information on the page:

![OCI Email Delivery configuration page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yokmnqudg7j6xmv6hwq.png)

## Install Mailx

1.  Assuming you’re on Oracle Linux 8 (which you can create within an OCI Compute Instance), install Mailx in the Terminal using:

```sh
 sudo yum install mailx -y
```

2\. After installation, open the `/etc/mail.rc` file and add the following information (ignore the `...`). Save the file

```rc
...

#smtp settings
set nns-config-dir=/etc/pki/nssdb
set smtp-use-starttls
set smtp-auth=plain
set smtp=smtp.email.us-ashburn-1.oci.oraclecloud.com
set from=fdurrani@ulhqlkypwzshkgqhwqfb.com
set smtp-auth-user=ocid1.user.oc1..aaaaaaaaaznjxw5fvpuizguxf5avbsje6sbta7j53oh3gyhiorp73fq@ocid1.tenancy.oc1..aaaaaaaaaeve7ubmplo4cxobeukpwp2uaiuusekfkut3hcvblaanpa.go.com
set smtp-auth-password=0jfpB;uQpAjh_u!a
```

Be sure to change the last four lines with your credentials. smtp can be retrieved from the Configuration page, and the user and password can be retrieved from the SMTP credentials you created earlier.

## Send an email

Once you saved the mail.rc file, write this command in your Terminal, replacing your sender and recipient emails, smtp information, port, user, and password information appropriately.

```sh
echo "Test Monday morning Email" | mail -v -s "Send an email via mailx" -r "Faris<fdurrani@ulhqlkypwzshkgqhwqfb.com>" -S replyto="Faris><fdurrani@ulhqlkypwzshkgqhwqfb.com>" -S smtp="smtp.email.us-ashburn-1.oci.oraclecloud.com:25" -S smtp-use-starttls -S smtp-auth=plain -S smtp-auth-user='ocid1.user.oc1..aaaaaaaaaznjxw5fvpuizguxf5avbsje6sbta7j53oh3gyhiorp73fq@ocid1.tenancy.oc1..aaaaaaaaaeve7ubmplo4cxobeukpwp2uaiuusekfkut3hcvblaanpa.go.com' -S smtp-auth-password='0jfpB;uQpAjh_u!a' -S ssl-verify=ignore fdurrani@mythics.com
```

Click Enter. If authentication is successful, you will see this message in the Terminal:

![Terminal showing successful email sending](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1u3zbmi2h409ufz7ups.png)

Open your inbox and you will see your email (ignore the CAUTION line that was automatically added):

![Outlook showing email received](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbibxj96fl3nafhy7v65.png)

---

## References

1.  [Oracle Cloud Infrastructure: Email Delivery via mailx](https://youtu.be/p0hi--rNfiM)
2.  [Oracle Email Delivery](https://www.oracle.com/cloud/networking/email-delivery/)

***Safe harbor statement***
The information provided on this channel/article/story is solely intended for informational purposes and cannot be used as a part of any contractual agreement. The content does not guarantee the delivery of any material, code, or functionality, and should not be the sole basis for making purchasing decisions. The postings on this site are my own and do not necessarily reflect the views or work of Oracle or Mythics, LLC.

*This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).*