「nodemailer」Node 邮件发送模块

4,266 阅读2分钟

简介

Nodemailer 是一个简单易用的 Node.JS 邮件发送模块(通过 SMTP,sendmail,或者 Amazon SES),支持 unicode,你可以使用任何你喜欢的字符集。

功能

  • 具有零依赖关系的单个模块–代码容易审核,因为没有死角
  • 高度重视安全性,没有人喜欢RCE漏洞
  • Unicode支持使用任何字符,包括表情符号💪
  • Windows支持–您可以使用安装npm在Windows上,与其他模块一样,没有编译的依赖项。从Azure或Windows盒子免费使用它
  • 使用HTML内容以及纯文本替代
  • 加 附件 到消息
  • 嵌入式的 HTML内容的图像附件–您的设计不会被阻塞
  • 使用TLS / STARTTLS的安全电子邮件传递
  • 不同 运输方式 除了内置 SMTP支持
  • 与签署消息 DKIM
  • 自订 插件支持 用于处理消息
  • 桑 OAuth2 认证方式
  • 代理人 用于SMTP连接
  • ES6码-没有更多的无意内存泄漏,由于吊装VAR的
  • 自动生成来自的电子邮件测试帐户电子邮件

使用

安装

npm install nodemailer --save
# or
yarn add nodemailer

使用

 const nodemailer = require('nodemailer')

 //创建一个SMTP客户端配置对象
 const transporter = nodemailer.createTransport({
     // 默认支持的邮箱服务包括:”QQ”、”163”、”126”、”iCloud”、”Hotmail”、”Yahoo”等
     service: "QQ",
     auth: {
         // 发件人邮箱账号
         user: '744xxx@qq.com',
         //发件人邮箱的授权码 需要在自己的邮箱设置中生成,并不是邮件的登录密码
         pass: '*******'
     }
 })

 // 配置收件人信息
 const receiver = {
     // 发件人 邮箱  '昵称<发件人邮箱>'
     from: `"张大力"<744xxx@qq.com>`,
     // 主题
     subject: '录取通知',
     // 收件人 的邮箱 可以是其他邮箱 不一定是qq邮箱
     to: 'yexxxx@xxx.com',
     // 可以使用html标签
     html: `
    <h1>你好,你的摸鱼程度符合本公司要求,请尽快加入我们</h1>
    <br>
    <table width="100%" style="border-collapse: collapse; border-spacing: 0;border: 0;height:120px;">
                    <tr height="40px" style="padding: 0;border: 0;">
                        <td style="width: 50px;font-size: 16px;font-weight: bold;line-height: 21px;color: rgba(255, 95, 46, 1);"
                            colspan="1"> 张大力 </td>
                        <td style="font-size: 16px;font-weight: 400;line-height: 21px;color: rgba(151, 151, 151, 1);"
                            colspan="3"> 摸鱼部 </td>
                    </tr>
                    <tr>
                        <td colspan="4"
                            style="font-size: 14px;font-weight: bold;line-height: 19px;color: rgba(151, 151, 151, 1);  padding: 0;border: 0;">
                            上班摸鱼有限公司</td>
                    </tr>
                    <tr>
                        <td style="font-size: 14px;font-weight: bold;line-height: 19px;color: rgba(151, 151, 151, 1);  padding: 0;border: 0;"
                            colspan="1"> 电话:</td>
                        <td style="position: relative;left: -10px;font-size: 12px;line-height: 19px;color: rgba(151, 151, 151, 1); padding: 0;border: 0;"
                            colspan="1" style="position: relative;left: -10px;font-size: 12px;line-height: 19px;color:
                            rgba(151, 151, 151, 1);">
                            139 1xxx 5xxx </td>
                        <td style="font-size: 14px;font-weight: bold;line-height: 19px;color: rgba(151, 151, 151, 1);  padding: 0;border: 0;"
                            colspan="1"> 热线:</td>
                        <td style="position: relative;left: -10px;font-size: 12px;line-height: 19px;color: rgba(151, 151, 151, 1); padding: 0;border: 0;"
                            colspan="1">400 0xx 5xxx</td>
                    </tr>
                    <tr>
                        <td style="font-size: 14px;font-weight: bold;line-height: 19px;color: rgba(151, 151, 151, 1);  padding: 0;border: 0;"
                            colspan='1'> 官网:</td>
                        <td style="position: relative;left: -10px;font-size: 12px;line-height: 19px;color: rgba(151, 151, 151, 1); padding: 0;border: 0;"
                            colspan="3"> www.moyu.com
                        </td>
                    </tr>
                    <tr>
                        <td
                            style="font-size: 14px;font-weight: bold;line-height: 19px;color: rgba(151, 151, 151, 1);  padding: 0;border: 0;">
                            地址:</td>
                        <td style="position: relative;left: -10px;font-size: 12px;line-height: 19px;color: rgba(151, 151, 151, 1); padding: 0;border: 0;"
                            colspan="3">世界摸鱼广场 </td>
                    </tr>
                </table>
    `
 }

 // 发送邮件 
 transporter.sendMail(receiver, (error, info) => {
     if (error) {
         return console.log('发送失败:', error);
     }
     transporter.close()
     console.log('发送成功:', info.response)
 })

发送效果

配置公司邮箱

公司邮箱一般为mail.company.com,不在默认支持的邮箱服务内,所以要另外进行配置. 这里用企业微信邮箱做例子.

 const config = {
	 // 配置服务商提供的发送服务器和端口号
     host: "smtp.exmail.qq.com",
     port: '465',
     auth: {
         // 发件人邮箱账号
         user: 'xxx@company.com',
         //发件人邮箱的授权码 需要在自己的邮箱设置中生成,并不是邮件的登录密码
         pass: '*******'
     }
 }
 const transporter = nodemailer.createTransport(config)
 // 发送方法与上述一致
 // ....