|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + This script provides a basic example of how to use the Email Service in the TypeScript SDK. |
| 8 | + This will demonstrate: |
| 9 | + * Creating, retrieving, updating, listing, and deleting email senders |
| 10 | + * Creating, retrieving, listing, and deleting email suppressions |
| 11 | + * Creating, updating, listing, and deleting SMTP credentials. |
| 12 | + See https://docs.cloud.oracle.com/iaas/Content/Email/Tasks/configuresmtpconnection.htm for more |
| 13 | + information on sending emails with your IAM user |
| 14 | +
|
| 15 | + This script accepts four arguments: |
| 16 | + * The compartment ID where email senders will be created |
| 17 | + * The target compartmentId to change compartments |
| 18 | + * The address of the email sender |
| 19 | + * The address of the email suppression |
| 20 | +
|
| 21 | + Note that email senders are created in the compartment which you specify, but the suppressions are always created at the tenancy |
| 22 | + level. The tenancy will be read from your configuration file. |
| 23 | +*/ |
| 24 | + |
| 25 | +const identity = require("oci-identity"); |
| 26 | +const common = require("oci-common"); |
| 27 | +const email = require("oci-email"); |
| 28 | + |
| 29 | +const provider = new common.ConfigFileAuthenticationDetailsProvider(); |
| 30 | + |
| 31 | +const args = process.argv.slice(2); |
| 32 | +if (args.length !== 4) { |
| 33 | + console.error( |
| 34 | + "Unexpected number of arguments received. Consult the script header comments for expected arguments" |
| 35 | + ); |
| 36 | + process.exit(-1); |
| 37 | +} |
| 38 | + |
| 39 | +const compartmentId = args[0]; |
| 40 | +const targetCompartmentId = args[1]; |
| 41 | +const senderEmailAddress = args[2]; |
| 42 | +const suppressionEmailAddress = args[3]; |
| 43 | + |
| 44 | +const emailClient = new email.EmailClient({ authenticationDetailsProvider: provider }); |
| 45 | +const identityClient = new identity.IdentityClient({ authenticationDetailsProvider: provider }); |
| 46 | + |
| 47 | +async function delay(s) { |
| 48 | + return new Promise(resolve => setTimeout(resolve, s * 1000)); |
| 49 | +} |
| 50 | + |
| 51 | +async function createEmailSender(compartmentId, senderEmailAddress) { |
| 52 | + const request = { |
| 53 | + createSenderDetails: { |
| 54 | + compartmentId: compartmentId, |
| 55 | + emailAddress: senderEmailAddress |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + // Try to add a generic waiter here or use delay of 2min 30s |
| 60 | + const response = await emailClient.createSender(request); |
| 61 | + await delay(240); // Give some time to createSender. |
| 62 | + console.log("Created Sender: ", response); |
| 63 | + return response; |
| 64 | +} |
| 65 | + |
| 66 | +async function updateEmailSender(sender) { |
| 67 | + const request = { |
| 68 | + senderId: sender.sender.id, |
| 69 | + updateSenderDetails: { |
| 70 | + freeformTags: { |
| 71 | + department: "Finance" |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + const response = await emailClient.updateSender(request); |
| 76 | + console.log("Updated Sender with freeformTags: ", response.sender.freeformTags); |
| 77 | + return response; |
| 78 | +} |
| 79 | + |
| 80 | +async function changeSenderCompartment(sender) { |
| 81 | + const request = { |
| 82 | + senderId: sender.sender.id, |
| 83 | + changeSenderCompartmentDetails: { |
| 84 | + compartmentId: targetCompartmentId |
| 85 | + } |
| 86 | + }; |
| 87 | + const response = await emailClient.changeSenderCompartment(request); |
| 88 | + console.log(`Change sender compartment to: ${targetCompartmentId}`); |
| 89 | + return response; |
| 90 | +} |
| 91 | + |
| 92 | +async function listAllSender(compartmentId, emailAddress) { |
| 93 | + const request = { |
| 94 | + compartmentId: compartmentId |
| 95 | + }; |
| 96 | + if (emailAddress) { |
| 97 | + request["emailAddress"] = emailAddress; |
| 98 | + } |
| 99 | + |
| 100 | + const senders = await common.paginatedRecordsWithLimit(request, req => |
| 101 | + emailClient.listSenders(request) |
| 102 | + ); |
| 103 | + console.log("Listing all senders: "); |
| 104 | + senders.forEach(sender => { |
| 105 | + console.log("Sender: ", sender); |
| 106 | + }); |
| 107 | + return; |
| 108 | +} |
| 109 | + |
| 110 | +async function createSuppresion(compartmentId, emailAddress) { |
| 111 | + const request = { |
| 112 | + createSuppressionDetails: { |
| 113 | + compartmentId: compartmentId, |
| 114 | + emailAddress: emailAddress |
| 115 | + } |
| 116 | + }; |
| 117 | + const response = await emailClient.createSuppression(request); |
| 118 | + console.log("Created suppression: ", response.suppression); |
| 119 | + return response; |
| 120 | +} |
| 121 | + |
| 122 | +async function listAllSuppressions(compartmentId, emailAddress) { |
| 123 | + const request = { |
| 124 | + compartmentId: compartmentId |
| 125 | + }; |
| 126 | + if (emailAddress) { |
| 127 | + request["emailAddress"] = emailAddress; |
| 128 | + } |
| 129 | + const suppressions = await common.paginatedRecordsWithLimit(request, req => |
| 130 | + emailClient.listSuppressions(request) |
| 131 | + ); |
| 132 | + console.log("List suppressions: "); |
| 133 | + suppressions.forEach(suppression => { |
| 134 | + console.log("Suppression: ", suppression); |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +async function deleteSender(sender) { |
| 139 | + const request = { |
| 140 | + senderId: sender.sender.id |
| 141 | + }; |
| 142 | + await emailClient.deleteSender(request); |
| 143 | + console.log("Deleted Sender."); |
| 144 | + return; |
| 145 | +} |
| 146 | + |
| 147 | +async function deleteSuppression(suppression) { |
| 148 | + const request = { |
| 149 | + suppressionId: suppression.suppression.id |
| 150 | + }; |
| 151 | + await emailClient.deleteSuppression(request); |
| 152 | + console.log("Deleted Supression."); |
| 153 | + return; |
| 154 | +} |
| 155 | + |
| 156 | +async function createSmtpCredential() { |
| 157 | + const request = { |
| 158 | + userId: provider.getUser(), |
| 159 | + createSmtpCredentialDetails: { |
| 160 | + description: "New Credentials" |
| 161 | + } |
| 162 | + }; |
| 163 | + const smptCredential = await identityClient.createSmtpCredential(request); |
| 164 | + console.log("Created smptCredential: ", smptCredential); |
| 165 | + return smptCredential; |
| 166 | +} |
| 167 | + |
| 168 | +async function updateSmtpCredential(smptCredential) { |
| 169 | + const request = { |
| 170 | + userId: provider.getUser(), |
| 171 | + smtpCredentialId: smptCredential.smtpCredential.id, |
| 172 | + updateSmtpCredentialDetails: { |
| 173 | + description: "Updated Credentials" |
| 174 | + } |
| 175 | + }; |
| 176 | + const updatedSmtpCredential = await identityClient.updateSmtpCredential(request); |
| 177 | + console.log("Updated SMTP Credential: ", updatedSmtpCredential.smtpCredentialSummary); |
| 178 | + return; |
| 179 | +} |
| 180 | + |
| 181 | +async function listSmtpCredentials() { |
| 182 | + const request = { |
| 183 | + userId: provider.getUser() |
| 184 | + }; |
| 185 | + const smtpCredentialsResponse = await identityClient.listSmtpCredentials(request); |
| 186 | + console.log("SMTP Credentials for user: ", smtpCredentialsResponse); |
| 187 | + return; |
| 188 | +} |
| 189 | + |
| 190 | +async function deleteSmtpCredential(smptCredential) { |
| 191 | + const request = { |
| 192 | + userId: provider.getUser(), |
| 193 | + smtpCredentialId: smptCredential.smtpCredential.id |
| 194 | + }; |
| 195 | + await identityClient.deleteSmtpCredential(request); |
| 196 | + console.log("Deleted SMTP Credential"); |
| 197 | + return; |
| 198 | +} |
| 199 | + |
| 200 | +(async () => { |
| 201 | + // Create a sender |
| 202 | + const sender = await createEmailSender(compartmentId, senderEmailAddress); |
| 203 | + |
| 204 | + // Update sender |
| 205 | + await updateEmailSender(sender); |
| 206 | + |
| 207 | + // Change sender compartment |
| 208 | + await changeSenderCompartment(sender); |
| 209 | + |
| 210 | + // List all senders in a compartment |
| 211 | + await listAllSender(compartmentId, ""); |
| 212 | + |
| 213 | + // List all senders in a compartment filtered by email address |
| 214 | + await listAllSender(compartmentId, senderEmailAddress); |
| 215 | + |
| 216 | + // Create Suppression |
| 217 | + const suppression = await createSuppresion(compartmentId, suppressionEmailAddress); |
| 218 | + |
| 219 | + // List Suppression in a compartment |
| 220 | + await listAllSuppressions(compartmentId, ""); |
| 221 | + |
| 222 | + // List Suppression in a compartment filtered by email address |
| 223 | + await listAllSuppressions(compartmentId, suppressionEmailAddress); |
| 224 | + |
| 225 | + // Delete Sender |
| 226 | + await deleteSender(sender); |
| 227 | + |
| 228 | + // Delete Suppression |
| 229 | + await deleteSuppression(suppression); |
| 230 | + |
| 231 | + /* |
| 232 | + * In order to send email, we'll need to create an SMTP credential associated with an IAM user. More |
| 233 | + * information on sending email can be found here: https://docs.cloud.oracle.com/Content/Email/Tasks/configuresmtpconnection.htm |
| 234 | + * |
| 235 | + * Note, also, that an IAM user can only have two active SMTP credentials at any time |
| 236 | + * Also the password for the SMTP credential is ONLY available in the create response, so you should store/save this as it won't be retrievable later |
| 237 | + */ |
| 238 | + const smtpCredential = await createSmtpCredential(); |
| 239 | + |
| 240 | + // Update the description of an SMTP credential |
| 241 | + await updateSmtpCredential(smtpCredential); |
| 242 | + |
| 243 | + // List the credentials for a user |
| 244 | + await listSmtpCredentials(); |
| 245 | + |
| 246 | + // Delete SMTP credential |
| 247 | + await deleteSmtpCredential(smtpCredential); |
| 248 | +})(); |
0 commit comments