Categories
Oracle OCI

Sending emails through OCI with Powershell

I was trying to send emails through Oracle Cloud Infrastructure API with Powershell, but there aren’t many practical examples available on the open Internet.

Here is what I came up with:

# Import the required OCI PowerShell module
Import-Module OCI.PSModules
Import-Module OCI.PSModules.Emaildataplane

$Region = "YOUR_OCI_REGION" #for example us-phoenix-1
$Compartment = "YOUR_COMPARTMENT_OCID"
$MailSender = New-Object "Oci.EmaildataplaneService.Models.Sender"
$MailSender.SenderAddress = New-Object "Oci.EmaildataplaneService.Models.EmailAddress" -Property @{
    Email = "noreply@example.com"
    Name = "Noreply"
}
$MailSender.CompartmentId = $Compartment

$Recipient = New-Object "Oci.EmaildataplaneService.Models.Recipients"
$Recipient.To = @(
    (New-Object "Oci.EmaildataplaneService.Models.EmailAddress" -Property @{
        Email = "user1@example.com"
        Name = "User 1"
    }),
    (New-Object "Oci.EmaildataplaneService.Models.EmailAddress" -Property @{
        Email = "user2@example.com"
        Name = "User 2"
    })
)
$Subject = "Example subject"
$Body = @"
Example body
with
multiple
line
"@

$EmailDetails = New-Object "Oci.EmaildataplaneService.Models.SubmitEmailDetails" -Property @{
    Sender = $MailSender
    Recipients = $Recipient
    Subject = $Subject
    BodyText = $Body
 }
Invoke-OCIEmaildataplaneSubmitEmail -SubmitEmailDetails $EmailDetails -Region $Region

This assumes that the OCI powershell modules are installed and the default profile is configured.