简单两步使用node发送qq邮件

栏目: JavaScript · 发布时间: 6年前

内容简介:node发送邮件非常简单,这里只做qq的演示,你可以举一反三.使用nodemailer包接下来我们设置我们到发送内容

node发送邮件非常简单,这里只做qq的演示,你可以举一反三.

使用nodemailer包

let transporter = nodemailer.createTransport({
  // 使用qq发送邮件
  // 更多请查看支持列表:https://nodemailer.com/smtp/well-known/
  service: 'qq',
  port: 465, // SMTP 端口
  secureConnection: true, // 使用了 SSL
  auth: {
    user: '751734566@qq.com',
    // 这里密码不是qq密码,是你设置的smtp授权码
    // 获取qq授权码请看:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html
    pass: 'xxxxxxxx',
  }
});

接下来我们设置我们到发送内容

let mailOpt= {
  from: '"test" <xxxxxx@qq.com>',  // 你到qq邮箱地址
  to: 'xxxx@qq.com', // 接受人,可以群发填写多个逗号分隔
  subject: 'Hello', // 主题名(邮件名)
  // 可以发送text或者html格式,2选1
  // text: 'Hello world?', // 纯文本
  html: '<b>Hello world?</b>' // html
};

如果我们想发一个稍微漂亮到邮件怎么办?

我们可以使用html模板来实现

const template = require('art-template');

let html = template(__dirname + '/mail_temp.html', obj) // mail_temp.html为你想使用到页面模板,obj为你的参数

// 例如
obj = {
    name : 'test',
    phone : '183xxxxxxxx',
    time : new Date()
}
<section>
    新用户:{{name}}({{phone}})于{{time}}进行了注册.
</section>

所有准备完成,让我们发送邮件吧!

// 执行发送
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('邮件已发送成功,邮件id: %s', info.messageId);
});

文档参考

nodemailer : https://www.npmjs.com/package...

art-template : https://aui.github.io/art-tem...


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The  C Programming Language

The C Programming Language

Brian W. Kernighan、Dennis M. Ritchie / Prentice Hall / 1988-4-1 / USD 67.00

Presents a complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to ta......一起来看看 《The C Programming Language》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换