要使用Express.js以HTML格式发送电子邮件,你需要以下几个基础概念和技术:
以下是一个使用Express.js和Nodemailer发送HTML邮件的示例:
const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
// 创建Nodemailer传输对象
const transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'your_email@example.com',
pass: 'your_email_password'
}
});
app.get('/send-email', (req, res) => {
const mailOptions = {
from: 'your_email@example.com',
to: 'recipient@example.com',
subject: 'Test HTML Email',
html: `
<h1>Hello!</h1>
<p>This is a test email with <strong>HTML</strong> content.</p>
<a href="https://example.com">Click here</a>
`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.send('Error sending email');
} else {
console.log('Email sent: ' + info.response);
res.send('Email sent successfully');
}
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
通过以上步骤和示例代码,你应该能够成功使用Express.js以HTML格式发送电子邮件。如果遇到具体问题,请提供详细的错误信息以便进一步诊断。
领取专属 10元无门槛券
手把手带您无忧上云