42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const nodemailer = require('nodemailer');
|
|
const config = require('./config.json');
|
|
|
|
async function sendEmail(to, subject, text, html) {
|
|
const transporter = nodemailer.createTransport({
|
|
host: config.smtp.host,
|
|
port: config.smtp.port,
|
|
auth: config.smtp.auth,
|
|
secure: config.smtp.secure,
|
|
tls: config.smtp.tls
|
|
});
|
|
|
|
const mailOptions = {
|
|
from: config.smtp.auth.user,
|
|
to: to,
|
|
subject: subject,
|
|
text: text,
|
|
html: html
|
|
};
|
|
|
|
try {
|
|
const info = await transporter.sendMail(mailOptions);
|
|
console.log('邮件发送成功:', info.messageId);
|
|
return info;
|
|
} catch (error) {
|
|
console.error('邮件发送失败:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
if (require.main === module) {
|
|
const args = process.argv.slice(2);
|
|
const to = args[0] || 'cbed@163.com';
|
|
const subject = args[1] || '测试邮件';
|
|
const text = args[2] || '这是一封通过 Node.js 发送的测试邮件。';
|
|
const html = args[3] || '<p>这是一封通过 Node.js 发送的测试邮件。</p>';
|
|
|
|
sendEmail(to, subject, text, html).catch(console.error);
|
|
}
|
|
|
|
module.exports = sendEmail;
|