nodejs的邮件模块nodemailer使用

在涉及到推广的时候,邮件群发或者说邮件自动发送是不可避免的,而nodejs在这个方面也有很好的支持模块,方便大家进行邮件的开发,

安装这个模块:

 

npm install [email protected] --save


这里的版本选择了0.7 。最新版本好像是2.X

参考网站是官方文档: https://nodemailer.com/nodemailer-0-7-deprecated/

设置参数:

 

var smtpTransport = email.createTransport("SMTP",{
                host: "qq", // 主机
                 secureConnection: true, // 使用 SSL
                 port: 465, // SMTP 端口  ownjlzrtmjfdfeje
               auth: {
                    user: "[email protected]",
                    pass: "123456"            //QQ第三方登入不要使用密码,使用授权码登入,可以网上看如何获取QQmail授权码,自动生成
                }
                });


邮件参数

 

 var mailOptions = {
                   from: "[email protected]", // sender address
                   to: "[email protected]", // list of receivers
                 subject: "the tittle", // Subject line

                    html: emailtext,
                   
              // attachments: attachmentsall
               }


可以重写上述的参数,上述的参数就是js的对象,可以修改和添加相应的内容,不过一定要注意参数的数据类型,

 mailOptions.html需要使用字符串来连接,例如'<p>this is the content to send</p>'

 mailOptions.attachments的话你需要定义一个数组对象,再向其中加入附件对象。切记不要用string

开始执行发送:

 

smtpTransport.sendMail(mailOptions,function(error, response){
                 if(error){
                console.log(error);
                }else{
                  console.log("Message sent: " + response.message);
                }
                console.log("ok");
               });

这样就可以发送邮件了。无论是群发还是单例发送。

 

 

 

猜你喜欢

转载自caoweiju.iteye.com/blog/2303348