Node.js - 群发邮件

一、安装邮件服务模块 - nodemailer

npm install --save nodemailer 

二、新建一个 js 文件,配置 nodemailer (下面以 QQ 邮箱为例)

"use strict";

const nodemailer = require('nodemailer'); // 引入 nodemailder
let Email_QQ = {
    // 以 QQ 邮箱为例子代码如下,如需配置其他邮箱,可查看文章第三点 nodemailer 各邮箱相关配置 
    host: 'smtp.qq.com',
    port: 465,
    secure: true,
    auth: {
        user: '[email protected]', // 发送人邮箱
        // 这里是 stmp 授权码,并非邮箱密码,
        // 如未开通 stmp 请查阅 https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
        pass: 'qqqqqqqqqqqqqq'
    }
}

function sendEMail(mail, text) {
    // 创建发送邮件服务 nodemailer 对象,传入 QQ 设置,当然你也可以开发接口从前端传入不同的 user 和对应的 pass
    let transporter = nodemailer.createTransport(Email_QQ);

    // 邮件的相关信息
    let msg = {
        from: '[email protected]', // 发送人邮箱
        to: mail, // 接收者邮箱,多个邮箱用逗号间隔

        subject: 'nodemailer 群发邮件测试~~!', // 邮件标题

        // text: '纯文本内容。。。', // text 和 html 属性二选一
        html: text // html 文本内容,可以添加 html 元素
    }

    // 发送邮件
    transporter.sendMail(msg, (err, res) => {

        if (res) {
            let len = res.accepted.length
            let from = res.envelope.from
            console.log('已从' + from + '邮箱成功发出' + len + '个邮件')
        } else {
            console.log('err : ', err)
        }
    })
}
module.exports = sendEMail;

三、nodemailer 各邮箱相关配置

{
    "1und1": {
        "host": "smtp.1und1.de",
        "port": 465,
        "secure": true,
        "authMethod": "LOGIN"
    },

    "AOL": {
        "domains": ["aol.com"],
        "host": "smtp.aol.com",
        "port": 587
    },

    "DebugMail": {
        "host": "debugmail.io",
        "port": 25
    },

    "DynectEmail": {
        "aliases": ["Dynect"],
        "host": "smtp.dynect.net",
        "port": 25
    },

    "FastMail": {
        "domains": ["fastmail.fm"],
        "host": "smtp.fastmail.com",
        "port": 465,
        "secure": true
    },

    "GandiMail": {
        "aliases": ["Gandi", "Gandi Mail"],
        "host": "mail.gandi.net",
        "port": 587
    },

    "Gmail": {
        "aliases": ["Google Mail"],
        "domains": ["gmail.com", "googlemail.com"],
        "host": "smtp.gmail.com",
        "port": 465,
        "secure": true
    },

    "Godaddy": {
        "host": "smtpout.secureserver.net",
        "port": 25
    },

    "GodaddyAsia": {
        "host": "smtp.asia.secureserver.net",
        "port": 25
    },

    "GodaddyEurope": {
        "host": "smtp.europe.secureserver.net",
        "port": 25
    },

    "hot.ee": {
        "host": "mail.hot.ee"
    },

    "Hotmail": {
        "aliases": ["Outlook", "Outlook.com", "Hotmail.com"],
        "domains": ["hotmail.com", "outlook.com"],
        "host": "smtp.live.com",
        "port": 587
    },

    "iCloud": {
        "aliases": ["Me", "Mac"],
        "domains": ["me.com", "mac.com"],
        "host": "smtp.mail.me.com",
        "port": 587
    },

    "mail.ee": {
        "host": "smtp.mail.ee"
    },

    "Mail.ru": {
        "host": "smtp.mail.ru",
        "port": 465,
        "secure": true
    },

    "Maildev": {
        "port": 1025,
        "ignoreTLS": true
    },

    "Mailgun": {
        "host": "smtp.mailgun.org",
        "port": 465,
        "secure": true
    },

    "Mailjet": {
        "host": "in.mailjet.com",
        "port": 587
    },

    "Mailosaur": {
        "host": "mailosaur.io",
        "port": 25
    },

    "Mailtrap": {
        "host": "smtp.mailtrap.io",
        "port": 2525
    },

    "Mandrill": {
        "host": "smtp.mandrillapp.com",
        "port": 587
    },

    "Naver": {
        "host": "smtp.naver.com",
        "port": 587
    },

    "One": {
        "host": "send.one.com",
        "port": 465,
        "secure": true
    },

    "OpenMailBox": {
        "aliases": ["OMB", "openmailbox.org"],
        "host": "smtp.openmailbox.org",
        "port": 465,
        "secure": true
    },

    "Outlook365": {
        "host": "smtp.office365.com",
        "port": 587,
        "secure": false
    },

    "Postmark": {
        "aliases": ["PostmarkApp"],
        "host": "smtp.postmarkapp.com",
        "port": 2525
    },

    "qiye.aliyun": {
        "host": "smtp.mxhichina.com",
        "port": "465",
        "secure": true
    },

    "QQ": {
        "domains": ["qq.com"],
        "host": "smtp.qq.com",
        "port": 465,
        "secure": true
    },

    "QQex": {
        "aliases": ["QQ Enterprise"],
        "domains": ["exmail.qq.com"],
        "host": "smtp.exmail.qq.com",
        "port": 465,
        "secure": true
    },

    "SendCloud": {
        "host": "smtpcloud.sohu.com",
        "port": 25
    },

    "SendGrid": {
        "host": "smtp.sendgrid.net",
        "port": 587
    },

    "SendinBlue": {
        "host": "smtp-relay.sendinblue.com",
        "port": 587
    },

    "SendPulse": {
        "host": "smtp-pulse.com",
        "port": 465,
        "secure": true
    },

    "SES": {
        "host": "email-smtp.us-east-1.amazonaws.com",
        "port": 465,
        "secure": true
    },

    "SES-US-EAST-1": {
        "host": "email-smtp.us-east-1.amazonaws.com",
        "port": 465,
        "secure": true
    },

    "SES-US-WEST-2": {
        "host": "email-smtp.us-west-2.amazonaws.com",
        "port": 465,
        "secure": true
    },

    "SES-EU-WEST-1": {
        "host": "email-smtp.eu-west-1.amazonaws.com",
        "port": 465,
        "secure": true
    },

    "Sparkpost": {
        "aliases": ["SparkPost", "SparkPost Mail"],
        "domains": ["sparkpost.com"],
        "host": "smtp.sparkpostmail.com",
        "port": 587,
        "secure": false
    },

    "Tipimail": {
        "host": "smtp.tipimail.com",
        "port": 587
    },

    "Yahoo": {
        "domains": ["yahoo.com"],
        "host": "smtp.mail.yahoo.com",
        "port": 465,
        "secure": true
    },

    "Yandex": {
        "domains": ["yandex.ru"],
        "host": "smtp.yandex.ru",
        "port": 465,
        "secure": true
    },

    "Zoho": {
        "host": "smtp.zoho.com",
        "port": 465,
        "secure": true,
        "authMethod": "LOGIN"
    },

    "126": {
        "host": "smtp.126.com",
        "port": 465,
        "secure": true
    },

    "163": {
        "host": "smtp.163.com",
        "port": 465,
        "secure": true
    }
}

四、调用

在调用的地方引入 sendmail 函数,可先收集所有收件人邮箱放进一个数组,再使用 join(',') 把每个邮箱用 ',' 分隔,最后传入sendmail 即可

const sendmail = require("../../model/sendmail.js"); // 引入刚才写好的 sendmail.js

let mail = ['[email protected]','[email protected]','[email protected]'].join(',') // "[email protected],[email protected],[email protected]"
sendmail(mail,'<H1>使用 node 群发邮件~~~</H1><a href="https://www.cnblogs.com/elfpower/p/9323121.html">Node.js - 群发邮件</a>');

五、效果

QQ 邮箱页面:

node.js 输出

六、总结

非常简单,按照步骤安装->设置->调用,几分钟立即实现功能,后期再按实际需求稍微封装即可。需要注意一点,发送人邮箱记得开通 smtp!!!

猜你喜欢

转载自www.cnblogs.com/elfpower/p/9323121.html