node 邮箱发送nodemailer

安装 npm install nodemailer --save
router文件夹新建nodemailer.``js

// 这是一个发送邮件的模块
const nodemailer = require('nodemailer');
// 简单配置
const params = {
        host: 'smtp.163.com',
        port: 465,
        secture: false,
        auth: {
            user: '188*******[email protected]',
            pass: '********' // 这里的密码不是 邮箱的登录密码 而是 通过邮箱内 设置 形成的授权码 这个可以自己进行设置
        }
    }
    // 邮件信息
const mailOptions = {
        from: '188*******[email protected]',
        to: '*******@163.com', // 这里可以发送多个用户  分别用 , 隔开
        subject: 'hello!',
        html: ' 那个小妖精是谁啊??????', // 这里可以是 html 标签 字符串
        // attachments: [{ // 这里可以添加附件  并且可以添加多个
        //     filename: 'test.txt',
        //     content: 'hello world!'
        //   },{
        //     filename: 'test.txt',
        //     content: 'hello world!',
        //     contentType: 'text/plain'
        //   }]
        // }
    }
    // 发送邮件
var send = function() {
    const transpoter = nodemailer.createTransport(params);
    transpoter.sendMail(mailOptions, (err, info) => {
        if (err) {
            console.log(err);
            return
        }
        console.log(info);
    });
}
module.exports = send;

1.app.js中

var postmailer = require('./routes/nodemailer');
app.use('/mailer', postmailer);

2.js中发送ajax调用

      $.ajax({
                url: '/mailer',
                type: 'post',
                dataType: 'json',
                success: function(data) {
                    console.log(data)
                },
                error: function(err) {
                    console.log(err)
                }
            })

猜你喜欢

转载自blog.csdn.net/colorset/article/details/82866074
今日推荐