Sending emails using Mailgun with NodeMailer package

后端 未结 3 905
慢半拍i
慢半拍i 2021-02-02 11:01

A couple of days ago I realized that Google has changed the security of gmail accounts, particularly for the possibility of sending emails from applications. After Googling arou

相关标签:
3条回答
  • 2021-02-02 11:30

    I created the Nodemailer transport for mailgun.

    Here it how it works.

    You install the package with npm install as you would do with any package, then in an empty file:

    var nodemailer = require('nodemailer');
    var mg = require('nodemailer-mailgun-transport');
    
    // This is your API key that you retrieve from www.mailgun.com/cp (free up to 10K monthly emails)
    var auth = {
      auth: {
        api_key: 'key-1234123412341234',
        domain: 'sandbox3249234.mailgun.org'
      }
    }
    
    var nodemailerMailgun = nodemailer.createTransport(mg(auth));
    
    nodemailerMailgun.sendMail({
      from: 'myemail@example.com',
      to: 'recipient@domain.com', // An array if you have multiple recipients.
      subject: 'Hey you, awesome!',
      text: 'Mailgun rocks, pow pow!',
    }, function (err, info) {
      if (err) {
        console.log('Error: ' + err);
      }
      else {
        console.log('Response: ' + info);
      }
    });
    

    Replace your API key with yours and change the details and you're ready to go!

    0 讨论(0)
  • 2021-02-02 11:43
    var nodemailer = require('nodemailer');
    // send mail with password confirmation
    var transporter = nodemailer.createTransport( {
        service:  'Mailgun',
        auth: {
         user: 'postmaster@sandboxXXXXXXXXXXXXXXXX.mailgun.org',
         pass: 'XXXXXXXXXXXXXXXX'   
        }
    });
    var mailOpts = {
        from: 'office@yourdomain.com',
        to: 'user@gmail.com',
        subject: 'test subject',
        text : 'test message form mailgun',
        html : '<b>test message form mailgun</b>'
    };
    transporter.sendMail(mailOpts, function (err, response) {
        if (err) {
         //ret.message = "Mail error.";
        } else {
         //ret.message = "Mail send.";
        }
    });
    
    0 讨论(0)
  • 2021-02-02 11:50

    It worked me, when I added the domain also to the auth object (not only the api_key). Like this:

    var auth = {
      auth: {
        api_key: 'key-12319312391',
        domain: 'sandbox3249234.mailgun.org'
      }
    };
    
    0 讨论(0)
提交回复
热议问题