How to send bulk email using javascript by azure ACS?

How to send bulk email using javascript by Azure Communication Services

const { EmailClient } = require("@azure/communication-email");

const connectionString = "endpoint=https://callandsms.communication.azure.com/;accesskey=xxxxxxxxxxxxxxxxxxxxxxx";
const client = new EmailClient(connectionString);
const sender = "support@xxxxx.azurecomm.net";
const emailContent = {
  subject: "Send email quick start test- JS sample",
  plainText: "Test Email from JS Send Email Sample Application\n\n This email is part of testing of email communication service. \\n Best wishes",
  html: "<html><head><title>ACS Email as a Service</title></head><body><h1>ACS Email as a Service - Html body</h1><h2>This email is part of testing of email communication service</h2></body></html>",
};
const toRecipients = {
  to: [
    { email: "xxxxx@yahoo.com", displayName: "xxx" },
    { email: "xxxxx@gmail.com", displayName: "xxx" },
  ],
};

async function main() {
  try {
    const emailMessage = {
      sender: sender,
      content: emailContent,
      recipients: toRecipients,
    };

    const sendResult = await client.send(emailMessage);

    if (sendResult && sendResult.messageId) {
      // check mail status, wait for 5 seconds, check for 60 seconds.
      const messageId = sendResult.messageId;
      if (messageId === null) {
        console.log("Message Id not found.");
        return;
      }

      console.log("Send email success, MessageId :", messageId);

      let counter = 0;
      const statusInterval = setInterval(async function () {
        counter++;
        try {
          const response = await client.getSendStatus(messageId);
          if (response) {
            console.log(`Email status for {${messageId}} : [${response.status}]`);
            if (response.status.toLowerCase() !== "queued" || counter > 12) {
              clearInterval(statusInterval);
            }
          }
        } catch (e) {
          console.log("Error in checking send mail status: ",e);
        }
      }, 5000);
    } else {
      console.error("Something went wrong when trying to send this email: ", sendResult);
    }
  } catch (e) {
    console.log("################### Exception occoured while sending email #####################", e);
  }
}

main();
const { EmailClient } = require("@azure/communication-email");

const connectionString = "endpoint=https://callandsms.communication.azure.com/;accesskey=xxxxxxxxxxxxxxxxxxxxxxx";
const client = new EmailClient(connectionString);
const sender = "support@xxxxx.azurecomm.net";
const emailContent = {
  subject: "Send email quick start test- JS sample",
  plainText: "Test Email from JS Send Email Sample Application\n\n This email is part of testing of email communication service. \\n Best wishes",
  html: "<html><head><title>ACS Email as a Service</title></head><body><h1>ACS Email as a Service - Html body</h1><h2>This email is part of testing of email communication service</h2></body></html>",
};
const toRecipients = {
  to: [
    { email: "xxxxx@yahoo.com", displayName: "xxx" },
    { email: "xxxxx@gmail.com", displayName: "xxx" },
  ],
};

async function main() {
  try {
    const emailMessage = {
      sender: sender,
      content: emailContent,
      recipients: toRecipients,
    };

    const sendResult = await client.send(emailMessage);

    if (sendResult && sendResult.messageId) {
      // check mail status, wait for 5 seconds, check for 60 seconds.
      const messageId = sendResult.messageId;
      if (messageId === null) {
        console.log("Message Id not found.");
        return;
      }

      console.log("Send email success, MessageId :", messageId);

      let counter = 0;
      const statusInterval = setInterval(async function () {
        counter++;
        try {
          const response = await client.getSendStatus(messageId);
          if (response) {
            console.log(`Email status for {${messageId}} : [${response.status}]`);
            if (response.status.toLowerCase() !== "queued" || counter > 12) {
              clearInterval(statusInterval);
            }
          }
        } catch (e) {
          console.log("Error in checking send mail status: ",e);
        }
      }, 5000);
    } else {
      console.error("Something went wrong when trying to send this email: ", sendResult);
    }
  } catch (e) {
    console.log("################### Exception occoured while sending email #####################", e);
  }
}

main();

I want to send emails to many users without the recipients seeing who I’m sending

Is there a solution to send mail individually and import emails from a text or excel file?
I tried a lot and I couldn’t on my own

Please help and thank you in advance

You’ll want to loop the toRecipients and send emails to them one by one.

for (const recipient of toRecipients) {
   // send email to recipient
}
1 Like
let emailMessage = {sender: sender, content: emailContent};

for(let i = 0; i< toRecipients.to.length; i++) {
  emailMessage.recipients = {to: toRecipients.to[i] }; // single email
  const sendResult = await client.send(emailMessage);
}

I did that but I’m getting this error

################### Exception occoured while sending email ##################### Error: emailMessage.recipients.to must be of type Array.

This is what BCC was invented for.

That will work, but it always looks weird when you get one of those sent to “undisclosed recipients”. Also you can’t do any personalisation (not that we know it the OP wants that, it might not be a concern at all).

@abidiziko99 you can solve the error by wrapping the single recipient in an array, like so:

for(let i = 0; i< toRecipients.to.length; i++) {
  emailMessage.recipients = {to: [toRecipients.to[i]] }; // single email
  const sendResult = await client.send(emailMessage);
}
1 Like

Thank you this works

Is there a way to retrieve the message content from an external html file?

Because when I attach code here

HTML: “”

I get glitches

What is the you have now?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.