nodejs 发送邮件配置

1,使用nodemailer

npm install nodemailer --save-dev

2,创建一个发送邮件的工具包,如email_server.js

var nodemailer = require('nodemailer');

var internals = {};

internals.extend = function (o, n) {
  for (var p in n) {
    if (n.hasOwnProperty(p)) {
      o[p] = n[p];
    }
  };
  return o;
};

var mailConfig = {
  service: 'qq',
  auth: {
    user: '8707***[email protected]',
    pass: '********'
  }
};

var mailOptions = {
  from: '870****[email protected]',
  to: 'noahli@**.com',
  subject: '测试邮件',
  text: '测试邮件内容,请不要回复'
};

internals.sendEmail = function (_config,_options, attachment) {
  mailConfig = internals.extend(mailConfig,_config);
  mailOptions = internals.extend(mailOptions,_options);
  
  //执行发送操作
  var transporter = nodemailer.createTransport(mailConfig);
  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log("邮件发送错误:" + error);
    } else {
      console.log("邮件发送成功:" + info.response);
    }
  });
};

exports = module.exports = internals;

3,使用工具发送邮件

var email_tools = require('./service/email_server');

//发送邮箱配置
var config = {
	service: 'qq',
	auth: {
		user: '870****[email protected]',
		pass: '******'
	}
};

//收件箱配置
var options = {
	subject: "测试邮件",
	text: "我是邮件内容",
	to: "noahli@****.com",
	cc:"870***[email protected],51****[email protected]",
	sender:"imdou8",
	attachments:[
		{
			filename: 'app.js',
                        //content: 'hello world!'
			path:"app.js"
		}
	],
	html:{path:"tpl/index.html"}
};

email_tools.sendEmail(config, options);


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。