Nodemailer SMTP 无法在生产服务器上运行
Nodemailer SMTP not working on production server
我在生产环境中通过 nodemailer - SMTP(来自另一台主机)发送电子邮件时遇到问题
我在服务器(Scaleway Dev)上上传了我的 API,我正在使用 Ubuntu Bionic 并且在测试时注意到它没有发送电子邮件(我需要它来验证用户) .
起初我以为请求没有到达服务器,但是当我尝试登录时我得到了 "confirm your password" 响应,我检查了 mongoDB 数据库并且用户在那里,但仍然没有确认邮件。
我尝试在 localhost 上检查它,认为它可能是 dotenv 依赖项,但它在那里工作,是什么原因?
我服务器上的节点版本是8.10.0
在我的个人电脑上 11.12.0
这些是我的依赖项
enter code here
"dependencies": {
"bcrypt": "^3.0.6",
"body-parse": "^0.1.0",
"client-sessions": "^0.8.0",
"connect": "^3.6.6",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.0.0",
"express": "~4.16.1",
"express-session": "^1.16.1",
"express-validator": "^5.3.1",
"http-errors": "~1.6.3",
"moment": "^2.24.0",
"mongoose": "^5.5.8",
"morgan": "~1.9.1",
"nodemailer": "^6.1.1",
"nodemon": "^1.19.0",
"passport": "^0.4.0",
"pug": "2.0.0-beta11",
"randomstring": "^1.1.5",
"session": "^0.1.0",
"session-mongoose": "^0.5.2"
}
使用的这些选项由我使用的主机指定
//this is my transporter constant
const transporter = nodemailer.createTransport({
host: '*different host from the server*',
port: 465,
secure: true,
auth: {
user: my used email(hardcoded),
pass: process.env.EMAILPASS
},
tls: {
rejectUnauthorized: false
}
});
//my email options
let mailOptions ={
from: '"Company Name <noreply@*different host DNS*>',
to: req.body.email,
subject: *subject*
html: `Email Content with confirmation token`
}
//emailing the message itself
transporter.sendMail(mailOptions, (err, info) => {
if(err){
return console.log(err);
}
console.log("message sent");
});
我正在使用此电子邮件生成器 (https://generator.email/) 快速发送一次性电子邮件。邮件在我保存用户后开始。
我对我应该做什么感到困惑...感谢所有帮助
我过去曾遇到过 nodemailer 的问题。我正在使用 mailgun (https://www.mailgun.com/) 发送电子邮件。他们每月免费提供 10,000 封电子邮件。这是与 mailgun 一起使用的代码:
依赖关系:
"express": "4.16.4",
"config": "3.0.1",
"nodemailer": "5.1.1",
"nodemailer-mailgun-transport": "1.4.0"
代码(它被包裹在一个快速路由器中):
const config = require('config');
const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
//the data has to be an array. It will send as many emails as the number of items in the array
var emailsList = ["test@test.com"];
emailsList[0].name = "Test Name";
var auth = {
auth: {
api_key: config.get('mailgunApiKey'),
domain: config.get('mailgunDomain')
}
};
transporter = nodemailer.createTransport(mg(auth)),
EmailTemplate = require('email-templates').EmailTemplate,
path = require('path'),
Promise = require('bluebird');
function sendEmail (obj) {
return transporter.sendMail(obj);
}
function loadTemplate (templateName, contexts) {
let template = new EmailTemplate(path.join(__dirname, '../templates', templateName));
return Promise.all(contexts.map((context) => {
return new Promise((resolve, reject) => {
template.render(context, (err, result) => {
if (err) reject(err);
else resolve({
email: result,
context,
});
});
});
}));
}
loadTemplate('dailyReferralEmail', emailsList).then((results) => {
return Promise.all(results.map((result) => {
sendEmail({
to: result.context.email,
from: 'Your Name <your-email@test.com>',
'h:Reply-To': 'your-email@test.com',
subject: result.email.subject,
html: result.email.html,
text: result.email.text,
});
}));
}).then(() => {
var response = {
text: "Email sent"
}
JSON.stringify(response);
res.status(200).send(response);
});
});
module.exports = router;
作为模板生成器,我使用的是 HBS。所以你的项目根目录下一定要有templates文件夹,有这棵树:
您可以通过 {{ }} 在电子邮件中使用变量。
里面 html.hbs:
<p>Hello {{name}}</p>
里面subject.hbs
Subject of your email
里面text.hbs
Preview text of your email
希望对您有所帮助!
我在生产环境中通过 nodemailer - SMTP(来自另一台主机)发送电子邮件时遇到问题
我在服务器(Scaleway Dev)上上传了我的 API,我正在使用 Ubuntu Bionic 并且在测试时注意到它没有发送电子邮件(我需要它来验证用户) .
起初我以为请求没有到达服务器,但是当我尝试登录时我得到了 "confirm your password" 响应,我检查了 mongoDB 数据库并且用户在那里,但仍然没有确认邮件。
我尝试在 localhost 上检查它,认为它可能是 dotenv 依赖项,但它在那里工作,是什么原因?
我服务器上的节点版本是8.10.0 在我的个人电脑上 11.12.0
这些是我的依赖项
enter code here
"dependencies": {
"bcrypt": "^3.0.6",
"body-parse": "^0.1.0",
"client-sessions": "^0.8.0",
"connect": "^3.6.6",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.0.0",
"express": "~4.16.1",
"express-session": "^1.16.1",
"express-validator": "^5.3.1",
"http-errors": "~1.6.3",
"moment": "^2.24.0",
"mongoose": "^5.5.8",
"morgan": "~1.9.1",
"nodemailer": "^6.1.1",
"nodemon": "^1.19.0",
"passport": "^0.4.0",
"pug": "2.0.0-beta11",
"randomstring": "^1.1.5",
"session": "^0.1.0",
"session-mongoose": "^0.5.2"
}
使用的这些选项由我使用的主机指定
//this is my transporter constant
const transporter = nodemailer.createTransport({
host: '*different host from the server*',
port: 465,
secure: true,
auth: {
user: my used email(hardcoded),
pass: process.env.EMAILPASS
},
tls: {
rejectUnauthorized: false
}
});
//my email options
let mailOptions ={
from: '"Company Name <noreply@*different host DNS*>',
to: req.body.email,
subject: *subject*
html: `Email Content with confirmation token`
}
//emailing the message itself
transporter.sendMail(mailOptions, (err, info) => {
if(err){
return console.log(err);
}
console.log("message sent");
});
我正在使用此电子邮件生成器 (https://generator.email/) 快速发送一次性电子邮件。邮件在我保存用户后开始。
我对我应该做什么感到困惑...感谢所有帮助
我过去曾遇到过 nodemailer 的问题。我正在使用 mailgun (https://www.mailgun.com/) 发送电子邮件。他们每月免费提供 10,000 封电子邮件。这是与 mailgun 一起使用的代码:
依赖关系:
"express": "4.16.4",
"config": "3.0.1",
"nodemailer": "5.1.1",
"nodemailer-mailgun-transport": "1.4.0"
代码(它被包裹在一个快速路由器中):
const config = require('config');
const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
//the data has to be an array. It will send as many emails as the number of items in the array
var emailsList = ["test@test.com"];
emailsList[0].name = "Test Name";
var auth = {
auth: {
api_key: config.get('mailgunApiKey'),
domain: config.get('mailgunDomain')
}
};
transporter = nodemailer.createTransport(mg(auth)),
EmailTemplate = require('email-templates').EmailTemplate,
path = require('path'),
Promise = require('bluebird');
function sendEmail (obj) {
return transporter.sendMail(obj);
}
function loadTemplate (templateName, contexts) {
let template = new EmailTemplate(path.join(__dirname, '../templates', templateName));
return Promise.all(contexts.map((context) => {
return new Promise((resolve, reject) => {
template.render(context, (err, result) => {
if (err) reject(err);
else resolve({
email: result,
context,
});
});
});
}));
}
loadTemplate('dailyReferralEmail', emailsList).then((results) => {
return Promise.all(results.map((result) => {
sendEmail({
to: result.context.email,
from: 'Your Name <your-email@test.com>',
'h:Reply-To': 'your-email@test.com',
subject: result.email.subject,
html: result.email.html,
text: result.email.text,
});
}));
}).then(() => {
var response = {
text: "Email sent"
}
JSON.stringify(response);
res.status(200).send(response);
});
});
module.exports = router;
作为模板生成器,我使用的是 HBS。所以你的项目根目录下一定要有templates文件夹,有这棵树:
您可以通过 {{ }} 在电子邮件中使用变量。
里面 html.hbs:
<p>Hello {{name}}</p>
里面subject.hbs
Subject of your email
里面text.hbs
Preview text of your email
希望对您有所帮助!