Closed jedsmith13 closed 9 months ago
Same issue here
Same issue here, with ejsadapter too. Tried with node 14 and node 15.
Looking at lib/adapters/handlebars.adapter.ts I've figured it out: the mail template must start wit './'
! I'm using 1.6.0.
createMailerModuleOptions(): MailerOptions {
const options =
{
transport: {
host: process.env.MAIL_HOST,
port: process.env.MAIL_PORT,
secure: false, // TODO - Upgrade later with STARTTLS
ignoreTLS: true,
},
defaults: {
from: process.env.MAIL_FROM_ADDRESS,
},
template: {
dir: `${process.cwd()}/src/shared/provider/email/template/`,
adapter: new HandlebarsAdapter(),
options: {
strict: true,
},
},
};
return options;
}
public test(): void {
this
.mailerService
.sendMail({
to: 'me@myself.com',
subject: 'Testing Nest Mailermodule with template ✔',
template: `./index.hbs`,
context: {
code: 'cf1a3f828287',
username: 'john doe',
},
})
.then((success) => {
console.log(success)
})
.catch((err) => {
console.log(err)
});
}
}
That's really obscure. The existence of a global template option made think that the email template option should be only 'index'
, not './index'
.
Looking at lib/adapters/handlebars.adapter.ts I've figured it out: the mail template must start wit
'./'
! I'm using 1.6.0.createMailerModuleOptions(): MailerOptions { const options = { transport: { host: process.env.MAIL_HOST, port: process.env.MAIL_PORT, secure: false, // TODO - Upgrade later with STARTTLS ignoreTLS: true, }, defaults: { from: process.env.MAIL_FROM_ADDRESS, }, template: { dir: `${process.cwd()}/src/shared/provider/email/template/`, adapter: new HandlebarsAdapter(), options: { strict: true, }, }, }; return options; } public test(): void { this .mailerService .sendMail({ to: 'me@myself.com', subject: 'Testing Nest Mailermodule with template ✔', template: `./index.hbs`, context: { code: 'cf1a3f828287', username: 'john doe', }, }) .then((success) => { console.log(success) }) .catch((err) => { console.log(err) }); } }
That's really obscure. The existence of a global template option made think that the email template option should be only
'index'
, not'./index'
.
That was it! Thanks a lot!
That's a breaking change with v1.6.0 With version 1.5.1 its fine using templates like bellow
this.mailerService.sendMail({
...
template: 'password-reset',
...
});
with version 1.6.0 it has to be changed to
this.mailerService.sendMail({
...
template: './password-reset',
...
});
as @lzkill mentioned already
Yeah, this is not intuitive at all, and if you read the code it makes you think the template is in the same directory as the usage.
for me trick with ./
didn't worked, I have switched to 1.5.1
and
template: {
dir: join(__dirname, 'templates'),
adapter: new HandlebarsAdapter(), // or EJS adapter
options: { strict: true },
},
worked for me
My directory
├─ src
│ ├─ app.controller.ts
│ ├─ app.module.ts
│ ├─ app.service.ts
│ ├─ common
│ │ └─ middleware
│ │ └─ logger.middleware.ts
│ ├─ main.ts
│ ├─ module
│ │ ├─ email
│ │ │ ├─ email.controller.ts
│ │ │ ├─ email.module.ts
│ │ │ └─ email.service.ts
│ │ └─ user
│ │ ├─ user.controller.ts
│ │ ├─ user.entity.ts
│ │ ├─ user.module.ts
│ │ └─ user.service.ts
│ └─ templates
│ └─ welcome.pug
The project needs to be packaged at the end, so package the template static file in the dist directory. Remember to add the configuration in the nest-cli.json
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
// Add this configuration
"compilerOptions": {
"assets": ["templates/**/*"]
}
}
this.mailerService.sendMail({
to: 'xxxxx@x.com',
from: 'yyyyy@yy.com',
subject: 'test',
// I wrote it like this before template: 'welcome.pug', but I failed
// Then I changed it to look like this
template: './welcome.pug'
})
.then((res) => { console.log(res) })
.catch((e) => { console.log(e) });
useFactory: () => ({
transport: 'smtps://xxx@xx.com:pass@smtp.xx.com',
defaults: {
from: '"nest-modules" <modules@nestjs.com>'
},
template: {
dir: path.join(__dirname, 'templates'),
adapter: new PugAdapter(),
options: {
strict: true
}
}
})
Config:
Service:
This worked with
1.5.1
but on upgrading to1.6.0
it stopped working. However, if I changed the template to,it works again.
The error thrown is: