標籤:彙報 https set receive idt sig apt 登入
功能每天定時,並把截到的圖片自動通過郵件發送。說明代碼注釋已經非常詳細,就不多做說明,需要的朋友自己查看代碼即可,主檔案Mail.js,檔案capturePart1.js,capturePart2.js,capturePart3.js,這裡只展示了capturePart1.js其他兩個類似。值得注意的是有登入許可權的網站一定要設定Cookie,需要截取高品質圖片的話截取時間一定設定長一些。Mail.js/************************************************************************************************************ * 定時發送郵件功能說明: * node.js必備安裝模組:node_modules-->phantomjs,nodemailer,node-schedule,moment * 涉及JS檔案:route-->mail.js,public-->js-->capturePart1.js,capturePart2.js,capturePart3.js * 儲存地址:public-->images-->mainPage.jpeg(1600*4200) * url:http://www.***********.com * 程式主要思路: * (1)phantomjs-->參照http://phantomjs.org/ * (2)nodemailer發送郵件-->參照https://www.npmjs.com/package/nodemailer * (3)node-schedule定時-->參照https://www.npmjs.com/package/node-schedule * 注意: * 改變發件伺服器請修改SMTP * 改變收件者請修改變數receiver * 改變郵件內容請修改變數html * 改變郵件附加圖片和附件請修改attachments * 改變功能請修改public-->js-->server.js * 改變定時功能請修改變數rule * ------Sweety***************************************************************************************************************///組件引入開始var schedule = require("node-schedule");var path = require(‘path‘);var childProcess = require(‘child_process‘);var phantomjs = require(‘phantomjs‘);var nodemailer = require("nodemailer");var moment = require("moment");//組件引入結束/*--------------------------------------------------------------------------------------------------------------------------------------------*///變數定義開始var today; //今天開始時間var binPath = phantomjs.path; //擷取phantomjs.exe路徑var jsPath = process.cwd()+"/public/js/"; //擷取server.js所在目錄var childArgs;//capturePart3();//capturePart1();//變數定義結束/*--------------------------------------------------------------------------------------------------------------------------------------------*///主程式開始var rule = new schedule.RecurrenceRule(); //schedule定時器rule.hour = 11;rule.minute = 0;rule.second = 0; //定時器規則設定(每天11點觸發事件)var j = schedule.scheduleJob(rule, function(){ var now = moment(); today = now.clone().add(-1, ‘days‘).format(‘YYYY-MM-DD‘); capturePart1(); //觸發事件(郵件發送功能包含在事件裡邊)});//主程式結束/*---------------------------------------------------------------------------------------------------------------------------------------------*///phantomjs開始(第一張)function capturePart1(){ childArgs = [ path.join(jsPath, ‘serverPart1.js‘), //server.js ‘ https://www.hao123.com ‘ //要的url ]; childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) { if(err) { console.log(err); //列印錯誤資訊 }else{ console.log("Captured Part1 Successful !!"); //列印正確資訊 capturePart2(); } });}//phantomjs結束(第一張)//phantomjs開始(第二張)function capturePart2(){ childArgs = [ path.join(jsPath, ‘serverPart2.js‘), //server.js路徑 ‘https://www.hao123.com ‘ //要的url ]; childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) { if(err) { console.log(err); //列印錯誤資訊 }else{ console.log("Captured Part2 Successful !!"); //列印正確資訊 capturePart3(); } });}//phantomjs結束(第二張)//phantomjs開始(第三張)function capturePart3(){ childArgs = [ path.join(jsPath, ‘serverPart3.js‘), //server.js路徑 ‘ https://www.hao123.com ‘ //要的url ]; childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) { if(err) { console.log(err); //列印錯誤資訊 }else{ console.log("Captured Part3 Successful !!"); //列印正確資訊 sent(); // 觸發發送郵件事件 } });}//phantomjs結束(第三張)/*-------------------------------------------------------------------------------------------------------------------------------------------------*///nodemailer發送郵件開始function sent(){ var imgPart1 = fs.readFileSync(process.cwd()+"/public/images/mainPagePart1.jpeg"); //圖片來源 var imgPart2 = fs.readFileSync(process.cwd()+"/public/images/mainPagePart2.jpeg"); //圖片來源 var imgPart3 = fs.readFileSync(process.cwd()+"/public/images/mainPagePart3.jpeg"); //圖片來源 var smtpTransport = nodemailer.createTransport("SMTP",{ //郵件SMTP設定(發送郵箱伺服器必須開啟SMTP) host: "smtp.xxxxx.com", // 主機 secureConnection: false, // 不使用 SSL port: 587, // SMTP 連接埠 auth: { user: "[email protected]", //使用者名稱 pass: "xxxxxx" //密碼 } }); var html = ‘<meta http-equiv="Content-Type" content="text/html; charset=utf-8">‘ + ‘XX好:</br>‘+ ‘ 下面為【XXXX】日報彙報(‘+today+‘)的內容,請參考</br>‘ + ‘ (日報詳細資料請點擊<a href="#" onclick="changeData();">此處登陸</a>查看)‘+ ‘</br>‘ + ‘<img src="cid:img1" id="img1">‘+ ‘<img src="cid:img2" id="img2">‘+ ‘<img src="cid:img3" id="img3">‘;//郵件內容(html代碼),img唯一指定地址對應cid(見mailOptions設定) var receiver = "[email protected]";//收件者清單 var cc = "[email protected],[email protected],[email protected]"; //抄送人列表 var bcc = "[email protected],[email protected]"; //密抄送人列表 var mailOptions = { //郵件內容選項設定 from: "<[email protected]>", //發件地址 //to: "[email protected]", to: receiver, //收件者 cc:cc, //抄送人 bcc:bcc, //密抄送人 subject:"【XXXX】日報彙報("+today+")", //郵件主題 text: "【XXXX】日報彙報("+today+")", // plaintext body html:html, //html內容 attachments: [ { filename: ‘mainPagePart1.jpeg‘, //圖片名稱 contents: imgPart1, //圖片來源 cid: ‘img1‘ //插入圖片標識 },{ filename: ‘mainPagePart2.jpeg‘, //圖片名稱 contents: imgPart2, //圖片來源 cid: ‘img2‘ //插入圖片標識 },{ filename: ‘mainPagePart3.jpeg‘, //圖片名稱 contents: imgPart3, //圖片來源 cid: ‘img3‘ //插入圖片標識 } ] }; smtpTransport.sendMail(mailOptions, function(error, response){//發送郵件 if(error){ console.log(error); //列印錯誤資訊 }else{ console.log("Sent Successful !!"); //列印正確資訊 } });}//nodemailer發送郵件結束function changeData(){}/*---------------------------------------------------------------------------------------------------------------------------------------------*/capturePart1.js//phantomjsvar page = require(‘webpage‘).create(), system = require(‘system‘), address;page.viewportSize = { width:1920, height: 1080};page.clipRect = { top: 200, left: 210, width: 1680, height: 1530 };page.customHeaders={"Cookie":"koa:sess=e*******=;koa:sess.sig=pjadZtLAVtiO6-Haw1vnZZWrRm8"};if (system.args.length === 1) { phantom.exit(1);} else { address = system.args[1]; page.open(address, function (status) { });}setTimeout(function() { console.log(""); console.log("### STEP 5: Close page and shutdown (with a delay)"); page.render(‘./public/images/mainPagePart1.jpeg‘, {format: ‘jpeg‘, quality: ‘100‘}); page.close(); setTimeout(function(){ phantom.exit(); }, 3000);}, 19000);
Nodejs+定時+發送郵件