5

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: 'adikosh10@gmail.com',
    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

עדי קוש
  • 63
  • 2
  • 9

1 Answers1

2

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: "XXXXXX@outlook.com",
        pass: "XXXXXX"
    },
    tls: {
        ciphers:'SSLv3'
    }
});

For hotmail:

var transport = nodemailer.createTransport("SMTP", {
    service: "hotmail",
    auth: {
        user: "XXXXX@hotmail.com",
        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: "johnexample@organization.com",
        pass: "johnpassword"
    }, from: 'info@myorganization.com',
    to: 'jackexample@organization.com',
    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: "bar@example.com, baz@example.com", // 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

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35