Reputation: 71
I'm using nodemailer for email submission and running from my localhost. I have email services created manually in the following dir /api/email/services/Email.js
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'example.com',
port: 587,
secure: false,
auth: {
user: 'user',
pass: 'password',
},
});
module.exports = {
send: (from, to, subject, html) => {
const options = {
from,
to,
subject,
html
};
return transporter.sendMail(options);
},
};
So then I can use it like strapi.services.email.send(from, email, subject, html);
Usually, I write my html template in the code line const html = '<p>Email testing</p>'
to be passed in the email services. But I don't want to do this for every email submission from different controllers.
So, I created a html template in /config/email-templates/custom-email.html
and tried to call it like const html = path.join(__dirname + '/../../../config/email-templates/custom-email.html');
.
When I run it, the email can be sent successfully but it cannot render the html. Instead of a rendered html, it's showing the full path of the custom-email.html
as the email message. Is this method possible to achieve in strapi?
Upvotes: 1
Views: 5194
Reputation: 710
Instead of passing the path to the file, you need to pass the actual content. In the first case const html = '<p>Email testing</p>'
, you are actually passing the content , but in the second case you are passing the file path.
Modified send method could look something like below:
send: (from, to, subject, htmlpath) => {
const readHTMLFile = (path, callback)=> {
fs.readFile(path, {encoding: "utf-8"}, function (err, html) {
if (err)
return callback(err);
else
return callback(null, html);
});
}
readHTMLFile(htmlpath, function(err, html) {
const options = {
from,
to,
subject,
html
};
return transporter.sendMail(options);
}); }
Upvotes: 2