0

I have an HTML email template saved in a .html file on my nodejs express app.

I am looking to send this as an email template as follows:

// create reusable transporter object using the default SMTP transport
        const transporter = nodemailer.createTransport({
            host: '...',
            port: 587,
            secure: false, // true for 465, false for other ports
            auth: {
                user: '...', // generated ethereal user
                pass: '...', // generated ethereal password
            },
        });

        // send mail with defined transport object
        const info = await transporter.sendMail({
            from: '"Testing " <donotreply@testing.com>', // sender address
            to: recipientAddress, // list of receivers
            subject, // Subject line
            text: striptags(html), // plain text body
            html, // html body
        });

The problem I'm facing is how do I load the HTML from my template file into the html JS variable I can use here. I tried the following code but this throws an exception:

const html = require('../path/to/file.html');

<!DOCTYPE html> SyntaxError: Unexpected token '<'

when attempting to run the server.

Alk
  • 5,215
  • 8
  • 47
  • 116
  • Are you using any templating engine with it? If yes, then you can refer to the following post: https://stackoverflow.com/questions/39489229/pass-variable-to-html-template-in-nodemailer – ytrkptl Apr 28 '21 at 00:03
  • Thanks for that - if you post an answer, happy to accept, in case that answer gets removed in the future. – Alk Apr 28 '21 at 08:49
  • Thank you. I would not have thought of posting that as an answer yet but after reading your comment, felt much better to do so. Thanks. – ytrkptl Apr 28 '21 at 10:30
  • I meant including an answer which explains the solution, not just a link to it - that way if someone else comes across this post in the future they'll be able to see the answer directly on the post without needing to follow a link which might be down – Alk Apr 28 '21 at 12:53
  • Ok, well, I thought StackOverflow generally does not want duplicate questions and answers and I've seen people simply provide a link to the answer as a comment if it was already answered elsewhere. Since the other post is also on StackOverflow, I don't think it would get deleted or the like. But oh well, I'll leave this alone I guess. Deleted my answer as a result. – ytrkptl Apr 28 '21 at 12:59
  • They don't - usually you'd link to it as a duplicate and close the question, but I don't think in this case its quite a duplicate as the question the other person asked is a bit different - it just happens that the solution for that question also applies here – Alk Apr 28 '21 at 13:04
  • @Alk, you were importing an HTML file into NodeJS! Note you can only require a Javascript/Node module. NodeJS will check if the file being required is a valid Javascript module and apparently an HTML file is not a Javascript module. An HTML file typically starts with '<' and hence the error " SyntaxError: Unexpected token '<'. If you want to read the html file content and manipulate it, please use the fs library to read the file. – bean May 11 '22 at 00:11

0 Answers0