-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (49 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { EmailClient, KnownEmailSendStatus } = require("@azure/communication-email");
const { AzureKeyCredential } = require("@azure/core-auth");
require("dotenv").config();
var key = new AzureKeyCredential(process.env.KEY_CREDENTIAL);
var endpoint = process.env.AZURE_ENDPOINT;
const emailClient = new EmailClient(endpoint, key);
async function main() {
const POLLER_WAIT_TIME = 10
try {
const message = {
senderAddress: "<donotreply@dreebz.com>",
content: {
subject: "Welcome to Azure Communication Services Email",
plainText: "This email message is sent from Azure Communication Services Email using the JavaScript SDK.",
},
recipients: {
to: [
{
address: `<${process.env.SUPPORT_MAIL}>`,
displayName: `<${process.env.SUPPORT_NAME}>`,
},
],
},
};
const poller = await emailClient.beginSend(message);
if (!poller.getOperationState().isStarted) {
throw "Poller was not started."
}
let timeElapsed = 0;
while (!poller.isDone()) {
poller.poll();
console.log("Email send polling in progress");
await new Promise(resolve => setTimeout(resolve, POLLER_WAIT_TIME * 1000));
timeElapsed += 10;
if (timeElapsed > 18 * POLLER_WAIT_TIME) {
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;
}
} catch (e) {
console.log(e);
}
}
main();