美文网首页
nodejs实现发邮件

nodejs实现发邮件

作者: 然网名并卵 | 来源:发表于2018-11-21 19:35 被阅读13次

    nodejs有许许多多的功能模块,今天给大家演示如何用nodejs实现发邮件的效果,我们以QQ邮箱为例!

    首先我们要在QQ邮箱的 设置->账户中把POP3/SMTP服务 打开,然后获得授权码!

    然后我们现在前台简单布一下局:

    <form action="/sendemail" method="post">

        收件人:<input type="text" name="user">

        内容:<textarea name="some" id="" cols="30" rows="10">

        <input type="submit">

    </form>

    首先下载模块 npm i nodemailer -D

    然后引入模块var nodemailer=require('nodemailer');

    然后配置,app.post('/sendemail',function (req,res) {

           //1、配置邮箱

            var email = nodemailer.createTransport({

               host:'smtp.qq.com',//QQ邮箱的服务器

                port:587,      //端口号

                secure:false, //465为true,其他为false

                auth: {

                    user:'XXXXXXXX@qq.com', // 自己的邮箱

                    pass:'xxxxxxxxxxxx'// 授权码

                }

    });

        //2、配置内容

        var msg = {

            from:'XXXXXXXX<XXXXXXXXXXX@qq.com>', // 收件人显示的发件人信息

            to: req.body.user, // 目标邮箱号

            subject:'Hello ✔',

            text: req.body.some // 发送的内容

        };

        //3.发送邮件

        email.sendMail(msg,function (err,data) {

           res.send({error:0});    //发送完毕后返回

            email.close();      //发送完毕后关闭

        })

    })

    这样我们就可以在自己的页面上发送QQ邮箱啦!是不是很简单呢!

    相关文章

      网友评论

          本文标题:nodejs实现发邮件

          本文链接:https://www.haomeiwen.com/subject/wcqhqqtx.html