• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

NodeJS 实时收发 QQ 邮件

武飞扬头像
甘宇辉
帮助1

安装依赖 pnpm i mailparser imap moment

import { MailParser } from 'mailparser';
import * as Imap from 'imap';
import { createWriteStream, readFileSync, unlinkSync } from 'fs';
import { join } from 'path';
import * as moment from 'moment';

async QQ() {
		const imap = new Imap({
			/** QQ 邮箱 */
            user: 'xxx@qq.com',
            /** 这里填写的不是 QQ邮箱的密码,这里是 pop3 的密钥 */
            password: 'xxxxx',
            host: 'pop.qq.com',
            tls: false,
            authTimeout:1000,
            tlsOptions: { rejectUnauthorized: false }
        });
        const search = () => imap.search([['UNSEEN', ['SINCE', (new Date( new Date() - (5 * 60 * 1000))).toISOString()]]], (err, results) => {
            if (!err) {
                try {
                    const f = imap.fetch(results, { 
                        markSeen: false,
                        bodies: '',
                        struct: true,
                    });
                    f.on('message', (msg) => {
                        const mailparser = new MailParser();
                        const info = {
                            theme: '',
                            form: '',
                            mailName: '',
                            to: '',
                            datatime: '',
                            html: null,
                            text: null,
                            uid: null,
                            buffer: null,
                        };
                        let status = 0;
                        const next = (data: any) => {
                            imap.setFlags([data.uid], 'SEEN', () => {});
                            /** 这里拿到的 data 可以直接进行数据库存储 */
                            console.log(data);
                        }
                        msg.on('body', (stream) => {
                            stream.pipe(mailparser);
                            mailparser.on("headers", (headers: any) => {
                                info.theme = headers.get('subject');
                                info.form = headers.get('from').value[0].address;
                                info.mailName = headers.get('from').value[0].name;
                                info.to = headers.get('to').value[0].address;
                                info.datatime = moment(headers.get('date')).format('YYYY-MM-DD HH:mm:ss');
                                status   ;
                                if (status === 3) {
                                    next(info);
                                }
                            });
                            mailparser.on("data", (data) => {
                                if (data.type === 'text') {
                                    status   ;
                                    info.html = data.html;
                                    info.text = data.text;
                                    if (status === 3) {
                                        next(info);
                                    }
                                }
                                if (data.type === 'attachment') {
                                    const path = join(__dirname, '..', '..', '..', '..', 'public', 'mailer', `${new Date().valueOf()}.${data.filename.split('.')[data.filename.split('.').length - 1]}`);
                                    const ws = createWriteStream(path);
                                    ws.on('close', () => {
                                        status   ;
                                        info.buffer = readFileSync(path);
                                        unlinkSync(path);
                                        if (status === 3) {
                                            next(info);
                                        }
                                    });
                                    data.content.pipe(ws);
                                    data.release();
                                }
                            });
                        });
                        msg.on('attributes', (data) => {
                            info.uid = data.uid;
                            status   ;
                            if (status === 3) {
                                next(info);
                            }
                        });
                    });
                } catch(err) {}
            } else {
                throw err;
            }
        });
        let init = true;        
        imap.once('ready', () => {
            imap.openBox('INBOX', true, (err) => {
                if (!err) {
                    init = false;
                    search();
                } else {
                    throw err
                }
            });
        });
        imap.on('mail', () => {
            if (!init) {
                search()
            }
        });
        imap.connect();
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjicbf
系列文章
更多 icon
同类精品
更多 icon
继续加载