forwardemail / email-templates

Create, preview (browser/iOS Simulator), and send custom email templates for Node.js. Made for @forwardemail, @ladjs, @cabinjs, @spamscanner, and @breejs.
https://forwardemail.net/docs/send-emails-with-node-js-javascript
MIT License
3.67k stars 337 forks source link

Error: No content was passed for subject, html, text, nor attachments message props #360

Closed TidyIQ closed 5 years ago

TidyIQ commented 5 years ago

Hi, I'm not sure why this isn't working.

dist/emails/index.ts

const transporter = nodemailer.createTransport({
  host: "smtp.gmail.com",
  port: 465,
  secure: true,
  auth: {
    type: "OAuth2",
    user: process.env.GOOGLE_USER,
    clientId: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    refreshToken: process.env.GOOGLE_REFRESH_TOKEN
  }
});

const email = new Email({
  message: {
    from: "REDACTED<support@REDACTED.com>"
  },
  transport: transporter
});

export const sendEmailConfirmation: SendEmailConfirmation = (to, name, token) =>
  email
    .send({
      template: path.join(__dirname, "emailConfirmation"),
      message: {
        to
      },
      locals: {
        name,
        token
      }
    })
    .then((): boolean => true)
    .catch(
      (err: Error): boolean => {
        console.log("error in sendEmailConfirmation:", err);
        return false;
      }
    );

dist/emails/emailConfirmation/html.pug

p Hi #{name},
p Please confirm your email address by clicking the following link
a(href="https://www.REDACTED.com/confirm?token=#{token}") Test

dist/emails/emailConfirmation/subject.pug

= `Confirm your REDACTED account`

And when I call sendEmailConfirmation(email, name, token), I get the following error in console:

error in sendEmailConfirmation: Error: No content was passed for subject, html, text, nor attachments message props. Check that the files for the template "/var/www/REDACTED.com/dist/emails/emailConfirmation" exist.
TidyIQ commented 5 years ago

I switched over to handlebars instead and now it works. No idea what the issue is with pug...

niftylettuce commented 5 years ago

Duplicate issue. Please only open one.

blakejoy commented 5 years ago

Im getting this error working with ejs

prionkor commented 4 years ago

@blakejoy Was you able to resolve your issue? If you did please share. I am using ejs but having same error. My directory structure

- utils
-- Emailer.js

- emails
-- default
---- html.ejs
---- subject.ejs
---- text.ejs

The email function was called from Emailer.js


        // send email
        email.send({
            template: 'default',
            locals: {
                data: this.data,
                config: this.config,
            }
        });

Emailer is exported on a expressjs route under directory /routes

blakejoy commented 4 years ago

Yes it had to do with the correcting the relative path of where the templates are pulled from. @prionkor

mrsasuuBeast commented 2 years ago

Hi! I was having the same issue running an app on Firebase (Firebase functions). If somebody has the same problem try with this:

const email = new Email({
      views: {
        root: path.join('', 'src/SOME_FOLDER/email/templates/'),
      },
      message: {
        from: EMAIL_FROM,
      },
      transport: YOUR_TRANSPORTER,
    })
email.send({
      template,
      message: {
        to,
      },
      locals: {
        ...
      },
    })

Considering we have all the templates under that directory (templates) and each of them in its own folder.

chahidy commented 2 years ago

Yes it had to do with the correcting the relative path of where the templates are pulled from. @prionkor

how @prionkor said.

Use this example command to debug and find the correct relative path:

NODE_DEBUG=email-templates node app.js

https://www.npmjs.com/package/email-templates#usage