带有 Mailgun 的 Nodemailer
Nodemailer with Mailgun
我正在尝试在用户注册到我的网站时发送电子邮件。我决定使用 Nodemailer 和 MailGun,但后来意识到并没有太多关于它们协同工作的教程或好的文档。对于 MailGun 等其他服务,似乎也是一个类似的问题。下面是我尝试让它工作的代码。我看了一个关于让它工作的教程,但似乎无法弄明白。我认为代码是正确的,但也许授权是错误的?或者我可以两者兼顾。我正在为我的开发环境使用 cloud9.io,所以这可能是问题所在?或者我的域尚未验证?
路线:
app.post('/signup',
setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
isUnauthenticated,
registrations.postSignup,
(req, res) =>{
const output = `
<h1>Your email is: ${req.body.email}</h1>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'postmaster@MYSUBDOMAIN',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
pass: 'MYPASSWORD' // generated ethereal password
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact" <your@email.com>', // sender address
to: '`${req.body.email}`', // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// render view here and pass in data/message
});
});
然后域名信息来自mailgun.com:
State
Unverified
IP Address
IPASSIGNED Manage IPs
SMTP Hostname
smtp.mailgun.org
Default SMTP Login
postmaster@MYSUBDOMAIN
API Base URL
https://api.mailgun.net/v3/MYSUBDOMAIN
Default Password
MYPASSWORD SMTP credentials
API Key
MYKEY
我想我应该更新这个答案以显示差异。我将端口从 587
更改为 2525
,因为我使用的是托管在 Google 的服务器上的云 9,它们阻止了端口 587
但允许 2525
对于 SMTP。其次,我将 to
电子邮件更改为我设置为 req.body.email
的变量,这是新用户的电子邮件。我的表格没有他们的名字,因为它适用于 a) 公司 b) 这对注册有更大的摩擦并且会略微降低转化率。如果您的表单有他们的名字,请在我再次使用他们的电子邮件的模板字符串中使用该名称。
app.post('/signup',
setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
isUnauthenticated,
registrations.postSignup,
(req, res) =>{
var newUserEmail = req.body.email;
const output = `
<h1>Your email is: ${req.body.email}</h1>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 2525,
secure: false, // true for 465, false for other ports
auth: {
user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
pass: 'MYPASSWORD' // generated ethereal password
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact" <your@email.com>', // sender address
to: newUserEmail, // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// render view here and pass in data/message
});
});
我正在尝试在用户注册到我的网站时发送电子邮件。我决定使用 Nodemailer 和 MailGun,但后来意识到并没有太多关于它们协同工作的教程或好的文档。对于 MailGun 等其他服务,似乎也是一个类似的问题。下面是我尝试让它工作的代码。我看了一个关于让它工作的教程,但似乎无法弄明白。我认为代码是正确的,但也许授权是错误的?或者我可以两者兼顾。我正在为我的开发环境使用 cloud9.io,所以这可能是问题所在?或者我的域尚未验证?
路线:
app.post('/signup',
setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
isUnauthenticated,
registrations.postSignup,
(req, res) =>{
const output = `
<h1>Your email is: ${req.body.email}</h1>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'postmaster@MYSUBDOMAIN',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
pass: 'MYPASSWORD' // generated ethereal password
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact" <your@email.com>', // sender address
to: '`${req.body.email}`', // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// render view here and pass in data/message
});
});
然后域名信息来自mailgun.com:
State
Unverified
IP Address
IPASSIGNED Manage IPs
SMTP Hostname
smtp.mailgun.org
Default SMTP Login
postmaster@MYSUBDOMAIN
API Base URL
https://api.mailgun.net/v3/MYSUBDOMAIN
Default Password
MYPASSWORD SMTP credentials
API Key
MYKEY
我想我应该更新这个答案以显示差异。我将端口从 587
更改为 2525
,因为我使用的是托管在 Google 的服务器上的云 9,它们阻止了端口 587
但允许 2525
对于 SMTP。其次,我将 to
电子邮件更改为我设置为 req.body.email
的变量,这是新用户的电子邮件。我的表格没有他们的名字,因为它适用于 a) 公司 b) 这对注册有更大的摩擦并且会略微降低转化率。如果您的表单有他们的名字,请在我再次使用他们的电子邮件的模板字符串中使用该名称。
app.post('/signup',
setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
isUnauthenticated,
registrations.postSignup,
(req, res) =>{
var newUserEmail = req.body.email;
const output = `
<h1>Your email is: ${req.body.email}</h1>
`;
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 2525,
secure: false, // true for 465, false for other ports
auth: {
user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
pass: 'MYPASSWORD' // generated ethereal password
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Nodemailer Contact" <your@email.com>', // sender address
to: newUserEmail, // list of receivers
subject: 'Node Contact Request', // Subject line
text: 'Hello world?', // plain text body
html: output // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// render view here and pass in data/message
});
});