0

I am using nodemailer to send HTML formatted email but cannot include script tag inside HTML.

const emailFormat = `
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Document</title>
    <style>
    .demo{
      border: 5px solid red;
      border-radius: 8px;
      width: 200px;
      text-align: center;
    }
  </style>
    <script>
      document.getElementById("demo").innerHTML = "I have changed!";
    </script>
  </head>
  <body>
      <div class="demo">"name"</div>
      <div>"desc"</div>
  </body>
</html>
`;

sending email like this using nodemailer

  let info = await transporter.sendMail({
    from: `"Proud Poshak" <${employeeEmail}>`, // sender address
    to: userEmail, // list of receivers
    subject: "Hello world", // Subject line
    html: emailFormat, // html body
  });

the above html does not read script tag, whether i put it inside head or body. How can I fix this?
Additionally, as I am sending email from node.js using nodemailer. Is there a better way to require entire html file and just pass that file that way I wont have to write html inside template string ?

Aman Chaudhary
  • 802
  • 3
  • 10
  • 27
  • What exactly do you mean with `the above html does not read script tag`? That the email client does not execute the script? (There is no email client that I know that would execute any script included in the email.) – t.niese May 03 '22 at 08:20
  • I simply mean html does not read the script tag. I mean html does not execute it. And i cannot set changed text in the body of html – Aman Chaudhary May 03 '22 at 08:22
  • ` html does not read the script tag.` does not make any sense. Does this mean that the `script` tag is not in the resulting HTML? Is it in the resulting HTML but not evaluated and executed? – t.niese May 03 '22 at 08:26
  • ``` ``` in simple term this tag should make the `demo` element text to `I have changed!` but it does not. – Aman Chaudhary May 03 '22 at 08:29
  • 1
    So the `script` tag **is** in the html code but not executed? And again the question from my first comment. Where? In the E-Mail Program? If so, then yes that's intended, to prevent scamming, phishing, and any other malicious things. – t.niese May 03 '22 at 08:35
  • Yes i was using in the email program. @t.niese thanks for this just a follow up. have a look at this link https://www.papareact.com/e/BAh7BjoWZW1haWxfZGVsaXZlcnlfaWRsKwg4KcuRAQA%3D--b8244fec7eae846221b57ea742a7d97b5c101b94?skip_click_tracking=true I got email and as you see in web browser, I see exactly in the email. I don't think there is no use of js here. So, how can i send similar email ? – Aman Chaudhary May 03 '22 at 08:40
  • 1
    `I don't think there is no use of js here` you should not assume whether or not javascript is used. Deactivate JavaScript in your browser and verify if it then still looks the same. And as you can see there is no JavaScript used. – t.niese May 03 '22 at 08:47

0 Answers0