Advertisement
Guest User

Untitled

a guest
Dec 5th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. import { Email } from "../Email";
  2. import { getConfigs } from "../../configs";
  3. import * as Imap from 'imap';
  4. import * as mailParser from 'mailparser';
  5. import * as fs from 'fs';
  6. import { getLatest } from '../email.filter';
  7. import { MessageText, AddressObject } from "mailparser";
  8. import * as moment from 'moment';
  9. import * as utf8 from 'utf8';
  10. import { EmailConfig } from "../index";
  11.  
  12.  
  13.  
  14. export async function getEmailBySubjectLast(
  15. subject: string,
  16. emailConfig: EmailConfig
  17. ): Promise<Email> {
  18. const imap = new Imap({
  19. user: emailConfig.auth.user,
  20. password: emailConfig.auth.pass,
  21. host: emailConfig.imapHost,
  22. port: emailConfig.imapPort,
  23. tls: true,
  24. authTimeout: 30000
  25. });
  26.  
  27. await connect(imap);
  28.  
  29. const emails: Email[] = await getEmails(imap, subject);
  30.  
  31. const email = getLatest(emails);
  32. await disconnect(imap);
  33. return email;
  34. }
  35.  
  36. async function getEmails(
  37. imap: Imap,
  38. subject: string
  39. ): Promise<Email[]> {
  40. return new Promise<Email[]>((resolve, reject) => {
  41. const emails: Email[] = [];
  42. imap.openBox('INBOX', (err, box) => {
  43. if (err) {
  44. return reject(err);
  45. }
  46. try {
  47. imap.search(['UNSEEN', ['SUBJECT', subject]], (err, uids) => {
  48. if (err) { return reject(err); }
  49. if (uids.length === 0) { return resolve(null); }
  50. const f = imap.fetch(uids, { bodies: [''], markSeen: true });
  51. const counter = [uids.length];
  52. f.on('message', async function (msg, seqno) {
  53. const message = await processMessage(msg, seqno, counter);
  54. emails.push(message);
  55. if (counter[0] === 0) {
  56. resolve(emails);
  57. }
  58. });
  59.  
  60. f.on('end', () => {
  61. });
  62. });
  63. } catch (err) {
  64. resolve(err);
  65. }
  66. });
  67. });
  68. }
  69.  
  70.  
  71. function processMessage(
  72. msg: Imap.ImapMessage,
  73. seqno: number,
  74. counter: number[]
  75. ): Promise<Email> {
  76. return new Promise<Email>((resolve, reject) => {
  77.  
  78. let email: Email = {
  79. body: '',
  80. to: '',
  81. from: '',
  82. date: null,
  83. id: seqno.toString(),
  84. subject: ''
  85. }
  86. const parser = new mailParser.MailParser();
  87. parser.on("headers", function (headers) {
  88. email.subject = headers.get('subject').toString();
  89. email.from = (headers.get('from') as AddressObject).text;
  90. email.to = (headers.get('to') as AddressObject).text;
  91. email.date = moment(headers.get('date').valueOf()).toDate();
  92. });
  93.  
  94. parser.on('data', data => {
  95. if (data.type === 'text') {
  96. email.body = data.text;
  97. counter[0]--;
  98. resolve(email);
  99. }
  100. });
  101.  
  102. parser.on('end', (parsed) => {
  103. });
  104.  
  105. msg.on("body", function (stream) {
  106. stream.on("data", function (chunk) {
  107. parser.write(chunk.toString("utf8"));
  108. });
  109. });
  110.  
  111. msg.once("end", function () {
  112. parser.end();
  113. });
  114. });
  115. }
  116.  
  117. function connect(imap: Imap
  118. ): Promise<void> {
  119. return new Promise((resolve, reject) => {
  120. imap.connect();
  121. imap.once('error', function (err: any) {
  122. reject(err);
  123. });
  124. imap.once('ready', function () {
  125. if (imap.state != 'authenticated') {
  126. imap.connect();
  127. } else {
  128. resolve();
  129. }
  130. });
  131. });
  132. }
  133.  
  134.  
  135. function disconnect(imap: Imap
  136. ): Promise<void> {
  137. return new Promise((resolve, reject) => {
  138. imap.once('end', function () {
  139. resolve();
  140. });
  141. imap.end();
  142. });
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement