Sending emails using Mailgun with NodeMailer package

后端 未结 3 906
慢半拍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!

提交回复
热议问题