angular - 如何在 Web 主机服务器中运行节点邮件程序
问题描述
我创建了一个页面,用于使用节点邮件程序将表单数据发送到 gmail 帐户。
要将页面表单数据发送到 gmail,我必须首先运行node server.js
我配置节点邮件程序的服务器。它在本地运行良好,但在生产现场却没有。
运行 server.js 后很清楚,只有这样才有效。
但是我如何node server.js
从网络托管服务器启动它以使其在生产站点中工作。
是否有任何不同的生产配置,例如在本地启动服务器,如下所述,
服务器.js
const nodemailer = require("nodemailer");
const xoauth2 = require("xoauth2");
const cors = require('cors');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// app.use(function (req, res, next) {
// res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Accept, Authorization');
// res.setHeader('Access-Control-Allow-Credentials', true);
// next();
// });
app.use(cors({ origin: "*" }));
app.use(bodyParser.json());
// app.use(bodyParser.json({limit: "50mb"}));
// app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
app.post('/sendFormData', (req, res) => {
console.log(req.body, 'data of form');
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
secure: 'true',
port: '465',
auth: {
user: 'test@gmail.com', // must be Gmail
pass: 'pass'
}
});
var mailOptions = {
from: `${req.body.email}`,
to: 'test@gmail.com', // must be Gmail
cc: `${req.body.name}`,
subject: 'New Order',
html: `
<table style="width: 100%; border: 1px solid #6c0078">
<thead>
<tr style="background-color: #6c0078; color: #fff;">
<th style="padding: 10px 0">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center; border-right: 1px solid #6c0078;">${req.body.name}</td>
</tr>
</tbody>
</table>
`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
res.status(200).json({
message: 'successfuly sent!'
})
}
});
});
app.listen(3000, () => {
console.log("server run!!!");
});
生产现场出错
解决方案
确保您的端点看起来像http://serverIp:3000/sendFormData
. 您还可以添加错误响应。
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.status(500).json({
error
});
} else {
console.log('Email sent: ' + info.response);
res.status(200).json({
message: 'successfuly sent!'
})
}
});
推荐阅读
- json - 获取 JSON 数组中具有特定 ID 的第一个 json 元素 - Microsoft Flow
- macos - Flutter android studio in mac 显示 Please configure Android SDK in Logcat
- service-worker - Workbox Stale-while-revalidate 策略总是从网络调用而不是缓存返回响应
- javascript - 从网站打开 GoogleMaps 应用程序以在 android 上导航
- ember.js - Ember 车把 如何对车把文件进行 Ajax 或 jquery 调用?
- c# - 没有属性名称的 Http get 和 post 方法
- javascript - 使用 Javascript 的下拉列表
- php - 我想在我的克隆元素显示后重新加载/刷新页面
- python - 我将 rasterio 捆绑到可执行文件中的方式有问题
- git - 在 git 中使用 mergeequests 和 releasenotes 时经常发生冲突