我使用nodemailer在node.js中设置了mailgun,当我使用REST客户端测试路由时,我无法获取它来发送电子邮件。当我直接将代码写入服务器文件时,服务器启动后立即发送电子邮件。
但是,当我在app.post中编写电子邮件api并使用REST客户端对其进行测试时,电子邮件在mailgun端失败并被Google拒绝???这只是我的假设,因为我得到了错误代码。
Server response: 550 5.7.1 Unauthenticated email from guest.com is not
accepted due to domain's 5.7.1 DMARC policy. Please contact the administrator
of guest.com domain if 5.7.1 this was a legitimate mail. Please visit 5.7.1
https://support.google.com/mail/answer/2451690 to learn about the 5.7.1 DMARC
initiative. s12-v6si3013316qtg.362 - gsmtp
我已经点击了提供的链接,并且文档说要与域服务器一起检查。我不明白为什么会收到此错误,因为我已经在mailgun中验证了我的gmail。
我还尝试过直接使用mailgun-js中间件进行切换。从mailgun.com,github上的mailgun-js和npmjs.com上的mailgun-js找到了三种不同的mailgun-js api。出现与上述相同的错误。
茫然。不知道该怎么办。
我的nodemailer代码:
const express = require('express');
const nodemailer = require('nodemailer');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'dist')));
app.post('/email', (req, res) => {
let transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'postmaster@sandboxc8f2ecf64f364ca6ad740e658485c557.mailgun.org',
pass: 'my API key here'
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: req.body.from, // sender address
to: 'cu0ngpitt@gmail.com', // list of receivers
subject: 'Someone wrote you from your Online Resume!', // Subject line
text: req.body.messge, // plain text 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));
});
});
app.get('/', (req, res) => {
res.send('Hello world');
});
app.listen(port, () => {
console.log('Server running on port ' + port);
})
答案 0 :(得分:0)
Mailgun回复了我的票,并说由于DMARC的安全性,我只能发送与我的mailgun帐户具有相同域的电子邮件。
因为我有一个免费的mailgun帐户。我必须将发件人地址更改为我的免费mailgun地址,该地址为postmaster @ sandbox“一些长字符串”。mailgun.org
如果使用nodemailer,则可以省略发件人电子邮件或对其进行硬编码。
如果使用mailgun-js,则必须对其进行硬编码。
我的旧代码:
// setup email data with unicode symbols
let mailOptions = {
from: req.body.from, // sender address
to: 'cu0ngpitt@gmail.com', // list of receivers
subject: 'Someone wrote you from your Online Resume!', // Subject line
text: req.body.messge, // plain text body
};
更正的代码:
// setup email data with unicode symbols
let mailOptions = {
postmaster@sandbox"some long string".mailgun.org, // sender address
to: 'cu0ngpitt@gmail.com', // list of receivers
subject: 'Someone wrote you from your Online Resume!', // Subject line
text: req.body.messge, // plain text body
};