How best to encode or clean up the email body when collecting mails through Ruby Net::IMAP -
i collecting emails imap server code beneath, content of email body ugly, , impossible understand. many of emails contains danish , swedish special characters e.g. æ, ä, ö, ø , å, don't think problem. how best encode , clean up?
imap = net::imap.new(address, port, enable_ssl?) imap.login(user_name, password) imap.examine(flag) search_query = "#{last_uid}:*" imap.uid_search(search_query).each |uid| if uid.to_i > last_uid.to_i header = imap.uid_fetch(uid, "body[header.fields (from date subject)]")[0].attr["body[header.fields (from date subject)]"] = mail.read_from_string(header).from.first = mail.read_from_string(header).to.first rescue nil subject = mail.read_from_string(header).subject date = mail.read_from_string(header).date body = imap.uid_fetch(uid, "body[text]")[0].attr["body[text]"].gsub(/\r\n?/, "\n").force_encoding('utf-8') end end imap.logout() imap.disconnect()
sample body content:
1:
ls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls08yni+dqppcmry zwrhdg86idi4ltazltiwmtmgmtq6ndc6mtg8yni+dqppcmryzw51bw1lcjogmta5mdm1pgjy pg0kvhjhbnnha3rpb25zsuq6ide2nzgymq0kpgjypjxicj4nckzha3r1cmvyaw5nc2fkcmvz c2u6pgjypg0kls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0t ls0tls0tls0tls0tls0tls0tls0tls0tls0tltxicj48yni+dqpoawtsyxmgsnv1bcboawvs c2vupgjyic8+qs5qlibnw7hsbgvyietvbgxlz2lldcaxmdu8yniglz41nzawifn2zw5kym9y zzxiciavpkrlbm1hcms8yniglz5utey6oiaymdyzmdcznzxiciavpjxhighyzwy9im1hawx0 bzpuawtzqgxpdmuuzgsipm5pa3nabgl2zs5kazwvyt48yniglz4ncjxicj48yni+dqpmzxzl cmluz3nhzhjlc3nlojxicj4nci0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0t ls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls08yni+pgjypg0ktmlrbgfz iep1dwwgtmllbhnlbjxiciavpkeuuc4gtco4bgxlciblb2xszwdpzxqgmta1pgjyic8+ntcw mcbtdmvuzgjvcmc8yniglz5ezw5tyxjrpgjyic8+vexgojogmja2mza3mzc8yniglz48ysbo cmvmpsjtywlsdg86bmlrc0bsaxzllmrrij5uawtzqgxpdmuuzgs8l2e+pgjyic8+dqo8yni+ pgjypg0kt3jkcmvkyxrhojxicj4nci0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0t ls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls0tls08yni+dqonciagmsww mcbzdgsuifn0yxigv2fycybcyxr0bgvmcm9udcbjssbyym94icg0mtazmikgw6egretlidi2 myw5osatielhbhq6ierlsyazmjksotkncjxicj4ncjxicj4nckjldgfsaw5noiayoibeyw5z a2uga3jlzgl0a29ydcbbdhjhbnnha3rpb25zz2viexigmswynsvdiches0sgncwxmykncjxi cj4nckzvcnnlbmrlbhnloiagkerlsyawldawkq0kpgjypjxicj4nclnhbwxldcbwcmlzidog retlidmzncwxmg0kpgjypg0ksgvyywygbw9tczogretlidy2ldgzdqo=
2 (shortened):
------=_nextpart_000_0482_01ce2b9e.a689a9f0 content-type: multipart/related; boundary="----=_nextpart_001_0483_01ce2b9e.a689a9f0" ------=_nextpart_001_0483_01ce2b9e.a689a9f0 content-type: multipart/alternative; boundary="----=_nextpart_002_0484_01ce2b9e.a689a9f0" ------=_nextpart_002_0484_01ce2b9e.a689a9f0 content-type: text/plain; charset="iso-8859-1" content-transfer-encoding: quoted-printable =20 =09 =09 =09 =09 =20 =09 =09 =09 =09 =09 =09 =09 =09 =09 daily restock information. =09 =09 item format 1+=20 5+ =20 box price=20 qty barcode =09
3 (shortened):
--boundary-=_shccxhuuyyhtgdglfciebdutoeun content-type: text/plain; charset="iso-8859-1" --boundary-=_shccxhuuyyhtgdglfciebdutoeun content-type: application/octet-stream content-disposition: attachment; filename="systemstock.xlsx" content-transfer-encoding: base64 uesdbbqabgaiaaaaiqc5olcvkgeaaiwgaaatan0bw0nvbnrlbnrfvhlwzxndlnhtbcci2qeooaac aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaamrvywrdmbc9f/oprtcsk0mhlbinhy7hnpd0axrreovyktbmtr/v2floimti hejf+7xl9eyejlzfhq0hohe2eb24kykwqdpglhlxox3rpioisvmtcmchettamrre3gymow8ycbxf rgre/klktdmofmbog+u3cxckrxwbftkrdkkwipvd7onmnsww1kesqwwhlzbxq5yi1y0/3iuzgsui 5/13jvuilpe5srwxulm2+gdjx83njgxt0lxb0dh6aepjbkbfhvtgmdfmgiinozddwqebdkzdnfab
etc..
dug around many hours trying solve problem adding answer few of threads found...
https://stackoverflow.com/a/26604049/2386548
hope helps somebody...
Comments
Post a Comment