Reputation: 995
I am sending templated email using nodemailer
with nodemailer-express-handlebars
, but whenever I am trying to send a mail, I am getting the
Error: A partials dir must be a string or config object.
I don't know what is the problem.
const express = require('express');
const hbs = require('nodemailer-express-handlebars');
const nodemailer = require('nodemailer');
const app = express();
const user_name = '[email protected]';
const refresh_token = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const email_to = '[email protected]';
let transporter = nodemailer
.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
clientId: client_id,
clientSecret: client_secret
},
tls:{
rejectUnauthorized: false
}
});
transporter.use('compile', hbs({
viewPath: 'views/email',
extName: '.hbs'
}));
transporter.on('token', token => {
console.log('A new access token was generated');
console.log('User: %s', token.user);
console.log('Access Token: %s', token.accessToken);
console.log('Expires: %s', new Date(token.expires));
});
let mailOptions = {
from : user_name,
to : email_to,
subject : 'Hello ✔',
text : 'Hello world ?',
template: 'emailt',
context: {},
auth : {
user : user_name,
refreshToken : refresh_token,
expires : 1494388182480
}
};
// send mail with defined transport object
app.get('/', (req,res) => {
transporter.sendMail(mailOptions).then( r => {
res.send(r);
}).catch(e =>{
res.send(e);
});
});
app.listen(3000 ,()=>{
console.log('port: 3000');
});
My directory looks like this views>email>emailt.hbs
Upvotes: 3
Views: 7967
Reputation: 9865
I managed to get my code working, posting in case it helps out. I had to add the partialsDir
to my handlebarOptions
where I didn't have the field before:
const handlebarOptions = {
viewEngine: {
extName: '.hbs',
partialsDir: 'src/path',
layoutsDir: 'src/path',
defaultLayout: 'email.hbs',
},
viewPath: 'src/path',
extName: '.hbs',
};
transporter.use('compile', hbs(handlebarOptions));
Hope this helps.
Upvotes: 3
Reputation:
Looks like a recent change to express-handlebars caused this issue, someone posted a fix here https://github.com/yads/nodemailer-express-handlebars/issues/22
Or
I guess you could just go back to a version that doesn't have this issue, at least until it is fixed.
Upvotes: 9