0

i'm using Oauth2 and nodemailer to send emails using nodejs. when i send the email to a gmail email ex: example@gmail.com everything is working fine, but when i try to send the email to a hotmail email ex: example@hotmail.com, it's returning for me a success message, but when i check my email, i see that the email was not sent.

this is my code:

const nodemailer = require('nodemailer');
const { google } = require('googleapis');

const CLIENT_ID = 'My client id';
const CLEINT_SECRET = 'the secret';
const REDIRECT_URI = 'https://developers.google.com/oauthplayground';
const REFRESH_TOKEN = 'the refresh token';


const oAuth2Client = new google.auth.OAuth2(
  CLIENT_ID,
  CLEINT_SECRET,
  REDIRECT_URI
);
oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });




exports.sendEmail = async (email, subject, text, html) => {


  try {
    const accessToken = await oAuth2Client.getAccessToken();
    const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        type: 'OAuth2',
        user: process.env.EMAIL,
        clientId: CLIENT_ID,
        clientSecret: CLEINT_SECRET,
        refreshToken: REFRESH_TOKEN,
        accessToken: accessToken,
      }
    });

    const mailOptions = {
      from: 'Twitter from AliExpress <process.env.EMAIL>',
      to: email,
      subject: subject,
      text: text,
      html: html
    };

    const result = await transporter.sendMail(mailOptions);
    return result
  }

  catch (error) {
    let err = new Error('Email err: '+ error.message)
    return err

  }
}

Thank you.

petra
  • 29
  • 6

0 Answers0