Reputation: 21
I am sending an email using nodemailer
and have an email template in EJS
. When I render this template on my machine, I can see the logo/image
too but when I send this template to an email address, images are not showing in the email.
I am hosting images using static middleware
and here is the middleware
:
app.use('/static', express.static(path.join(__dirname, 'public')))
Is there any way to set the logo/image
in the email?
Upvotes: 1
Views: 1822
Reputation: 13669
you have to give full path of image to ejs template when sending to email
// common function used for send different emails
async sendMail(to:string, template:string, subject:string) {
//create transport to send mail
var transporter = nodemailer.createTransport({
service: config.get('SMTP_CONSTANTS.SERVICE'),
auth: {
user: config.get('SMTP_CONSTANTS.USERNAME'),
pass: config.get('SMTP_CONSTANTS.PASSWORD')
}
});
//define mail options
var mailOptions = {
from: 'Your Domain <[email protected]>',
to: to, // receiver
subject: subject, // Subject line
html: template
};
// send mail with defined transport object
let response = await transporter.sendMail(mailOptions);
return response;
}
app.get('/test',(req,res)=>{
let image_path = path.join(__dirname+'public/','logo.png');
console.log('image_path==',image_path);
let subject = 'Welcome To Website';
let to = 'to send mail';
let parameters = {
name: 'James',
image_path:image_path
};
let html = ejs.render(fs.readFileSync(__dirname+`/../views/auth/welcome.ejs`).toString(), parameters);
let response:any = await this.sendMail(to, html, subject).then((data:any)=>{return data}).catch(error => { return error });
return response;
});
Upvotes: 3