简体   繁体   中英

Sending mail using nodemailer, need the steps

I am new to testcafe and Node.js and I want to send mail using nodemailer . Can someone give me the steps? I tried few I am not sure if I am doing something incorrectly.

Here are the steps that followed:

  1. install nodemailer: npm install nodemailer
  2. Create a Nodemailer transporter using either SMTP or some other transport mechanism
  3. Set up message options (who sends what to whom)
  4. Deliver the message object using the sendMail() method of your previously created transporter:
var mailer = require("nodemailer");

var transporter = mailer.createTransport({
  host: "smtp.abc.email",
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
    user: "email@abc.com", //
    pass: "password" //
  }
});

// send mail with defined transport object
var mail = {
  from: "email@abc.com", // sender address
  to: "email@abc.com", // list of receivers
  subject: "hello", // Subject line
  text: "Hello world?", // plain text body
  html: "<b>Hello world?</b>" // html body
};

transporter.sendMail(mail, function(error, response) {
  if (error) {
    console.log(error);
  } else {
    console.log("Message sent: " + response.message);
  }

  transporter.close();
});

I have the following questions:

  1. For SMTP do I need to install anything from npm?

  2. Most of the examples in net shows Gmail, but I replace it with my company mail, I have considered here as smtp.abc.com do I need to do anything else?

  3. I want to send mail from my company email, what different setting is needed for that?

  1. For SMTP do I need to install anything from npm?

No. Only nodemailer

  1. I want to send mail from my company email, what different setting is needed for that?
import * as nodeMailer from "nodemailer";

const MAILER_CONFIG = Object.freeze({
    host: "smtp.mycompany.com",
    port: 25,
    secure: false, // true for 465 SMTP port, otherwise false
    auth: {
        user: "username",
        pass: "password"
    }
});

const MAIL_OPTIONS = Object.freeze({
    from: mailSender,
    to: mailReceiver,
    subject: mailSubject,
    text: mailBodyText,
    html: mailBodyHTML
});

const TRANSPORTER = Object.freeze(nodeMailer.createTransport(MAILER_CONFIG));

const mailSendingResult = await TRANSPORTER.sendMail(MAIL_OPTIONS);

TRANSPORTER.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM