nodejs送信メール構成

1791 ワード

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);