Nodejsタイミング自動スクリーンショットとメールボックスへの送信

2542 ワード

Nodejsはデスクトップのスクリーンショットを取得し、指定したメールボックスコードアドレスにタイミングよく送信します.https://github.com/klren0312/NodejsGetScreenshotSendPythonがスクリーンショットと同時メールを取得するコードアドレスも前に書いたことがあります.https://github.com/klren0312/PythonGetScreenshotSend
1.関連パッケージのインストール
npm install --save screenshot-desktop //  
npm install --save nodemailer         //   
npm install --save node-schedule      //  

2. screenshot-desktop
スクリーンショットのパッケージのURL:https://github.com/bencevans/screenshot-desktop
3. nodemailer
メール用のパッケージアドレス:https://nodemailer.com/about/
4. node-schedule
定期的に使用するパッケージのWebサイト:https://github.com/node-schedule/node-schedule
5.導入パッケージ
const screenshot = require('screenshot-desktop')
const nodemailer = require('nodemailer')
const schedule = require('node-schedule')
const fs = require('fs')//nodejs       

6.メールを送信するメールボックスの設定
var transporter = nodemailer.createTransport({
    host:"smtp     ",
    secure:true,
    port:  , //         ,    ssl      ssl
    auth: {
        user: "     ",
        pass: "  "
    },
    debug: true // include SMTP traffic in the logs
});

7.設定タイミング
1分ごとに送信する設定
var rule = new schedule.RecurrenceRule();
rule.second = 10;
var j = schedule.scheduleJob(rule,function(){

})

8.スクリーンショットの設定
screenshot()
.then((img) => {
    //           out.jpg
    fs.writeFile('out.jpg', img,function(err){
        if(err){
            throw err
        }
        console.log('written to out.jpg')
    });
})

9.送信するメールの内容を設定する
公式のexampleを見て、画像は次のattachmentsに書いてcidを提供して、上のhtmlのimgに呼び出します.
var message = {
    from:"      ",
    to:"      ",
    subject:"    ",
    html:'    :![](cid:test)',
    //    ,  cid    img  
    attachments:[
        {
           filename: 'out',
           path: __dirname + '/out.jpg',
           cid: 'test' // should be as unique as possible
        }
    ]
}

10.メールの送信
transporter.sendMail(message, (error, info) => {
    if (error) {
        console.log('Error occurred');
        console.log(error.message);
        return;
    }
    console.log('Message sent successfully!');
    console.log('Server responded with "%s"', info.response);
    transporter.close();
});