Sending email from local host with Nodemailer

前端 未结 5 1253
小蘑菇
小蘑菇 2020-12-20 13:38

I\'d like to send mails on my local server but it seems not working with Nodemailer and NodeJS.

Is there any solutions to send mails from local?

             


        
5条回答
  •  攒了一身酷
    2020-12-20 14:30

    In the localhost its not working because a secure and safetly connection is required for sending an email but using gmail[smtp(simple mail transfer protocol)] we can achieve it functionality.

    Don't forget to first do the setting - Allow less secure apps to access account.

    its given permission for access gmail account.

    by default this settings is off and you simply turn it on. Now move on coding part.

    ****************************--------------------------------------------------------------------*********************

    const nodemailer = require('nodemailer');
    
    let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
        user: 'your.gmail.account@gmail.com', // like : abc@gmail.com
        pass: 'your.gmailpassword'           // like : pass@123
    }
    });
    
    let mailOptions = {
     from: 'your.gmail.account@gmail.com',
     to: 'receivers.email@domain.com',
     subject: 'Check Mail',
     text: 'Its working node mailer'
    };
    
    transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
         return console.log(error.message);
      }
    console.log('success');
    }); 
    

提交回复
热议问题