Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm making a node.js server which sends emails on demand. The variable "output" is what I want to send via email. When I use inline html it works fine, however I want to import a complete html file instead.

const { EmailClient } = require("@azure/communication-email");<br />
<br />
const connectionString = "<ACS_CONNECTION_STRING>";<br />
const sender = "<SENDER_EMAIL>";<br />
const toRecipients = {<br />
    to: [<br />
        { email: "<alice@contoso.com>", displayName: "Alice" },<br />
    ],<br />
};<br />
<br />
const client = new EmailClient(connectionString);<br />
const emailContent = {<br />
    subject: "Send email plain text - JS sample",<br />
  plainText: "",<br />
  // html: "<h3>Hi, this works</h3>", // WORKS<br />
  // html: "<object type="text/html" data="file.html"></object>", // // Doesn't work<br />
  html: "<link href="file.html" rel="import" />", // // Doesn't work<br />
};<br />
<br />
async function main() {<br />
  try {<br />
    const emailMessage = {<br />
      sender: sender,<br />
      content: emailContent,<br />
      importance: 'low',<br />
      recipients: toRecipients,<br />
    };<br />
<br />
    const sendResult = await client.send(emailMessage);<br />
<br />
    if (sendResult && sendResult.messageId) {<br />
        const messageId = sendResult.messageId;     <br />
        if (messageId === null || messageId === undefined) {<br />
        console.log("Message Id not found.");<br />
        return;<br />
      }<br />
<br />
      console.log("Send email success, MessageId :", messageId);<br />
<br />
      let counter = 0;<br />
      const statusInterval = setInterval(async function () {<br />
        counter++;<br />
        try {<br />
          const sendStatusResult = await client.getSendStatus(messageId);<br />
            if (sendStatusResult) {<br />
                console.log(`Email status for {${messageId}} : [${sendStatusResult.status}]`);<br />
                if (sendStatusResult.status.toLowerCase() !== "queued" || counter > 12) {<br />
              clearInterval(statusInterval);<br />
            }<br />
          }<br />
        } catch (e) {<br />
          console.log("Error in checking send mail status: ",e);<br />
        }<br />
      }, 5000);<br />
    } else {<br />
      console.error("Something went wrong when trying to send this email: ", sendResult);<br />
    }<br />
  } catch (e) {<br />
      console.log("################### Exception occurred while sending email #####################", e);<br />
  }<br />
}<br />
<br />
main();


Help is much appreciated.

What I have tried:

<br />
// html: "<h3>Hi, this works</h3>", // WORKS<br />
// html: "<object type="text/html" data="file.html"></object>", // // Doesn't work<br />
  html: "<link href="file.html" rel="import" />", // // Doesn't work<br />
Posted
Updated 21-Nov-22 0:31am

1 solution

You're not "importing" an HTML file; you're sending an HTML document which uses an old experimental hack to "import" external content, giving it a relative path to an HTML file.

Aside from the fact that this experiment was removed in February 2020[^], the relative path would almost certainly not exist on the recipient's computer or mail server. And if it did, and by some oversight their email client allowed the file to load, it's extremely unlikely that the file on their computer would have the same content as the file on your computer.

You need to read the contents of the file into a variable, and use that to construct your message body.

Reading files with Node.js[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900