简体   繁体   English

使用nodemailer发送邮件,需要步骤

[英]Sending mail using nodemailer, need the steps

I am new to testcafe and Node.js and I want to send mail using nodemailer .我是testcafe和 Node.js 的testcafe ,我想使用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安装 nodemailer: npm install nodemailer
  2. Create a Nodemailer transporter using either SMTP or some other transport mechanism使用 SMTP 或其他一些传输机制创建一个 Nodemailer 传输器
  3. Set up message options (who sends what to whom)设置消息选项(谁向谁发送什么)
  4. Deliver the message object using the sendMail() method of your previously created transporter:使用之前创建的传输器的sendMail()方法传递消息对象:
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?对于 SMTP,我是否需要从 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? net中的大多数示例都显示了Gmail,但我用我的公司邮件替换了它,我在这里考虑了smtp.abc.com我还需要做什么吗?

  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?对于 SMTP,我是否需要从 npm 安装任何东西?

No. Only nodemailer不,只有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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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