提问人:hemant 提问时间:11/9/2023 更新时间:11/9/2023 访问量:8
Nodemailer 配置在开发模式下工作,在 VPS 上不起作用
Nodemailer config works on dev mode and doesn't work on VPS
问:
下面的 Nodemailer 代码在笔记本电脑上的开发模式下工作,但在 Ubuntu VPS 上部署时不起作用。我收到transporter.verify错误。
使用开发模式时,相同的代码也适用于另一个 VPS 以及我的笔记本电脑。 不知道为什么它不能在我的新VPS上运行。
节点方法
async function createPrimaryAccount(reqBody, origin, user) {
// some code - which works - checked using console.log
console.log('sending email next');
// send email
await sendVerificationEmail(account.email, account.verificationToken, account.clientName, origin);
}
async function sendVerificationEmail(email, verificationToken, clientName, origin) {
console.log('acct service sendVerificationEmail email, clientName, origin', email, clientName, origin);
let message;
const verifyUrl = `${origin}/auth/verify-email?token=${verificationToken}`;
message =
`<h3 style="background-color: rgb(63, 81, 181); color: white; padding: 5px 10px">BFSI EDGE's CS Web App: Verification Email</h3>
<p>This email was sent to you because your email address was used for registering a user on BFSI EDGE's CS Web Application for the client: ${clientName}.</p>
<p>In case you have not done so, do not click on the below link.</p>
<p>If this is a valid registration, please click the below link to verify your email address:</p>
<p><a href="${verifyUrl}">CLICK HERE TO VERIFY</a></p>
<p></p>
<p>Note that this email cannot receive replies. For any clarification, contact your company's admin.
</p>
<p style="background-color: rgb(63, 81, 181); color: white; padding: 5px 10px">BFSI EDGE Address</p>`;
try {
await sendEmail({
to: email,
subject: 'BFSI-EDGE CS Web-App: Registration Verification required',
html: message
});
console.log('Verification email sent.')
// Getting this log but its not correct as there is an error is sendEmail method
} catch(err) {
console.log('Error while sending verification email: ' + err)
}
}
async function sendEmail({ to, subject, html, from = config.mailServerOptions.auth.user}) {
console.log('helper sendEmail - from', from)
const transporter = nodemailer.createTransport(config.mailServerOptions);
let sendEmail = true;
transporter.verify(function(error, success) {
if (error) {
sendEmail = false;
console.log('helper sendEmail - transporter.verify error:', error);
/// GETTING THE ABOVE ERROR
} else {
console.log('helper sendEmail - transporter.verify - OKAY')
if(sendEmail) {
console.log('helper sendEmail - sending email NOW')
try {
transporter.sendMail({ from, to, subject, html });
} catch (err) {
console.log('helper sendEmail - transporter.sendMail error:', err);
}
}
}
});
}
config.mailServerOptions - 我已经尝试了以下两个选项,nodemailer 所需的邮件服务器选项。
"mailServerOptions": {
"pool": true,
"host": "smtp.example.com",
"auth": {
"user": "[email protected]",
"pass": "password"
},
"tls": {
"rejectUnauthorized": false
}
}
"mailServerOptions": {
"service": "gmail",
"auth": {
"user": "[email protected]",
"pass": "password" // USED Google App password here
},
"tls": {
"rejectUnauthorized": false
}
}
console.log :以下是上述代码中的控制台注销语句。
sending email next
acct service sendVerificationEmail email, clientName, origin [email protected] BFSI EDGE https://demo-cs.bfsiapp.com
helper sendEmail - from [email protected]
Verification email sent.
helper sendEmail - transporter.verify error: Error: Connection timeout
at SMTPConnection._formatError (/root/demo-cs/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at SMTPConnection._onError (/root/demo-cs/node_modules/nodemailer/lib/smtp-connection/index.js:770:20)
at Timeout.<anonymous> (/root/demo-cs/node_modules/nodemailer/lib/smtp-connection/index.js:229:22)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
code: 'ETIMEDOUT',
command: 'CONN'
}
答: 暂无答案
评论