我正在尝试使用Nodejs包Nodemailer发送电子邮件,但我无法将电子邮件更改为我的自定义域电子邮件。任何帮助将不胜感激。 这是我用来发送电子邮件的代码。
transporter.sendMail({
from: 'support@domain.com',
to: 'recipient@example.com',
subject: 'Message',
text: 'I hope this message gets through!',
auth: {
user: 'zeshanvirk@gmail.com'
}
});
答案 0 :(得分:2)
就像在nodemailer文档https://nodemailer.com/smtp/
中指出的那样您需要使用自定义域信息(主机,端口,用户和密码)配置传输器您可以在特定主机提供商的电子邮件配置中找到此信息。
var transporter = nodemailer.createTransport({
host: 'something.yourdomain.com',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'username@mydomain.com', // your domain email address
pass: 'password' // your password
}
});
然后你可以继续定义邮件选项:
var mailOptions = {
from: '"Bob" <bob@bobsdomain.com>',
to: 'tom@gmail.com',
subject: "Hello",
html : "Here goes the message body"
};
最后,您可以继续使用transporter和mailOptions通过sendEmail函数发送电子邮件
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
return ('Error while sending email' + err)
}
else {
console.log("Email sent");
return ('Email sent')
}
});
答案 1 :(得分:-1)
function sendEmail(to, subject, message, attachments) {
console.log(message);
var transporter = nodemailer.createTransport({
service:'gmail',
auth: {
user: 'email@gmail.com',
pass: 'YOUR_PASSWORD'
}
});
var mailOptions = {
from: 'support@gmail.com',
to: 'example@gmail.com'
subject: subject,
html : message,
attachments : attachments
};
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
return 'Error while sending email' + err
}
else {
console.log("Email sent");
return 'Email sent'
}
})
}