使用Nodemailer发送邮件时链接不可点击的问题求助
解决Nodemailer+Brevo发送邮件链接无法点击的问题
你的问题出在HTML字符串的手动转义处理上,尤其是引号的转义导致邮件客户端无法正确识别链接属性。直接按下面的方法修改代码即可解决:
修复方案1:用模板字符串写HTML(推荐)
别再手动转义<>和引号了,用ES6的模板字符串(反引号)直接写HTML内容,既能保证结构正确,又不容易出错:
var link = "http://google.com"; const mailOptions = { from: process.env.RESET_PASSWORD_EMAIL, to: email, subject: 'Password Reset', html: ` <p>You are receiving this email because you have requested the reset of the password for your account.</p><br /> <p>Please click on the following link, or paste this into your browser to complete the process:</p> <a href="${link}">Reset password</a><br /> <p>If you did not request this, please ignore this email and your password will remain unchanged.</p> ` };
修复方案2:补充完整HTML结构
有些邮件服务商(比如Brevo)需要完整的HTML文档才能正确解析链接,加上<html>和<body>标签更稳妥:
var link = "http://google.com"; const mailOptions = { from: process.env.RESET_PASSWORD_EMAIL, to: email, subject: 'Password Reset', html: ` <!DOCTYPE html> <html> <body> <p>You are receiving this email because you have requested the reset of the password for your account.</p><br /> <p>Please click on the following link, or paste this into your browser to complete the process:</p> <a href="${link}" target="_blank">Reset password</a><br /> <p>If you did not request this, please ignore this email and your password will remain unchanged.</p> </body> </html> ` };
问题根源
你原代码里用\"转义引号,导致最终生成的HTML里href属性变成了href="http://google.com",而不是标准的href="http://google.com"。邮件客户端识别不了这种转义后的引号,自然就把链接当成普通文本,无法点击。
内容的提问来源于stack exchange,提问作者GenesysRTA
相关产品推荐
相关产品推荐

