带车把的 nodemailer 无法正确显示样式
nodemailer with handlebars not showing styles correctly
我正在发送一封带有 nodemailer 和 handlebars 的电子邮件,我收到了正确的电子邮件,但未应用样式。
这是我发送电子邮件的代码:
const hbsConfig = {
viewEngine: {
extName: '.hbs',
partialsDir: path.join(__dirname, '../../../../src/modules/hq-nd/templates/'),
layoutsDir: path.join(__dirname, '../../../../src/modules/hq-nd/templates/'),
defaultLayout: ''
},
viewPath: path.join(__dirname, '../../../../src/modules/hq-nd/templates/'),
extName: '.hbs'
};
const transporter = nodemailer.createTransport({
host: 'smtp.sendgrid.net',
port: 465,
secure: true,
auth: {
user: 'zzz',
pass: process.env.SENDGRID_SECRET,
},
});
transporter.use('compile', hbs(hbsConfig));
const email = {
from: "Boxtribe <123@gmail.com>",
to: params.username,
subject: 'Reset your zzz Password',
template: 'forgot-password',
context: {
...user,
token: tokenRes.token,
url: process.env.HARMONY_URL
}
};
const response = await transporter.sendMail(email as Mail.Options);
这是我的模板:
<html>
<head>
<meta
content="text/html; charset=utf-8"
http-equiv="Content-Type"
/>
<link
href="https://fonts.googleapis.com/css?family=Lato"
rel="stylesheet"
type="text/css"
/>
</head>
<body>
<style>
body {
font-family: Lato, Tahoma, Verdana, Segoe, sans-serif;
}
.header {
margin: 0 auto;
min-width: 320px;
max-width: 620px;
}
.logo {
display: block;
margin: auto;
}
.center {
text-align: center;
}
.title {
margin: 0;
}
.invoice {
font-size: 14;
color: #71777D;
line-height: 1.2;
word-break: break-word;
}
.btn-success
{
color:#fff;
background: #22caab;
border-radius: 5px;
transition:.15s linear;
border-color: #22caab;
}
.powered {
font-size: 12;
line-height: 1.2;
word-break: break-word;
padding-top: 30px;
padding-right: 10px;
padding-bottom: 10px;
padding-left: 10px;
}
</style>
<div class="header">
<img
class="logo"
src="{{url}}img/teal_bto_head.png"
width="150"
alt="logo"
>
<h3 class="center title">
Reset Password
</h3>
<p class="invoice center">
Harmony recently received a request for a forgotten password.
</p>
<p class="invoice center">
To change your password, please press the next button:
</p>
<p class="invoice center">
<a href="{{url}}reset-password.php?t={{token}}" class="btn btn-success" >Continue Setup</a>
</p>
<p class="powered center">
Powered by Qorus Inc.
</p>
</div>
</body>
</html>
我已经在 http://tryhandlebarsjs.com/ 中进行了测试,它显示正确:
但是邮件里没有申请css类:
发现了问题,这是因为我使用的是 类 而不是内联样式。当我将样式更改为 inline
时它已修复
我正在发送一封带有 nodemailer 和 handlebars 的电子邮件,我收到了正确的电子邮件,但未应用样式。
这是我发送电子邮件的代码:
const hbsConfig = {
viewEngine: {
extName: '.hbs',
partialsDir: path.join(__dirname, '../../../../src/modules/hq-nd/templates/'),
layoutsDir: path.join(__dirname, '../../../../src/modules/hq-nd/templates/'),
defaultLayout: ''
},
viewPath: path.join(__dirname, '../../../../src/modules/hq-nd/templates/'),
extName: '.hbs'
};
const transporter = nodemailer.createTransport({
host: 'smtp.sendgrid.net',
port: 465,
secure: true,
auth: {
user: 'zzz',
pass: process.env.SENDGRID_SECRET,
},
});
transporter.use('compile', hbs(hbsConfig));
const email = {
from: "Boxtribe <123@gmail.com>",
to: params.username,
subject: 'Reset your zzz Password',
template: 'forgot-password',
context: {
...user,
token: tokenRes.token,
url: process.env.HARMONY_URL
}
};
const response = await transporter.sendMail(email as Mail.Options);
这是我的模板:
<html>
<head>
<meta
content="text/html; charset=utf-8"
http-equiv="Content-Type"
/>
<link
href="https://fonts.googleapis.com/css?family=Lato"
rel="stylesheet"
type="text/css"
/>
</head>
<body>
<style>
body {
font-family: Lato, Tahoma, Verdana, Segoe, sans-serif;
}
.header {
margin: 0 auto;
min-width: 320px;
max-width: 620px;
}
.logo {
display: block;
margin: auto;
}
.center {
text-align: center;
}
.title {
margin: 0;
}
.invoice {
font-size: 14;
color: #71777D;
line-height: 1.2;
word-break: break-word;
}
.btn-success
{
color:#fff;
background: #22caab;
border-radius: 5px;
transition:.15s linear;
border-color: #22caab;
}
.powered {
font-size: 12;
line-height: 1.2;
word-break: break-word;
padding-top: 30px;
padding-right: 10px;
padding-bottom: 10px;
padding-left: 10px;
}
</style>
<div class="header">
<img
class="logo"
src="{{url}}img/teal_bto_head.png"
width="150"
alt="logo"
>
<h3 class="center title">
Reset Password
</h3>
<p class="invoice center">
Harmony recently received a request for a forgotten password.
</p>
<p class="invoice center">
To change your password, please press the next button:
</p>
<p class="invoice center">
<a href="{{url}}reset-password.php?t={{token}}" class="btn btn-success" >Continue Setup</a>
</p>
<p class="powered center">
Powered by Qorus Inc.
</p>
</div>
</body>
</html>
我已经在 http://tryhandlebarsjs.com/ 中进行了测试,它显示正确:
但是邮件里没有申请css类:
发现了问题,这是因为我使用的是 类 而不是内联样式。当我将样式更改为 inline
时它已修复