gxd-vue-library
Version:
依赖与element Ui插件库,聚福宝福利PC端插件库
100 lines (86 loc) • 2.53 kB
JavaScript
;
const nodemailer = require("nodemailer");
const fileHepler = require('./../fileHepler');
const basePath = require('./../path');
const clog = require('./../clog');
const emailRoot = '/Users/shiyonggao/home/root/project';
const template = require('./../lib/template');
const buildTemplateDirectory = basePath.buildTemplateDirectory;
const {
dateToTime,
beautifyFile,
getArgv
} = require('./../lib/utils')
/**
* @description 创建邮件服务
*/
const transporter = nodemailer.createTransport({
// 163邮箱为163, qq邮箱为qq, 谷歌邮箱为gmail...
service: '163',
auth: {
user: 'gaoshiyong1272@163.com',
pass: 'gao1272gsy'
}
});
/**
* @description 设置发送邮件主体
* @param email
* @param html
* @param title
* @returns {{subject: *, from: string, html: *, to: *, text: string}}
*/
const getMailOptions = (email , title, html = '测试邮件') => {
let temp = {
from: '高世勇 <gaoshiyong1272@163.com>',
to: email['emailTo'].join(','),
subject: title,
text: 'text',
html: html
};
if (email['emailCC']) temp['cc'] = email['emailCC'];
if (email['emailBCC']) temp['bcc'] = email['emailBCC'];
return temp
};
let params = getArgv();
const init = (params)=> {
const name = params[0];
const env = params[1];
if (!fileHelper.existFileSync(emailRoot + '/' + name + '.js')) {
clog('邮件配置不存在!', 'red');
return
}
const email = require(emailRoot + '/' + name + '.js');
//设置标题
let title = `${email['name']} 项目的【提侧邮件】`;
let fileName = '/email/pro_test.art';
if (env === 'production') {
title = `${email['name']} 项目的【上线邮件】`;
fileName = '/email/pro_done.art';
}
let bottom = template(basePath.buildTemplateDirectory + '/email/bottom.art', {});
let html = template(basePath.buildTemplateDirectory + fileName, {
email: email,
nowTime: dateToTime(Math.floor(new Date().getTime() / 1000), 'date'),
bottom,
env,
func: email.func[email['currentVersion'].join('-')]
});
html = beautifyFile(html, 'html_beautify');
console.log(html)
//发送邮件
transporter.sendMail(getMailOptions(email, title, html), function (error, info) {
if (error) {
clog('Message sent fail!', 'red');
return console.log(error);
}
clog('Message sent success!', 'green');
// 关闭连接池
transporter.close();
});
};
if(params.length === 2) {
init(params)
}
else {
clog('配置参数错误!', 'red');
}