0

I write email class:

const pug = require('pug')
const nodemailer = require('nodemailer');
const htmlToText = require('html-to-text');

module.exports = class Email{
   constructor(user, url) {
      this.to = user.email;
      this.firstName = user.name.split(' ')[0];
      this.url = url;
      this.from = `Feruz Atamuradow <${process.env.EMAIL_FROM}>`;
   }

   newTransport() {
      if (process.env.NODE_ENV === 'production') {
         return nodemailer.createTransport({
         service: 'SendGrid',
         auth: {
            user: process.env.SENDGRID_USERNAME,
            pass: process.env.SENDGRID_PASSWORD
         }
         });
      }

      return nodemailer.createTransport({
         host: process.env.EMAIL_HOST,
         port: process.env.EMAIL_PORT,
         auth: {
         user: process.env.EMAIL_USERNAME,
         pass: process.env.EMAIL_PASSWORD
         },
         tls:{
            rejectUnauthorized:false
         } // and  try this without 

      });
  }

   async send(template, subject) {
      const html = pug.renderFile(`${__dirname}/../views/email/${template}.pug`, {
         firstName: this.firstName,
         url: this.url,
         subject
      });

      const mailOptions = {
         from: this.from,
         to: this.to,
         subject,
         html,
         text: htmlToText.fromString(html)
      }
      
      await this.newTransport().sendMail(mailOptions)

   }
   async sendWelcome() {
      await this.send('welcome', 'Welcome to the Natours Family!');
   }
   async sendPasswordReset() {
      await this.send('passwordReset', 'Your password reset token (valid for only 10 minutes)')
   }
}

But I get only this error and I didnt understand this error. What mean and how to solve this

error: Error: connect ECONNREFUSED 3.209.246.195:2525 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) { errno: -4078, code: 'ESOCKET', syscall: 'connect', address: '3.209.246.195', port: 2525, command: 'CONN' }

and I get this 'production' error:

error: Error: Message failed: 550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements at SMTPConnection._formatError (C:\Users\feruz\Desktop\complete\4-natours\node_modules\nodemailer\lib\smtp-connection\index.js:784:19) at SMTPConnection._actionSMTPStream (C:\Users\feruz\Desktop\complete\4-natours\node_modules\nodemailer\lib\smtp-connection\index.js:1674:34) at SMTPConnection. (C:\Users\feruz\Desktop\complete\4-natours\node_modules\nodemailer\lib\smtp-connection\index.js:1152:22) at SMTPConnection._processResponse (C:\Users\feruz\Desktop\complete\4-natours\node_modules\nodemailer\lib\smtp-connection\index.js:947:20) at SMTPConnection._onData (C:\Users\feruz\Desktop\complete\4-natours\node_modules\nodemailer\lib\smtp-connection\index.js:749:14) at TLSSocket.SMTPConnection._onSocketData (C:\Users\feruz\Desktop\complete\4-natours\node_modules\nodemailer\lib\smtp-connection\index.js:189:44) at TLSSocket.emit (events.js:315:20) at addChunk (_stream_readable.js:309:12) at readableAddChunk (_stream_readable.js:284:9) at TLSSocket.Readable.push (_stream_readable.js:223:10) { code: 'EMESSAGE', response: '550 The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements', responseCode: 550,
command: 'DATA' }

1
  • Yeah it is work for process.env.NODE_ENV!=='production'. but cant send email in sendgrid Commented Dec 28, 2021 at 16:41

1 Answer 1

0

IP, port that you use is incorrect or service is not running at port 2525 of 3.209.246.195. You should check it again.

Check connection: ping 3.209.246.195

Check service (port): telnet 3.209.246.195 2525

Not the answer you're looking for? Browse other questions tagged or ask your own question.