如何在NodeJS中以html页面作为电子邮件发送自动回复电子邮件



我最近开始在NodeJS中编程。我想不出如何发送自动回复电子邮件。当另一个人给我发电子邮件时,会向他/她的电子邮件发送一封HTML电子邮件格式的自动回复电子邮件。

我该怎么做?

首先,您需要使用一个通用的smtp or imap client来轮询或监视您的帐户以获取传入邮件,例如https://www.npmjs.com/package/imap或者更好的是,一个实际监控新电子邮件的模块,例如https://www.npmjs.com/package/mail-notifier

张贴的例子是

Start listening new mails :
const notifier = require('mail-notifier');

const imap = {
user: "yourimapuser",
password: "yourimappassword",
host: "imap.host.com",
port: 993, // imap port
tls: true,// use secure connection
tlsOptions: { rejectUnauthorized: false }
};

notifier(imap)
.on('mail', mail => console.log(mail))
.start();
Keep it running forever :
const n = notifier(imap);
n.on('end', () => n.start()) // session closed
.on('mail', mail => console.log(mail.from[0].address, mail.subject))
.start();

接下来,很难击败nodemailer来发送您的响应。

https://nodemailer.com/about/

这是他们的例子。(这个代码有一半是评论和日志(

"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo@example.com>', // sender address
to: "bar@example.com, baz@example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);

最新更新