个性化阅读
专注于IT技术分析

如何在Node.js中使用nodemailer发送电子邮件(gmail,Outlook和Zoho)

点击下载

本文概述

在本文中, 你将学习如何使用nodemailer模块发送电子邮件。在nodemailer最知名的功能之间:

  • Node.js 0.10+, 没有使用会破坏你的生产应用程序的ES6恶作剧。
  • Unicode可使用任何字符, 包括完整的表情符号支持。
  • Windows –你可以像其他任何模块一样在Windows上使用npm安装它, 没有编译的依赖项。从Azure或Windows轻松使用它。
  • HTML内容以及纯文本替代。
  • 附件(包括用于发送较大文件的附件流)。
  • HTML中的嵌入式图像。
  • 使用SSL / STARTTLS的安全电子邮件传递。
  • 使用内置的SMTP传输或通过外部插件使用不同的传输方法。
  • 用于处理消息的自定义插件支持(添加DKIM签名, 使用markdown内容而不是HTML等)。
  • 具有自动访问令牌生成功能的Sane XOAUTH2登录(以及有关更新令牌的反馈)。
  • 使用节点电子邮件模板或自定义渲染器的简单内置模板。
  • SMTP连接(SOCKS, HTTP和自定义连接)的代理。

要求

如前所述, 为了在Node.js中发送电子邮件, 你将需要nodemailer模块。要将nodemailer添加为项目中的依赖项, 请在Node.js命令提示符下执行以下命令:

npm install nodemailer

你可以访问nodemailer的Github官方存储库以获取更多信息, 或者在此处访问NPM中的软件包站点。下载后, 你可以使用” require(‘nodemailer’)”来要求该模块。

使用Gmail帐户发送

Google使用SSL加密和端口465。

注意:要使用Gmail, 你可能需要在Gmail帐户中配置”允许安全程度较低的应用程序”, 除非你使用的是2FA, 在这种情况下, 你必须创建”应用程序专用”密码。你可能还需要使用”允许访问你的Google帐户”来解锁你的帐户才能使用SMTP。

var nodemailer = require('nodemailer');

// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com', port: 465, secure: true, // use SSL
    auth: {
        user: 'myemail@gmail.com', pass: 'myPassword'
    }
});

// setup e-mail data
var mailOptions = {
    from: '"Our Code World " <myemail@gmail.com>', // sender address (who sends)
    to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
    subject: 'Hello', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }

    console.log('Message sent: ' + info.response);
});

使用Zoho帐户发送

Zoho邮件使用SSL加密和端口465, 与Gmail相同。

var nodemailer = require('nodemailer');

// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
    host: 'smtp.zoho.com', port: 465, secure: true, // use SSL
    auth: {
        user: 'myzoho@zoho.com', pass: 'myPassword'
    }
});

// setup e-mail data, even with unicode symbols
var mailOptions = {
    from: '"Our Code World " <myzoho@zoho.com>', // sender address (who sends)
    to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
    subject: 'Hello ', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }

    console.log('Message sent: ' + info.response);
});

使用Outlook(或Hotmail)帐户发送

与Gmail或Zoho不同, Outlook在端口587中使用TLS加密。在这种情况下, 我们需要禁用默认的安全连接以使用SSLv3加密启用TLS。

var nodemailer = require('nodemailer');

// Create the transporter with the required configuration for Outlook
// change the user and pass !
var transporter = nodemailer.createTransport({
    host: "smtp-mail.outlook.com", // hostname
    secureConnection: false, // TLS requires secureConnection to be false
    port: 587, // port for secure SMTP
    tls: {
       ciphers:'SSLv3'
    }, auth: {
        user: 'mymail@outlook.com', pass: 'myPassword'
    }
});

// setup e-mail data, even with unicode symbols
var mailOptions = {
    from: '"Our Code World " <mymail@outlook.com>', // sender address (who sends)
    to: 'mymail@mail.com, mymail2@mail.com', // list of receivers (who receives)
    subject: 'Hello ', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }

    console.log('Message sent: ' + info.response);
});

或者, 如果你的帐户是hotmail而不是Outlook, 则可以通过以下传输方式使用内置hotmail服务:

var transport = nodemailer.createTransport("SMTP", {
    service: "hotmail", auth: {
        user: "user@hotmail.com", pass: "password"
    }
});

玩得开心 !

赞(1)
未经允许不得转载:srcmini » 如何在Node.js中使用nodemailer发送电子邮件(gmail,Outlook和Zoho)

评论 抢沙发

评论前必须登录!