Reputation: 457
I try to create a personnalized template and send automated message with this templates.
I use nodemailer so i use nodeJs, and i have created an html template. The text appears on the mail but the image does not.
I followed a tutorial, and there is my .handlebars where i do my html (pictures are real, i pasted one in the same folder)
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Formulaire</title>
<link rel="stylesheet" href="../css/mail.css">
</head>
<body>
<div class='txt2'>
<div>
<img class='logo_synapture' src="logosynapture.png">
</div>
<div>
<img class='logo_hackathon' src="escape.png">
</div>
</div>
<div class='txt'>
<h1>Merci d'avoir rejoint notre Hackathon</h1>
</div>
<div class='cordonnees'>
<div>
</div>
</div>
</body>
</html>
and there is my .js
/*
Video: https://www.youtube.com/watch?v=38aE1lSAJZ8
Don't forget to disable less secure app from Gmail: https://myaccount.google.com/lesssecureapps TODO:
*/
require('dotenv').config();
const nodemailer = require('nodemailer');
const hbs = require('nodemailer-handlebars');
const log = console.log;
// Step 1
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mail', // TODO: your gmail account
pass: 'mdp' // TODO: your gmail password
}
});
// Step 2
transporter.use('compile', hbs({
viewEngine: 'express-handlebars',
viewPath: './views/'
}));
// Step 3
let mailOptions = {
from: '[email protected]', // TODO: email sender
to: 'mail.com', // TODO: email receiver
subject: 'Nodemailer - Test',
text: 'Wooohooo it works!!',
template: 'index',
context: {
name: 'Accime Esterling'
} // send extra values to template
};
// Step 4
transporter.sendMail(mailOptions, (err, data) => {
if (err) {
return log('Error occurs');
}
return log('Email sent!!!');
});
I don't understand what the problem could be, I searched on internet but there are not a lot of answers.
I don't think nodemailer can't read img, but did I forget something? And if it can't, how can I send images?
Upvotes: 2
Views: 509
Reputation: 86
The first thing you should notice here is: where did you put these images? The images src is pointing to a root folder. I think that the possibilities for your 404 in the images could be:
Even if you are working with mail protocols, basically the requests are http(s) and need to search for a valid address in your server (or a CDN) to fetch the content you are trying to show.
Upvotes: 1