I'm using imap-simple
lib to get the emails from my inbox, I had no difficulty retrieving the data, but the body of the email is coming with formatting headers and a code. As I do not know much about email, I wanted to know how I can remove and pick up only the content of the email.
I'm trying with the following code:
const imaps = require('imap-simple')
const quotedPrintable = require('quoted-printable')
const port = 993
const host = 'imap-mail.outlook.com'
module.exports = async ({ user, pass }) => {
console.log({ user, pass })
const config = {
imap: { host, port, user, password: pass, tls: true, authTimeout: 10000 }
}
const connection = await imaps.connect(config)
await connection.openBox('INBOX')
const headerField = 'HEADER'
const emails = await connection.search(['UNSEEN'], { bodies: [headerField, 'TEXT'], markSeen: false, struct: true })
await connection.end()
return emails.map(email => {
const { attributes } = email
const response = { header: null, flags: attributes.flags, date: attributes.date, uid: attributes.uid, body: null }
email.parts.forEach(part => {
switch (part.which) {
case headerField:
response.header = part.body
break
case 'TEXT':
response.body = unescape(quotedPrintable.decode(part.body))
break
}
})
return response
})
}
The body
of the email is a string as an example:
- 000000000000e429a60578e63118 \ r \ nContent-Type: text / plain; charset="UTF-8" \ r \ nContent-Transfer-Encoding: quoted-printable Justification: \ r \ nThe purpose of this product = C3 = A9 display a system to meet m = C3 = A9dicos \ r \ nveter = C3 = Insemin = C3 = A7 = C3 = Artificial Axis of = \ r \ nequines. The system = C3 = A9 used to record the information = C3 = A7 = C3 = B5es on the in = \ r \ nsemina = C3 = A7 = C3 = A3o available in cellular or on a computer, since the data are = \ r \ nr = C3 = A3o \ r \ nregistered from organized form where m = C3 = A9dico can j = C3 = A1 inform a = \ r \ no client \ r \ n values and information = C3 = A7 = C3 = B5 are important about: ovule = C3 = A7 = C3 = A3o, p = \ r \ nalpa = C3 = A7 = C3 = A3o, measures of \ r \ nfol = C3 = ADculo, among others. The program was based on a search done directly with a C3 = Area professional (M = C3 = Veterinary A9dico = C3 = \ r \ n = A1rio), where \ r \ n even uses spreadsheets and sheets manually to record and collect \ r \ ninformation = C3 = A7 = C3 = B5es. Over time, these C3 = A7 = C3 = B5es v = C3 = A3o if = \ r \ n losing or exiting \ r \ n control. Due to these reasons, the idea of of \ r \ in a system to automate and organize these processes. \ r \ n \ r \ n - 000000000000e429a60578e63118 \ r \ nContent-Type: text / html; charset="UTF-8" \ r \ nContent-Transfer-Encoding: quoted-printable \ r \ nJustif = \ r \ nicative: O objective of this product = C3 = A9 display a system to meet m = C3 = A9 = veterinarians = C3 = Actors working in the field of Insemina = C3 = A7 = C3 = A3o Artifici = equals equals. The system = C3 = A9 used to record the information = C3 = A7 = \ r \ n = C3 = B5es on the insemin = C3 = A7 = C3 = Artificial A3 = Available in the cell = \ r \ nr or in a computer, since the data will be recorded = C3 = A3o where the m = C3 = A9dico can j = C3 = A1 inform the customer the values and info = \ r \ nrma = C3 = A7 = C3 = B5 are important about: ovula = C3 = A7 = C3 = A3o, palpa = C3 = A7 = C3 = A3o = measures of fol = C3 = ADculo, among others. The program was based on = \ r \ n a search made directly with a professional = C3 = A1rea (M = C3 = A9di = \ r \ nco Veterin = C3 = A1rio), where it uses form sheets and sheets manu = \ r \ nal to record and collect information = C3 = A7 = C3 = B5es. As time, these infor = \ r \ nma = C3 = A7 = C3 = B5es v = C3 = A3o getting lost or leaving out of control. Due to these reasons, the idea of the development of a system for automating and organizing these processes. \ r \ n \ r \ n - 000000000000e429a60578e63118 -
Note that the content of the email is there, but I have some additional headers, such as Content-Type: text/plain
and email code 000000000000e429a60578e63118
. Could someone give a help and give an idea of how to parse this string? I tried manual, hacking with split
, but depending on the email provider that formatting may change, for example, icloud emails do not have the HTML version and some additional headers.