码迷,mamicode.com
首页 > Web开发 > 详细

nodejs 发送邮件配置

时间:2015-05-12 21:06:33      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:node   email   

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***29@qq.com',
    pass: '********'
  }
};

var mailOptions = {
  from: '870****9@qq.com',
  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****9@qq.com',
		pass: '******'
	}
};

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

email_tools.sendEmail(config, options);


nodejs 发送邮件配置

标签:node   email   

原文地址:http://blog.csdn.net/zimu002/article/details/45674339

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!