前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nodejs定时自动截图并发送给邮箱

Nodejs定时自动截图并发送给邮箱

作者头像
治电小白菜
发布2020-08-25 14:21:27
2K0
发布2020-08-25 14:21:27
举报
文章被收录于专栏:技术综合技术综合

Nodejs获取桌面截图,并定时发送给指定邮箱 代码地址: https://github.com/klren0312/NodejsGetScreenshotSend 前面还写过Python获取截图并发邮件的 代码地址:https://github.com/klren0312/PythonGetScreenshotSend

1.安装相关包

代码语言:javascript
复制
npm install --save screenshot-desktop //截图
npm install --save nodemailer         //发邮件
npm install --save node-schedule      //定时

2.screenshot-desktop

截图的包 网址: https://github.com/bencevans/screenshot-desktop

3.nodemailer

发邮件用的包 网址: https://nodemailer.com/about/

4.node-schedule

定时使用的包 网址: https://github.com/node-schedule/node-schedule

5.引入包

代码语言:javascript
复制
const screenshot = require('screenshot-desktop')
const nodemailer = require('nodemailer')
const schedule = require('node-schedule')
const fs = require('fs')//nodejs 文件操作的包

6.配置发送邮件的邮箱

代码语言:javascript
复制
var transporter = nodemailer.createTransport({
    host:"smtp服务器地址",
    secure:true,
    port:端口, //端口注意了 分两种,一种是有ssl的一种是没有ssl
    auth: {
        user: "发送的邮箱",
        pass: "密码"
    },
    debug: true // include SMTP traffic in the logs
});

7.设置定时

设置每一分钟发送一次

代码语言:javascript
复制
var rule = new schedule.RecurrenceRule();
rule.second = 10;
var j = schedule.scheduleJob(rule,function(){

})

8.设置截图

代码语言:javascript
复制
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调用。

代码语言:javascript
复制
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.发送邮件

代码语言:javascript
复制
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();
});
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.安装相关包
  • 2.screenshot-desktop
  • 3.nodemailer
  • 4.node-schedule
  • 5.引入包
  • 6.配置发送邮件的邮箱
  • 7.设置定时
  • 8.设置截图
  • 9.设置发送的邮件内容
  • 10.发送邮件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档