我是testcafe
和节点Js的新手,我想使用节点邮件程序发送邮件。有人可以给我步骤吗?我试了几次,我不确定自己做错了什么。
以下是执行的步骤:
安装nodemailer:npm install nodemailer
使用SMTP或其他某种传输机制创建Nodemailer传输器
设置消息选项(谁发送给谁)
使用先前创建的传输器的sendMail()
方法传递消息对象
var mailer = require("nodemailer");
var transporter = mailer.createTransport({
host: "smtp.abc.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "email@abc.com", //
pass: "password" //
}
});
// send mail with defined transport object
var mail = {
from: "email@abc.com", // sender address
to: "email@abc.com", // list of receivers
subject: "hello", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>" // html body
};
transporter.sendMail(mail, function(error, response) {
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}
transporter.close();
});
我有2个问题: 对于SMTP,我需要从npm安装任何内容吗? net中的大多数示例都显示gmail,但我将其替换为公司的邮件,在此我已将其视为smtp.abc.com,是否还需要做其他事情?我想从公司电子邮件中发送邮件,这需要什么不同的设置?
答案 0 :(得分:1)
- 对于SMTP,我是否需要从npm安装任何内容?
不。仅nodemailer
- 我想从公司电子邮件中发送邮件,这需要什么不同的设置?
const nodemailer = require("nodemailer");
const mailHost = "smtp.mycompany.com";
const mailPort = 25;
const mailAuthUser = "username";
const mailAuthPass = "pwd";
const isMailSecure = false; // true for 465, false for other ports
module.exports = {
sendMail: function (mailSender, mailReceiver, mailSubject, mailBodyText, mailBodyHTML) {
try {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: mailHost,
port: mailPort,
secure: isMailSecure,
auth: {
user: mailAuthUser,
pass: mailAuthPass
}
});
// setup email data with unicode symbols
let mailOptions = {
from: mailSender,
to: mailReceiver,
subject: mailSubject,
text: mailBodyText,
html: mailBodyHTML
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
} catch (err) {
console.log(err);
}
}
};