Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Email } from "../Email";
- import { getConfigs } from "../../configs";
- import * as Imap from 'imap';
- import * as mailParser from 'mailparser';
- import * as fs from 'fs';
- import { getLatest } from '../email.filter';
- import { MessageText, AddressObject } from "mailparser";
- import * as moment from 'moment';
- import * as utf8 from 'utf8';
- import { EmailConfig } from "../index";
- export async function getEmailBySubjectLast(
- subject: string,
- emailConfig: EmailConfig
- ): Promise<Email> {
- const imap = new Imap({
- user: emailConfig.auth.user,
- password: emailConfig.auth.pass,
- host: emailConfig.imapHost,
- port: emailConfig.imapPort,
- tls: true,
- authTimeout: 30000
- });
- await connect(imap);
- const emails: Email[] = await getEmails(imap, subject);
- const email = getLatest(emails);
- await disconnect(imap);
- return email;
- }
- async function getEmails(
- imap: Imap,
- subject: string
- ): Promise<Email[]> {
- return new Promise<Email[]>((resolve, reject) => {
- const emails: Email[] = [];
- imap.openBox('INBOX', (err, box) => {
- if (err) {
- return reject(err);
- }
- try {
- imap.search(['UNSEEN', ['SUBJECT', subject]], (err, uids) => {
- if (err) { return reject(err); }
- if (uids.length === 0) { return resolve(null); }
- const f = imap.fetch(uids, { bodies: [''], markSeen: true });
- const counter = [uids.length];
- f.on('message', async function (msg, seqno) {
- const message = await processMessage(msg, seqno, counter);
- emails.push(message);
- if (counter[0] === 0) {
- resolve(emails);
- }
- });
- f.on('end', () => {
- });
- });
- } catch (err) {
- resolve(err);
- }
- });
- });
- }
- function processMessage(
- msg: Imap.ImapMessage,
- seqno: number,
- counter: number[]
- ): Promise<Email> {
- return new Promise<Email>((resolve, reject) => {
- let email: Email = {
- body: '',
- to: '',
- from: '',
- date: null,
- id: seqno.toString(),
- subject: ''
- }
- const parser = new mailParser.MailParser();
- parser.on("headers", function (headers) {
- email.subject = headers.get('subject').toString();
- email.from = (headers.get('from') as AddressObject).text;
- email.to = (headers.get('to') as AddressObject).text;
- email.date = moment(headers.get('date').valueOf()).toDate();
- });
- parser.on('data', data => {
- if (data.type === 'text') {
- email.body = data.text;
- counter[0]--;
- resolve(email);
- }
- });
- parser.on('end', (parsed) => {
- });
- msg.on("body", function (stream) {
- stream.on("data", function (chunk) {
- parser.write(chunk.toString("utf8"));
- });
- });
- msg.once("end", function () {
- parser.end();
- });
- });
- }
- function connect(imap: Imap
- ): Promise<void> {
- return new Promise((resolve, reject) => {
- imap.connect();
- imap.once('error', function (err: any) {
- reject(err);
- });
- imap.once('ready', function () {
- if (imap.state != 'authenticated') {
- imap.connect();
- } else {
- resolve();
- }
- });
- });
- }
- function disconnect(imap: Imap
- ): Promise<void> {
- return new Promise((resolve, reject) => {
- imap.once('end', function () {
- resolve();
- });
- imap.end();
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement