我使用 NodeMailer 和 Gmail API 从我的服务器发送电子邮件。当我在本地主机上测试它时,一切都很好。但是当我在我的服务器上运行它时,Nodemailer 没有任何响应,也没有发送任何内容,我不知道为什么。 感谢您阅读我的问题。请解答,谢谢。 :'( 这是我的代码:
const { google } = require('googleapis');
const OAuth2 = google.auth.OAuth2;
const oauth2Client = new OAuth2(
AuthConfig.clientId,
AuthConfig.clientSecret,
'https://developers.google.com/oauthplayground'
);
oauth2Client.setCredentials({
refresh_token: AuthConfig.refreshToken
});
export default class GmailConfig {
static async createConfig () {
const tokens = await oauth2Client.getRequestHeaders();
const accessToken = tokens.Authorization;
return {
service: 'gmail',
auth: {
type: 'OAuth2',
user: 'address@gmail.com',
clientId: AuthConfig.clientId,
clientSecret: AuthConfig.clientSecret,
refreshToken: AuthConfig.refreshToken,
accessToken
}
}
}
}
async sendMail (receiver, subject, content) {
try {
const option = {
from: 'address',
to: receiver,
subject: subject,
markdown: content
}
const mailConfig = await GmailConfig.createConfig();
const transporter = emailer.createTransport(mailConfig);
transporter.use('compile', markdown())
await transporter.sendMail(option);
transporter.close();
return 'success';
}
catch (error) {
console.log(error);
return error;
}
}
最佳答案
不知道您是否仍然需要帮助,但我正在回答:
const mailConfig = await GmailConfig.createConfig();
const transporter = emailer.createTransport(mailConfig);
transporter.use('compile', markdown())
await transporter.sendMail(option, function (error, info)); // callback here
console.log(info); // log the body and status etc...
transporter.close();
return 'success';
所以,基本上,如果您想要服务器端的响应,请发送一个回调,返回发送者的响应。
关于javascript - NodeMailer 服务器上没有任何响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54161226/