imap-reader
Advanced tools
Comparing version
179
index.js
const Imap = require('imap'); | ||
const { simpleParser } = require('mailparser'); | ||
const fs = require('fs') | ||
const jsdom = require("jsdom"); | ||
const { JSDOM } = jsdom; | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; | ||
const read = (data) => { | ||
@@ -61,3 +61,2 @@ return new Promise((resolve, reject) => { | ||
imap.once('error', err => { | ||
// console.log(err); | ||
resolve({ | ||
@@ -89,6 +88,178 @@ status: false, | ||
const sleep = (ms) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve() | ||
}, ms); | ||
}) | ||
} | ||
const waitForLink = ({ searchLinkString = 'http', imap, timeOut = 120000 }) => { | ||
return new Promise(async (resolve, reject) => { | ||
query = { | ||
timeOut: timeOut, | ||
searchLinkString: searchLinkString, | ||
imap: imap | ||
} | ||
query.searchLinkString = String(query.searchLinkString).toLowerCase() | ||
var wait_time = Date.now() | ||
var while_status = true | ||
var response_status = { | ||
status: false, | ||
data: 'No mail received' | ||
} | ||
var search_link = [] | ||
try { | ||
while (while_status) { | ||
try { | ||
var query_response = await read(query.imap).catch(err => { return { status: false } }) | ||
if (query_response.status == true && query_response.data.inbox_data.length > 0) { | ||
query_response.data.inbox_data.forEach(item => { | ||
try { | ||
var new_v = item.html | ||
new_v = String(new_v).toLowerCase() | ||
const { document } = (new JSDOM(new_v)).window; | ||
document.querySelectorAll('a').forEach(i2 => { | ||
try { | ||
if (i2.href.indexOf(query.searchLinkString) > -1) { | ||
search_link.push(i2.href) | ||
} | ||
} catch (err) { } | ||
}); | ||
} catch (err) { } | ||
}); | ||
} | ||
} catch (err) { response_status.data = 'An error occurred during processing' } | ||
if (while_status == true) { | ||
await sleep(1000) | ||
} | ||
if (query.timeOut != 0 && Date.now() > (wait_time + query.timeOut)) { | ||
while_status = false | ||
response_status.data = 'No mail with the link was received within the specified time' | ||
} | ||
if (search_link.length > 0) { | ||
while_status = false | ||
} | ||
} | ||
} catch (err) { response_status.data = 'An error occurred during processing' } | ||
if (search_link.length > 0) { | ||
response_status.status = true | ||
response_status.data = search_link | ||
} | ||
resolve(response_status) | ||
}) | ||
} | ||
const waitForCode = ({ querySelector = '', codeLength = 0, codeType = '0', imap, timeOut = 120000 }) => { | ||
return new Promise(async (resolve, reject) => { | ||
var wait_time = Date.now() | ||
var while_status = true | ||
var response_status = { | ||
status: false, | ||
data: 'No mail received' | ||
} | ||
var search_code = [] | ||
try { | ||
while (while_status) { | ||
try { | ||
var query_response = await read(imap).catch(err => { return { status: false } }) | ||
if (query_response.status == true && query_response.data.inbox_data.length > 0) { | ||
query_response.data.inbox_data.forEach(item => { | ||
var new_v = item.html | ||
var { document } = (new JSDOM(new_v)).window; | ||
var global_query_item = document | ||
var findResponse = false | ||
try { | ||
try { | ||
if (querySelector && querySelector.length > 0 && document.querySelector(querySelector)) { | ||
global_query_item = document.querySelector(querySelector) | ||
var { document } = (new JSDOM('<div>' + String(global_query_item.innerHTML) + '</div>')).window; | ||
global_query_item = document | ||
} | ||
} catch (err) { } | ||
try { | ||
if (codeLength > 0) { | ||
findResponse = true | ||
global_query_item.querySelectorAll('*').forEach(item1 => { | ||
try { | ||
if (item1.textContent.length == codeLength) { | ||
search_code.push(item1.textContent) | ||
} | ||
} catch (err) { } | ||
}); | ||
} | ||
} catch (err) { } | ||
try { | ||
if (codeType != '0') { | ||
findResponse = true | ||
var new_search_code = [] | ||
search_code.forEach(item2 => { | ||
item2 = item2.textContent | ||
var q_new = '' | ||
if (codeType == 'number') { | ||
q_new = String(item2).replace(/[^0-9]/g, '') | ||
} else if (codeType == 'lowerLetter') { | ||
q_new = String(item2).replace(/[^a-z]/g, '') | ||
} else if (codeType == 'upperLetter') { | ||
q_new = String(item2).replace(/[^A-Z]/g, '') | ||
} else if (codeType == 'allLetter') { | ||
q_new = String(item2).replace(/[^a-zA-Z]/g, '') | ||
} else if (codeType == 'nonAlfanumeric') { | ||
q_new = String(item2).replace(/[a-zA-Z0-9]/g, ''); | ||
} | ||
if (item2.length == q_new.length) { | ||
new_search_code.push(item2) | ||
} | ||
}); | ||
search_code = new_search_code | ||
} | ||
} catch (err) { } | ||
if (findResponse == false) { | ||
global_query_item = global_query_item.querySelector('body') | ||
if (global_query_item.textContent.length > 0) { | ||
search_code.push(global_query_item.textContent) | ||
} | ||
} | ||
} catch (err) { } | ||
}); | ||
} | ||
} catch (err) { } | ||
if (while_status == true) { | ||
await sleep(1000) | ||
} | ||
if (timeOut != 0 && Date.now() > (wait_time + timeOut)) { | ||
while_status = false | ||
response_status.data = 'No mail with the code was received within the specified time' | ||
} | ||
if (search_code.length > 0) { | ||
while_status = false | ||
} | ||
} | ||
} catch (err) { response_status.data = 'An error occurred during processing' } | ||
if (search_code.length > 0) { | ||
response_status.status = true | ||
search_code = search_code.filter((item, index, self) => { | ||
return self.indexOf(item) === index; | ||
}); | ||
response_status.data = search_code | ||
} | ||
resolve(response_status) | ||
}) | ||
} | ||
module.exports = { | ||
read | ||
read, | ||
waitForLink, | ||
waitForCode | ||
} |
{ | ||
"name": "imap-reader", | ||
"version": "1.0.7", | ||
"version": "1.0.9", | ||
"description": "Returns the header, mail and html information of all messages in the mailbox of the mail you give imap information in array type.", | ||
@@ -21,2 +21,3 @@ "main": "index.js", | ||
"imap": "^0.8.19", | ||
"jsdom": "^22.1.0", | ||
"mailparser": "^3.6.5" | ||
@@ -29,3 +30,3 @@ }, | ||
"readme": "README.md", | ||
"homepage":"https://www.npmjs.com/package/imap-reader" | ||
} | ||
"homepage": "https://www.npmjs.com/package/imap-reader" | ||
} |
143
README.md
# imap-reader | ||
- Returns the header, mail and html information of all messages in the mailbox of the mail you give imap information in array type. | ||
This library provides the following features | ||
- It allows you to get the header, sender, field, html, date information of all incoming mails belonging to that mail with a mail information. | ||
- It searches for a link that meets the conditions you want in incoming mails for a mail information. If it finds the link, it returns the full link to you. | ||
- Searches for a confirmation code that meets the conditions you want in incoming mails for an incoming mail information | ||
# Usage | ||
```bash | ||
@@ -12,2 +17,4 @@ npm i imap-reader | ||
### Receive all incoming mails | ||
```js | ||
@@ -31,2 +38,3 @@ const imapReader = require("imap-reader"); | ||
<details> | ||
@@ -113,5 +121,138 @@ <summary>Sample Successful Response</summary> | ||
### Search for a link in incoming mails and get the link | ||
```js | ||
const imapReader = require("imap-reader"); | ||
imapReader.waitForLink({ | ||
searchLinkString: 'https://mandrillapp.com/track/click/', | ||
imap: { | ||
user: 'alawocolynsm@hotmail.com', | ||
password: 'LKhHSrA80', | ||
host: 'imap-mail.outlook.com', | ||
port: 993, | ||
tls: true | ||
}, | ||
timeOut: 0 | ||
}) | ||
.then(resp => { | ||
console.log(resp) | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}) | ||
``` | ||
**searchLinkString:** It scans all the links in the incoming mail html data and if the string you send with this variable is in the link, it returns that link to you in an array. | ||
**timeOut: (Optional)** Specifies how many milliseconds to wait for a link containing a string you send. If not sent, it is set to 2 minutes. 0 If sent, it waits until it arrives. 1 Second = 1000 Milliseconds | ||
<details> | ||
<summary>Sample Successful Response</summary> | ||
```js | ||
{ | ||
status: true, | ||
data: [ | ||
'https://mandrillapp.com/track/click/31165340/auth0.openai.com?p=eyjzijoilvh4vc1xt2nocmyyvkn1ckturuz4zffwv05jiiwidii6mswicci6intcinvcijozmte2ntm0mcxcinzcijoxlfwidxjsxci6xcjodhrwczpcxfwvxfxcl2f1dggwlm9wzw5has5jb21cxfwvdvxcxc9lbwfpbc12zxjpzmljyxrpb24_dglja2v0ptlobdvzrnzyowjzb0x1au5juktuqjfks0nmbfe3ahf3i1wilfwiawrcijpcimfhnjcwmmrlyjgxodrlmde4ndrlodnmogy1odjlnzuwxcisxcj1cmxfawrzxci6w1wimwm3otuymjnimmq0ymuwmjbmzdjhntbmmmm5yzqxzjewmthlndu0y1wixx0ifq' | ||
] | ||
} | ||
``` | ||
</details> | ||
<details> | ||
<summary>Sample Failed Response</summary> | ||
```js | ||
{ | ||
status: false, | ||
data: 'No mail with the link was received within the specified time' | ||
} | ||
``` | ||
</details> | ||
### Search for a code that meets the conditions you set in incoming mails | ||
```js | ||
const imapReader = require("imap-reader"); | ||
imapReader.waitForCode({ | ||
imap: { | ||
user: 'lassalnimishr@hotmail.com', | ||
password: '91XTVPu69', | ||
host: 'imap-mail.outlook.com', | ||
port: 993, | ||
tls: true | ||
}, | ||
timeOut: 0, | ||
codeType: 'number', | ||
codeLength: 9, | ||
querySelector: 'a' | ||
}) | ||
.then(resp => { | ||
console.log(resp) | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
}) | ||
``` | ||
**timeOut: (Optional)** Specifies how many milliseconds to wait for a link containing a string you send. If not sent, it is set to 2 minutes. 0 If sent, it waits until it arrives. 1 Second = 1000 Milliseconds | ||
**codeType: (Optional)** Searches only for codes of the type you provide. Species it can take: number, lowerLetter, upperLetter, allLetter, nonAlfanumeric | ||
**codeLength: (Optional)** Filters strings with the number of characters you give from all html elements. | ||
**querySelector: (Optional)** It allows you to limit your html content and filter the codes within the html element you select. For example, if it is sent as 'a', it scans the content of the first a tag in the html content. | ||
<details> | ||
<summary>Sample Successful Response</summary> | ||
```js | ||
{ | ||
status: true, | ||
data: ['542569865'] | ||
} | ||
``` | ||
</details> | ||
<details> | ||
<summary>Sample Failed Response</summary> | ||
```js | ||
{ | ||
status: false, | ||
data: 'No mail with the code was received within the specified time' | ||
} | ||
``` | ||
</details> | ||
# Contact | ||
- By email only. zfcsoftware@gmail.com | ||
# License | ||
- ⚖️ Its protected by Creative Commons ([CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/)) | ||
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
18605
181.85%243
192.77%256
122.61%1
-50%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added