【问题标题】:NodeMailer Response Nothing on ServerNodeMailer 响应服务器上什么都没有
【发布时间】:2019-01-12 15:52:33
【问题描述】:

我使用 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;
    }
  }

【问题讨论】:

    标签: javascript node.js gmail nodemailer


    【解决方案1】:

    如果您仍然需要帮助,但我正在回答:

    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';
    

    所以,基本上,如果您想在服务器端响应,请发送一个回调,返回来自发件人的响应。

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 2020-01-25
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 2011-08-22
      • 2017-01-09
      • 2017-01-04
      相关资源
      最近更新 更多