Sending email via Node.js using nodemailer is not working

后端 未结 9 1784
悲&欢浪女
悲&欢浪女 2020-11-29 20:15

I\'ve set up a basic NodeJS server (using the nodemailer module) locally (http://localhost:8080) just so that I can test whether the server can actually send ou

相关标签:
9条回答
  • 2020-11-29 20:52

    And install smtp module as dependency:

    npm install smtp

    var nodemailer = require('nodemailer');
    
    var transporter = nodemailer.createTransport({
      service: 'gmail',
      type: "SMTP",
      host: "smtp.gmail.com",
      secure: true,
      auth: {
        user: 'writeYourGmailId@gmail.com',
        pass: 'YourGmailPassword'
      }
    });
    
    var mailOptions = {
      from: 'xyz.khan704@gmail.com',
      to: 'azran.khan704@gmail.com',
      subject: 'Sending Email to test Node.js nodemailer',
      text: 'That was easy to test!'
    };
    
    transporter.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent');
      }
    });
    

    Go to https://myaccount.google.com/lesssecureapps and change it ON because Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks.

    0 讨论(0)
  • 2020-11-29 20:52

    try this code its work for me.

    var http = require('http');
    var nodemailer = require('nodemailer');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
    
      var fromEmail = 'akarthi@xyz.com';
      var toEmail = 'akarthi@xyz.com';
    
      var transporter = nodemailer.createTransport({
        host: 'domain',
        port: 587,
        secure: false, // use SSL
        debug: true,
          auth: {
            user: 'fromEmail@xyz.com',
            pass: 'userpassword'
          }
      });
       transporter.sendMail({
          from: fromEmail,
          to: toEmail,
          subject: 'Regarding forget password request',
          text: 'This is forget password response from your app',
          html: '<p>Your password is <b>sample</b></p>'
      }, function(error, response){
          if(error){
              console.log('Failed in sending mail');
              console.dir({success: false, existing: false, sendError: true});
              console.dir(error);
              res.end('Failed in sending mail');
          }else{
              console.log('Successful in sending email');
              console.dir({success: true, existing: false, sendError: false});
              console.dir(response);
              res.end('Successful in sending email');
          }
      });
    }).listen(8000);
    console.log('Server listening on port 8000');
    

    response:

    Successful in sending email
    { success: true, existing: false, sendError: false }
    { accepted: [ 'akarthi@xyz.com' ],
      rejected: [],
      response: '250 2.0.0 uAMACW39058154 Message accepted for delivery',
      envelope: 
       { from: 'akarthi@xyz.com',
         to: [ 'akarthi@xyz.com' ] },
      messageId: '1479809555147-33de4987-29d605fa-6ee150f1@xyz.com' }
    
    0 讨论(0)
  • 2020-11-29 20:55

    For debugging purpose it is handy to implement a callback function (they never do on the nodemailer github page) which shows the error message (if there is one).

    transporter.sendMail({
        from: from,
        to: to,
        subject: subject,
        html: text
    }, function(err){
        if(err)
            console.log(err);
    })
    

    It helped me solve my problem... Turns out newer versions are not working properly:

    "Looks like nodemailer 1.0 has breaking changes so 0.7 must be used instead: http://www.nodemailer.com/

    Message posted on nodemailer as of 12/17/15:

    Do not upgrade Nodemailer from 0.7 or lower to 1.0 as there are breaking changes. You can continue to use the 0.7 branch as long as you like. See the documentation for 0.7 here."

    I found this answer here

    0 讨论(0)
  • 2020-11-29 21:04

    For those who actually want to use OAuth2 / don't want to make the app "less secure", you can achieve this by

    1. Search "Gmail API" from the google API console and click "Enable"
    2. Follow the steps at https://developers.google.com/gmail/api/quickstart/nodejs. In the quickstart.js file, changing the SCOPES var from ['https://www.googleapis.com/auth/gmail.readonly'] to ['https://mail.google.com/'] in the quickstart js file provided as suggested in troubleshooting at https://nodemailer.com/smtp/oauth2/
    3. After following the steps in (2), the generated JSON file will contain the acessToken, refreshToken, and expires attributes needed in the OAuth2 Examples for Nodemailer

    This way you can use OAuth2 authentication like the following

    let transporter = nodemailer.createTransport({
        service: 'Gmail',
        auth: {
            type: 'OAuth2',
            user: 'user@example.com',
            clientId: '000000000000-xxx0.apps.googleusercontent.com',
            clientSecret: 'XxxxxXXxX0xxxxxxxx0XXxX0',
            refreshToken: '1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx',
            accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x',
            expires: 1484314697598
        }
    });
    

    instead of storing your gmail password in plaintext and downgrading the security on your account.

    0 讨论(0)
  • 2020-11-29 21:05

    You only need App password for google auth, then replace your google password in your code. go here https://myaccount.google.com/apppasswords

    sample code:

    const nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
        service: "Gmail",
        auth: {
          user: 'example@gmail.com',
          pass: 'app password here'
        }
      });
    transporter.sendMail(option, function(error, info){
        if (error) {
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
    });
    

    0 讨论(0)
  • 2020-11-29 21:09

    i just set my domain to: smtp.gmail.com and it works. I am using a VPS Vultr.

    the code:

    const nodemailer = require('nodemailer');
    const ejs = require('ejs');
    const fs = require('fs');
    
    let transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'xxx@gmail.com',
            pass: 'xxx'
        }
    });
    
    let mailOptions = {
        from: '"xxx" <xxx@gmail.com>',
        to: 'yyy@gmail.com',
        subject: 'Teste Templete ✔',
        html: ejs.render( fs.readFileSync('e-mail.ejs', 'utf-8') , {mensagem: 'olá, funciona'})
    };
    
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message %s sent: %s', info.messageId, info.response);
    });
    

    my ejs template (e-mail.ejs):

    <html>
        <body>
            <span>Esse é um templete teste</span>
            <p> gerando com o EJS - <%=mensagem%> </p>
        </body>
    </html>
    

    Make sure:

    • install ejs: npm install ejs --save
    • install nodemailer: npm install nodemailer --save
    • ping to smtp.gmail.com works: ping smtp.gmail.com
    • change xxx@gmail.com to your gmail email
    • change yyy@gmail.com to the email that you want to send a email
    • Enable less secure apps
    • Disable Captcha temporarily

    have a nice day ;)

    0 讨论(0)
提交回复
热议问题