Nodemail使用服务帐户发送firebase应用程序的邮件

时间:2017-06-20 20:37:02

标签: firebase gmail-api nodemailer

尝试使用Nodemailer发送邮件时收到以下消息:

    '535-5.7.8 Username and Password not accepted. 
    Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials y42sm13399804wrc.51 - gsmtp',
  responseCode: 535,
  command: 'AUTH XOAUTH2' }

我正在使用firebase项目中的服务帐户,并已授予对GMail API的访问权限。但nodemailer docs for 2LO is really scarce所以我想知道是否有人可以帮助我找到我是否使用正确的凭证?

user (functions.config().gmail.user) : myfirebaseproject@appspot.gserviceaccount.com

-

function sendContactMail(contactName, contactEmail, contactDate, contactText) {


  // Create transport
  let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: 'OAuth2',
        user: functions.config().gmail.user,
        serviceClient: functions.config().gmail.client_id,
        privateKey: functions.config().gmail.private_key
        //accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x',
        //expires: 1484314697598
    }
  });

  // Mail Options
  let mailOptions = {
    from: `"${APP_NAME}" <${SENDER}>`,
    replyTo: `${contactEmail}`,
    to: 'recipient@gmail.com',
    subject: `Nouveau contact photo de ${contactName}`,
    html: `Nom : ${contactName}<br/>
           Email : ${contactEmail}<br/>
           Date du mariage : ${contactDate}<br/>
           Message : ${contactText}`,
    disableFileAccess: true,
    disableUrlAccess:true
  };

  // Send Mail
  return transporter.sendMail(mailOptions);
}

2 个答案:

答案 0 :(得分:0)

我使用此代码的云功能,它工作正常。试试这个

mailTransport = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: ${email},
    pass: ${password}
  }
});

答案 1 :(得分:0)

您的帐户是否启用了两步验证?在这种情况下,在https://myaccount.google.com/apppasswords上生成应用密码并使用它而不是您的普通密码:

const mailTransport = nodemailer.createTransport(
    `smtps://user@gmail.com:generatedAppPassword@smtp.gmail.com`);