Created
April 24, 2023 13:11
-
-
Save tofran/adbf0ced80e102f2963ae9f48138acec to your computer and use it in GitHub Desktop.
Simple email sending demonstration using Azure Communication Services. Based on: https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/email/send-email and https://github.com/Azure-Samples/communication-services-javascript-quickstarts/tree/main/send-email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMMUNICATION_SERVICES_CONNECTION_STRING= | |
EMAIL_SENDER= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "sample-azure-email-sending", | |
"version": "0.0.1", | |
"type": "module", | |
"main": "index.js", | |
"scripts": { | |
"run": "./send-email.js" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@azure/communication-email": "^1.0.0", | |
"dotenv": "^16.0.3", | |
"inquirer": "^9.2.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EmailClient, KnownEmailSendStatus } from "@azure/communication-email"; | |
import inquirer from "inquirer"; | |
import dotenv from "dotenv"; | |
dotenv.config(); | |
const connectionString = process.env.COMMUNICATION_SERVICES_CONNECTION_STRING; | |
const emailClient = new EmailClient(connectionString); | |
async function sendEmail(destinationEmail, destinationName) { | |
const POLLER_WAIT_TIME_SECONDS = 10; | |
const message = { | |
senderAddress: process.env.EMAIL_SENDER, | |
content: { | |
subject: "Azure test email", | |
plainText: | |
"This email message is sent from Azure Communication Services Email using the JavaScript SDK.", | |
}, | |
recipients: { | |
to: [ | |
{ | |
address: destinationEmail, | |
displayName: destinationName, | |
}, | |
], | |
}, | |
}; | |
const poller = await emailClient.beginSend(message); | |
if (!poller.getOperationState().isStarted) { | |
throw "Poller was not started."; | |
} | |
// From now on, this code is just to demo that we can listen for events, | |
// and check the email status. It does not need to be implemented most situations, | |
// as we might not need the status to be reported (or may not want the long delay). | |
let timeElapsed = 0; | |
while (!poller.isDone()) { | |
poller.poll(); | |
console.log("Email send polling in progress"); | |
await new Promise((resolve) => | |
setTimeout(resolve, POLLER_WAIT_TIME_SECONDS * 1000) | |
); | |
timeElapsed += 10; | |
if (timeElapsed > 18 * POLLER_WAIT_TIME_SECONDS) { | |
throw "Polling timed out."; | |
} | |
} | |
if (poller.getResult().status === KnownEmailSendStatus.Succeeded) { | |
console.log( | |
`Successfully sent the email (operation id: ${poller.getResult().id})` | |
); | |
} else { | |
throw poller.getResult().error; | |
} | |
} | |
inquirer | |
.prompt([ | |
{ | |
type: "input", | |
name: "email", | |
message: "Destination email address:", | |
}, | |
{ | |
type: "input", | |
name: "name", | |
message: "Destination name:", | |
}, | |
]) | |
.then((answers) => { | |
sendEmail(answers.email, answers.name); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment