如何使用 nodemailer 发送 html 文件
How do I send a html file using nodemailer
我正在使用 nodemailer 发送电子邮件,但我想知道如何从目录发送静态 HTML 文件。
let transporter = nodemailer.createTransport({
host: auth.host,
port: auth.port,
secure: auth.secure,
auth: {
type: auth.type,
user: auth.user,
clientId: auth.clientId,
clientSecret: auth.clientSecret,
refreshToken: auth.refreshToken,
accessToken: auth.accessToken,
expires: auth.expires
}
});
let mailOptions = {
from: '"xxxxx',
to: 'xxxx',
subject: "xxxx",
text: `xxxx`,
html: email.html
};
您将必须使用 fs
模块阅读文件。
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
async function sendMail() {
let transporter = nodemailer.createTransport({
host: auth.host,
port: auth.port,
secure: auth.secure,
auth: {
type: auth.type,
user: auth.user,
clientId: auth.clientId,
clientSecret: auth.clientSecret,
refreshToken: auth.refreshToken,
accessToken: auth.accessToken,
expires: auth.expires
}
});
let mailOptions = {
from: '"xxxxx',
to: 'xxxx',
subject: "xxxx",
text: `xxxx`,
html: await readFile('/path/to/file', 'utf8')
};
// send mail
}
如果文件不会改变,您可以缓存内容。
您可以使用 fs
读取模板文件,并使用 handlebars
替换模板中的值。
举个例子:
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const handlebars = require('handlebars');
const { promisify } = require('util');
const fs = require('fs');
const readFile = promisify(fs.readFile);
smtpTransport = nodemailer.createTransport(smtpTransport({
host: mailConfig.host,
secure: mailConfig.secure,
port: mailConfig.port,
auth: {
user: mailConfig.auth.user,
pass: mailConfig.auth.pass
}
}));
const sendMail = async () => {
let html = await readFile('/path/to/file', 'utf8');
let template = handlebars.compile(html);
let data = {
username: "Toto"
};
let htmlToSend = template(data);
let mailOptions = {
from: 'from@toto.com',
to : 'to@toto.com',
subject : 'test',
html : htmlToSend
};
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) console.log(error);
});
});
我正在使用 nodemailer 发送电子邮件,但我想知道如何从目录发送静态 HTML 文件。
let transporter = nodemailer.createTransport({
host: auth.host,
port: auth.port,
secure: auth.secure,
auth: {
type: auth.type,
user: auth.user,
clientId: auth.clientId,
clientSecret: auth.clientSecret,
refreshToken: auth.refreshToken,
accessToken: auth.accessToken,
expires: auth.expires
}
});
let mailOptions = {
from: '"xxxxx',
to: 'xxxx',
subject: "xxxx",
text: `xxxx`,
html: email.html
};
您将必须使用 fs
模块阅读文件。
const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
async function sendMail() {
let transporter = nodemailer.createTransport({
host: auth.host,
port: auth.port,
secure: auth.secure,
auth: {
type: auth.type,
user: auth.user,
clientId: auth.clientId,
clientSecret: auth.clientSecret,
refreshToken: auth.refreshToken,
accessToken: auth.accessToken,
expires: auth.expires
}
});
let mailOptions = {
from: '"xxxxx',
to: 'xxxx',
subject: "xxxx",
text: `xxxx`,
html: await readFile('/path/to/file', 'utf8')
};
// send mail
}
如果文件不会改变,您可以缓存内容。
您可以使用 fs
读取模板文件,并使用 handlebars
替换模板中的值。
举个例子:
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const handlebars = require('handlebars');
const { promisify } = require('util');
const fs = require('fs');
const readFile = promisify(fs.readFile);
smtpTransport = nodemailer.createTransport(smtpTransport({
host: mailConfig.host,
secure: mailConfig.secure,
port: mailConfig.port,
auth: {
user: mailConfig.auth.user,
pass: mailConfig.auth.pass
}
}));
const sendMail = async () => {
let html = await readFile('/path/to/file', 'utf8');
let template = handlebars.compile(html);
let data = {
username: "Toto"
};
let htmlToSend = template(data);
let mailOptions = {
from: 'from@toto.com',
to : 'to@toto.com',
subject : 'test',
html : htmlToSend
};
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) console.log(error);
});
});