עדי קוש
עדי קוש

Reputation: 73

Using nodemailer with different email providers

I'm using nodemailer to send emails from my web app. I already send emails from one gmail account to another gmail account using "service: 'gmail'".

const nodemailer = require("nodemailer");
const promisify = require("es6-promisify");

const transport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: "my gmail",
    pass: "my password"
  }
});

exports.send = async options => {
  const mailOptions = {
    from: options.email,
    to: '[email protected]',
    subject: options.subject,
    html: options.text,
    text: options.text
  };

  const sendMail = promisify(transport.sendMail, transport);
  return sendMail(mailOptions);
};

The code above works but I can't make it work for outlook accounts. Meaning it dose not work if I email to an outlook or hotmail account.

Moreover, I don't know how to register multiple services so I can send email to any email service (hotmail, outlook, gmail...) and not only gmail.

How can I do it ? Please help if you can.

Thank you, Adi

Upvotes: 6

Views: 9525

Answers (1)

Avinash Singh
Avinash Singh

Reputation: 5392

Here you can do following thing:

First you can use NodeJS Nodemailer Outlooknpm package, by this you can easily able to do it.

Or just try to make it by

For Outlook:

var transport = nodemailer.createTransport("SMTP", {
    host: "smtp-mail.outlook.com",
    secureConnection: false, 
    port: 587, 
    auth: {
        user: "[email protected]",
        pass: "XXXXXX"
    },
    tls: {
        ciphers:'SSLv3'
    }
});

For hotmail:

var transport = nodemailer.createTransport("SMTP", {
    service: "hotmail",
    auth: {
        user: "[email protected]",
        pass: "XXXXX"
    }
});

Together at a time: try some thing like this

var nodeoutlook = require('nodejs-nodemailer-outlook')
var nodemailer = require("nodemailer");
const promisify = require("es6-promisify");

nodeoutlook.sendEmail({
    auth: {
        user: "[email protected]",
        pass: "johnpassword"
    }, from: '[email protected]',
    to: '[email protected]',
    subject: 'Hey you, awesome!',
    html: '<b>This is bold text</b>',
    text: 'This is text version!'
    attachments: [
                   {   // file on disk as an attachment
                        filename: 'text3.txt',
                        path: '/path/to/file.txt' // stream this file
                   }
                ]
});


async function main(){
  let account = await nodemailer.createTestAccount();
  let transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email",
    port: 587,
    secure: false,
    auth: {
      user: account.user, 
      pass: account.pass 
    }
  });

  let mailOptions = {
    from: '"Fred " 
    to: "[email protected], [email protected]", // list of receivers
    subject: "Hello",
    text: "Hello world?", // plain text body
    html: "<b>Hello world?</b>" // html body
  };
  let info = await transporter.sendMail(mailOptions)
}

main().catch(console.error)

Hope it helps!!! thanks

Upvotes: 2

Related Questions