Coder Social home page Coder Social logo

imbox's Introduction

Imbox - Python IMAP for Humans

workflow

Python library for reading IMAP mailboxes and converting email content to machine readable data

Requirements

Python (3.6, 3.7, 3.8, 3.9)

Installation

pip install imbox

Usage

from imbox import Imbox

# SSL Context docs https://docs.python.org/3/library/ssl.html#ssl.create_default_context

with Imbox('imap.gmail.com',
        username='username',
        password='password',
        ssl=True,
        ssl_context=None,
        starttls=False) as imbox:

    # Get all folders
    status, folders_with_additional_info = imbox.folders()

    # Gets all messages from the inbox
    all_inbox_messages = imbox.messages()

    # Unread messages
    unread_inbox_messages = imbox.messages(unread=True)

    # Flagged messages
    inbox_flagged_messages = imbox.messages(flagged=True)

    # Un-flagged messages
    inbox_unflagged_messages = imbox.messages(unflagged=True)

    # Flagged messages
    flagged_messages = imbox.messages(flagged=True)

    # Un-flagged messages
    unflagged_messages = imbox.messages(unflagged=True)

    # Messages sent FROM
    inbox_messages_from = imbox.messages(sent_from='[email protected]')

    # Messages sent TO
    inbox_messages_to = imbox.messages(sent_to='[email protected]')

    # Messages received before specific date
    inbox_messages_received_before = imbox.messages(date__lt=datetime.date(2018, 7, 31))

    # Messages received after specific date
    inbox_messages_received_after = imbox.messages(date__gt=datetime.date(2018, 7, 30))

    # Messages received on a specific date
    inbox_messages_received_on_date = imbox.messages(date__on=datetime.date(2018, 7, 30))

    # Messages whose subjects contain a string
    inbox_messages_subject_christmas = imbox.messages(subject='Christmas')

    # Messages whose UID is greater than 1050
    inbox_messages_uids_greater_than_1050 = imbox.messages(uid__range='1050:*')

    # Messages from a specific folder
    messages_in_folder_social = imbox.messages(folder='Social')

    # Some of Gmail's IMAP Extensions are supported (label and raw):
    all_messages_with_an_attachment_from_martin = imbox.messages(folder='all', raw='from:[email protected] has:attachment')
    all_messages_labeled_finance = imbox.messages(folder='all', label='finance')

    for uid, message in all_inbox_messages:
    # Every message is an object with the following keys

        message.sent_from
        message.sent_to
        message.subject
        message.headers
        message.message_id
        message.date
        message.body.plain

imbox's People

Contributors

am0z avatar amuzhou avatar anderseta avatar at0myks avatar bernhard-herzog avatar daassh avatar dveselov avatar erwinjunge avatar fpischedda avatar graingerkid avatar jdanbrown avatar jdowner avatar manikantanr avatar martinrusev avatar mmulqueen avatar nirdrabkin avatar oleglite avatar ookami-kb avatar py-radicz avatar rbas avatar sangkaka avatar sblondon avatar shanzi avatar shoonoise avatar skulltech avatar snowwm avatar sylvainde avatar synchroack avatar tveronesi avatar zevaverbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imbox's Issues

LookupError on most emails.

I am getting this error on every email:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:/Users/muddi/Google Drive/sheet python project/tracking.py", line 45, in <module>
    for uid, msg in uRead:
  File "c:\Users\muddi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\imbox\__init__.py", line 57, in fetch_list
    yield (uid, self.fetch_by_uid(uid))
  File "c:\Users\muddi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\imbox\__init__.py", line 48, in fetch_by_uid
    email_object = parse_email(raw_email, policy=self.parser_policy)
  File "c:\Users\muddi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\imbox\parser.py", line 159, in parse_email
    content = decode_content(part)
  File "c:\Users\muddi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\imbox\parser.py", line 122, in decode_content
    return content.decode(charset, 'ignore')
LookupError: unknown encoding: cp-850

uRead is a unread Imbox fetch_list. I am trying to iterate through it, but keep getting this error

Documentation

Is any one working on documentation.. or can I volunteer as project is small and am familiar with sphinx etc ..

message-id shouldnt have a hyphen

The documentation suggests using "messages.message-id" but the hyphen throws an error because Python interoperates it as "message" minus "id".

error: command SEARCH illegal in state AUTH, only allowed in states SELECTED

error Traceback (most recent call last)
in ()
----> 1 mails.next()

/usr/local/lib/python2.7/dist-packages/imbox/init.pyc in fetch_list(self, **kwargs)
32
33 def fetch_list(self, **kwargs):
---> 34 uid_list = self.query_uids(**kwargs)
35
36 for uid in uid_list:

/usr/local/lib/python2.7/dist-packages/imbox/init.pyc in query_uids(self, **kwargs)
20 query = build_search_query(**kwargs)
21
---> 22 message, data = self.connection.uid('search', None, query)
23 return data[0].split()
24

/usr/lib/python2.7/imaplib.pyc in uid(self, command, *args)
756 "only allowed in states %s" %
757 (command, self.state,
--> 758 ', '.join(Commands[command])))
759 name = 'UID'
760 typ, dat = self._simple_command(name, command, *args)

error: command SEARCH illegal in state AUTH, only allowed in states SELECTED

SSL/TLS certificates are not validated!

Hi Folks,

from my Impression current imbox does not validate SSL/TLS certificates.

https://github.com/martinrusev/imbox/blob/master/imbox/imap.py#L21

calls IMAP4_SSL default constructor which looks like:

imaplib.IMAP4_SSL(host='', port=IMAP4_SSL_PORT, keyfile=None, certfile=None, ssl_context=None)

As you can see at ssl_context=None. This default configuration does not validate SSL/TLS certificates!

Using the default configuration makes software using Imbox vulnerable!

Idea to solve this problem (untested sketch code!)

import ssl as pythonssllib

       if ssl:
            context = pythonssllib.create_default_context()
            if not self.port:
                self.port = 993
            self.server = self.IMAP4_SSL(self.hostname, self.port, ssl_context=context)
        else:
            if not self.port:
                self.port = 143
            self.server = self.IMAP4(self.hostname, self.port)     

Issuetrace: certtools/intelmq#532

Should mailbox leverage pyzmail for parsing?

pyzmail does a great job parsing emails (as well as composing and sending them). It has clearer encoding support, simpler interfaces, and handles message/rfc822 objects as attachments (instead of recursively parsing the parts of the attached message by default) better than the standard libraries email module.

You might be interested in using it. I'd be willing to help.

Is imbox Python 3-only now?

Hello,

I'm using imbox 0.6 with Python 2.7 and I have just spotted that imbox 0.9 specifies requirements as 'Python (3.3, 3.4, 3.5, 3.6)'.

Does that mean that imbox is Python 3-only now? If so, can you tell me what the latest version to support Python 2.7 was?

Thanks.

got an unexpected keyword argument 'ssl_context'

Please note that:
ssl_context = pythonssllib.create_default_context()
is new in python version 2.7.9.

after installing 2.7.11, i get this error:

>>> imbox = Imbox('imap.gmail.com', username='[email protected]', password='...')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/naruto/Programming/imap_test/venv/lib/python2.7/site-packages/imbox/__init__.py", line 15, in __init__
    ssl_context=None)
  File "/home/naruto/Programming/imap_test/venv/lib/python2.7/site-packages/imbox/imap.py", line 28, in __init__
    self.server = self.transport(self.hostname, self.port, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'ssl_context'

looks like ssl_context argument is not supported in 2.7

I want forward a mail If particular string is not available in body. How can I do this?

I want forward a mail If particular string is not available in body. How can I do this?

not_body = kwargs.get('not_body', False)
if not_body:
query.append('(NOT_BODY "%s")' % not_body)

not_body and unbody is not working
body works, means it filters the emails which have string in the body. I required in a opposite way, can we do this using not_body or unbody?

datetime in filters

date__gt and date__lt should accept datetime.datetime or datetime.date objects

message.body.html

Hi.
This is a best python lib for work with imap mail.
But when i try parse message and get html body i have error
AttributeError: 'dict' object has no attribute 'html'
So, try list dict keys
for uid, message in messages_from:
print message.keys()
[u'body', u'attachments', u'parsed_date', u'cc', u'message_id', u'raw_email', u'bcc', u'headers', u'date', u'sent_from', u'sent_to', u'subject']
Where is body.html ?

UTF-8/UnicecodeDecodeError

Name: imbox
Version: 0.8.5

Seeing decoding exceptions on certain messages

'utf-8' codec can't decode byte 0xa9 in position 21957: invalid start byte" type="<class 'UnicodeDecodeError'>
File "/usr/lib64/python3.4/site-packages/imbox/init.py", line 50, in fetch_list
yield (uid, self.fetch_by_uid(uid))
File "/usr/lib64/python3.4/site-packages/imbox/init.py", line 41, in fetch_by_uid
email_object = parse_email(raw_email)
File "/usr/lib64/python3.4/site-packages/imbox/parser.py", line 126, in parse_email
raw_email = str_encode(raw_email, 'utf-8')
File "/usr/lib64/python3.4/site-packages/imbox/utils.py", line 10, in str_encode
return str(value, encoding, errors)""

Address family not supported by protocol

Hi

I try imbox on our linux server, but got the following error

OSError: [Errno 97] Address family not supported by protocol

while, the same script works fine on windows

Change messages. How?

Hello.
How i can change dome mail rows before use copy or move methods?
Example, i want move message in 'UNSORTED' folder, but with "Moved by <filtern_ame>" in mail subject?
Thanks.

Display an explicit method to run the tests

When I write a patch to imbox library, I run the tests with tox and a tox.ini file containing:

[tox]
envlist = py34,py35 # should add others versions
[testenv]
deps=nose
commands=nosetests

Perhaps you have a better way to run the tests?
I think it would be good to document it in a paragraph at the end of the README file (and including the tox file or whatever file required).

I can do a PR if you want.

Python 2.7 encoding Error

I'm trying to print the keys()
Traceback (most recent call last):
File "./WatchMe-Mail", line 14, in
for uid, message in unread_messages:
File "/usr/local/lib/python2.7/dist-packages/imbox/init.py", line 48, in fetch_list
yield (uid, self.fetch_by_uid(uid))
File "/usr/local/lib/python2.7/dist-packages/imbox/init.py", line 39, in fetch_by_uid
email_object = parse_email(raw_email)
File "/usr/local/lib/python2.7/dist-packages/imbox/parser.py", line 127, in parse_email
email_message = email.message_from_string(raw_email)
File "/usr/lib/python2.7/email/init.py", line 57, in message_from_string
return Parser(_args, *_kws).parsestr(s)
File "/usr/lib/python2.7/email/parser.py", line 82, in parsestr
return self.parse(StringIO(text), headersonly=headersonly)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 3997: ordinal not in range(128)

Errors

Getting the follow...

 Traceback (most recent call last):
  File "emailparser.py", line 25, in <module>
    for uid, message in messages_from:
  File "/home/graingerkid/blinky/venv/local/lib/python2.7/site-packages/imbox/__init__.py", line 39, in     fetch_list
    yield (uid, self.fetch_by_uid(uid))
  File "/home/graingerkid/blinky/venv/local/lib/python2.7/site-packages/imbox/__init__.py", line 31, in fetch_by_uid
    email_object = parse_email(raw_email)
  File "/home/graingerkid/blinky/venv/local/lib/python2.7/site-packages/imbox/parser.py", line 144, in parse_email
    attachment = parse_attachment(part)
  File "/home/graingerkid/blinky/venv/local/lib/python2.7/site-packages/imbox/parser.py", line 89, in parse_attachment
    'size': len(file_data),
TypeError: object of type 'NoneType' has no len()

It looks to be due to the attachments but I wanted to check before trying to fix in case it was something you were aware of or it's something weird I'm doing.

How can I write * in messages_folder = imbox.messages(folder='*')

messages_folder = imbox.messages(folder='Social')
output:

('OK', [b'() "/" "INBOX"', b'(\Drafts) "/" "&g0l6P3ux-"', b'(\Sent) "/" "&XfJT0ZAB-"', b'(\Trash) "/" "&XfJSIJZk-"', b'(\Junk) "/" "&V4NXPpCuTvY-"', b'() "/" "&dcVr0mWHTvZZOQ-"', b'() "/" "xiaoshuo"', b'() "/" "moive"', b'() "/" "others"', b'() "/" "Sent"'])

The question is How can I write * in messages_folder = imbox.messages(folder='*') to select folder

Support for OAuth2

I can implement it if you want. Do you think it should take just the access token or handle the entire authorization flow? (generating authorization url, etc)
I was working on this, so it would be simple to add it in.

Lazy data download

The current implementation downloads the full content of every message that matches the search query and that can take a lot of bandwidth and resources for mail boxes with a lot of messages

error when parsing attachment

I am running the example provided by README, My python version is 3.5, on windows platform, here is the error message, please take a look.

Traceback (most recent call last):
  File "C:\Users\**\Miniconda3\lib\base64.py", line 518, in _input_type_check
    m = memoryview(s)
TypeError: memoryview: a bytes-like object is required, not 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "D:/Project/account-adapter/imbox_downloader.py", line 61, in <module>
    for uid, message in all_messages:
  File "C:\Users\**\Miniconda3\lib\site-packages\imbox\__init__.py", line 50, in fetch_list
    yield (uid, self.fetch_by_uid(uid))
  File "C:\Users\**\Miniconda3\lib\site-packages\imbox\__init__.py", line 41, in fetch_by_uid
    email_object = parse_email(raw_email)
  File "C:\Users\**\Miniconda3\lib\site-packages\imbox\parser.py", line 160, in parse_email
    attachment = parse_attachment(part)
  File "C:\Users\**\Miniconda3\lib\site-packages\imbox\parser.py", line 102, in parse_attachment
    name, value = decode_param(param)
  File "C:\Users\**\Miniconda3\lib\site-packages\imbox\parser.py", line 74, in decode_param
    value = base64.decodestring(code)
  File "C:\Users\**\Miniconda3\lib\base64.py", line 560, in decodestring
    return decodebytes(s)
  File "C:\Users\**\Miniconda3\lib\base64.py", line 552, in decodebytes
    _input_type_check(s)
  File "C:\Users\**\Miniconda3\lib\base64.py", line 521, in _input_type_check
    raise TypeError(msg) from err
TypeError: expected bytes-like object, not str

UnicodeDecodeError not catched in message listing

Hi,

I am using imbox to list messages from a folder over IMAP. When calling

        for uid, message in server.messages(date__gt=since_date,
                                            folder=imap_folder):

I get a UnicodeDecodeError which is not catched:

  File "/home/phyks/Code/email-assistant/.env/lib/python3.6/site-packages/imbox/parser.py", line 168, in parse_email
    attachment = parse_attachment(part)
  File "/home/phyks/Code/email-assistant/.env/lib/python3.6/site-packages/imbox/parser.py", line 105, in parse_attachment
    name, value = decode_param(param)
  File "/home/phyks/Code/email-assistant/.env/lib/python3.6/site-packages/imbox/parser.py", line 73, in decode_param
    value = str_encode(value, encoding)
  File "/home/phyks/Code/email-assistant/.env/lib/python3.6/site-packages/imbox/utils.py", line 6, in str_encode
    return str(value, encoding, errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 49: invalid start byte

I think the relevant portion of the email is

Content-Type: image/png;
 filename="=?UTF-8?B?Q2FwdHVyZSBk4oCZZcyBY3JhbiAyMDE2LTA5LTEzIGHMgCAxNS40NS4yOS5w?= =?UTF-8?B?bmc=?="
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="=?UTF-8?B?Q2FwdHVyZSBk4oCZZcyBY3JhbiAyMDE2LTA5LTEzIGHMgCAxNS40NS4yOS5w?= =?UTF-8?B?bmc=?="

Not sure what is happening here, but it seems I have no way of ignoring this exception since it should be catched from within imbox. :/

Thanks!

P.S.: I have the full email available and I could give extra parts if the one above is not enough to reproduce. I'd rather not post it completely on a public issue tracker though.

Encoding error with Python 3.4

I have the next error fetching emails with latin characters:

Traceback (most recent call last):
  File "imboxsample.py", line 34, in <module>
    for uid, message in all_messages:
  File "C:\Documents and Settings\Usuario UTP\Envs\nucleus\lib\site-packages\imbox\__init__.py", line 48, in fetch_list
    yield (uid, self.fetch_by_uid(uid))
  File "C:\Documents and Settings\Usuario UTP\Envs\nucleus\lib\site-packages\imbox\__init__.py", line 39, in fetch_by_uid
    email_object = parse_email(raw_email)
  File "C:\Documents and Settings\Usuario UTP\Envs\nucleus\lib\site-packages\imbox\parser.py", line 182, in parse_email
    parsed_email[valid_key_name] = decode_mail_header(value)
  File "C:\Documents and Settings\Usuario UTP\Envs\nucleus\lib\site-packages\imbox\parser.py", line 38, in decode_mail_header
    logger.debug("Mail header no. {}: {} encoding {}".format(index, str_decode(text, 'utf-8'), charset))
  File "C:\Documents and Settings\Usuario UTP\Envs\nucleus\lib\site-packages\imbox\utils.py", line 13, in str_decode
    return bytes(value, encoding, errors).decode('utf-8')
TypeError: encoding or errors without a string argument

I change the method str_decode by:

    def str_decode(value='', encoding=None, errors='strict'):
        try:
            return bytes(value, encoding, errors).decode('utf-8')
        except Exception as e:
            value.decode("utf-8")

Error while decoding Polish text

Hi,
I'm connecting to GMail account on Python 3 and getting UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 5: invalid start byte while trying to get messages via for uid, message in inbox.messages(unread=True):.

The value at hand is b'Zosta\xb3 w\xb3\xb1czony dost\xeap dla mniej bezpiecznych aplikacji'. and from what I can tell is encoded in ISO 8859-2 instead of UTF-8.

Don't really know what to do here and what should be done in the code. Is there a way to make GMail 'give' us mails encoded with UTF-8? Should we maybe try a couple of encodings within str_decode?
I can do a PR after deciding what to do here.

Cheers,
Paweł

Strange error on parsing

I don't know is it imbox error or bad server response, but it gives such stack trace:

m = Imbox('imap.mail.ru', 'login', 'password')
for i in m.messages(sent_to='[email protected]'):
    print i

***/venv/bin/python ***/io/mail.py
Traceback (most recent call last):
  File "***/io/mail.py", line 12, in <module>
    mm()
  File "***/io/mail.py", line 10, in mm
    for i in m.messages(sent_to='[email protected]'):
  File "***/venv/lib/python2.7/site-packages/imbox/__init__.py", line 36, in fetch_list
    yield (uid, self.fetch_by_uid(uid))
  File "***/venv/lib/python2.7/site-packages/imbox/__init__.py", line 25, in fetch_by_uid
    message, data = self.connection.uid('fetch', uid, '(BODY.PEEK[])')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 760, in uid
    typ, dat = self._simple_command(name, command, *args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/imaplib.py", line 905, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: UID command error: BAD ['[PARSE] Range parse error']

but if

from imbox.parser import parse_email
import imaplib
i = imaplib.IMAP4_SSL('imap.mail.ru')
i.login('login', 'password')
i.select('INBOX')
r, d = i.search(None)
for m in d[0].split():
    parse_email(i.fetch(m, '(BODY.PEEK[])')[1][0][1])

everything is fine.

Auth failure: Google prevents sign-in attempt

I disabled two-factor authentication, but that wasn't sufficient to authenticate the IMAP connection. Here's the email I received from Google:

Hi Kevin,
Someone just tried to sign in to your Google Account [REDACTED] from an app that doesn't meet modern security standards.

We strongly recommend that you use a secure app, like Gmail, to access your account. All apps made by Google meet these security standards. Using a less secure app, on the other hand, could leave your account vulnerable. Learn more.

[...]

Best,
The Google Accounts team

There doesn't seem to be a way to grant access – the failure appears to be terminal. As a temporary workaround, is it possible to bypass the authentication step entirely and use imbox on a local .mbox file?

Anyone managed to get the date filtering to work?

I get the following...

  File "emailparser.py", line 26, in <module>
    for uid, message in messages_from:
  File "/home/graingerkid/blinky/venv/local/lib/python2.7/site-packages/imbox/__init__.py", line 43, in fetch_list
    uid_list = self.query_uids(**kwargs)
  File "/home/graingerkid/blinky/venv/local/lib/python2.7/site-packages/imbox/__init__.py", line 28, in query_uids
    message, data = self.connection.uid('search', None, query)
  File "/usr/lib/python2.7/imaplib.py", line 772, in uid
    typ, dat = self._simple_command(name, command, *args)
  File "/usr/lib/python2.7/imaplib.py", line 1087, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 917, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
error: UID command error: BAD ['Command Argument Error. 11']

Push new release to pypi

Any chance you could push a new release to pypi? It would be helpful to have some of the new features. Specifically cc/bcc parsing.

Thanks!

Problems decoding content

Traceback (most recent call last): File "/root/PycharmProjects/Teste/main.py", line 26, in <module> next_message = next(all_messages) File "/usr/local/lib/python3.5/dist-packages/imbox/__init__.py", line 50, in fetch_list yield (uid, self.fetch_by_uid(uid)) File "/usr/local/lib/python3.5/dist-packages/imbox/__init__.py", line 41, in fetch_by_uid email_object = parse_email(raw_email) File "/usr/local/lib/python3.5/dist-packages/imbox/parser.py", line 151, in parse_email content = decode_content(part) File "/usr/local/lib/python3.5/dist-packages/imbox/parser.py", line 119, in decode_content return content.decode(charset) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa9 in position 906: invalid start byte

Some emails can have content encoded with Latin-1, Latin-15 or something else.

May add:

`
import chardet
...

def _decode_content(message):
targetEncoding = "utf-8"

content = message.get_payload(decode=True)
charset = message.get_content_charset('utf-8')
try:
    sourceEncoding = chardet.detect(content).get("encoding")
    content = str(content).decode(sourceEncoding).encode(targetEncoding)
    return content.decode(charset)
except AttributeError:
    return content

`

Disable SSL verification via passing the ssl_context

Hi,

Description:
It is impossible to disable SSL verification. When you try to pass ssl_context into the Imbox constructor its just ignore it.

How to reproduce:

imbox = Imbox('mail.server.com',
        username='[email protected]',
        password='password',
        ssl=True,
        ssl_context=ssl._create_unverified_context())

How to fix:
Pass ssl_context into ImapTransport constructor instead of None.
File: imbox/init.py:15

parse_attachment() error when a trailing ';' exists

Hello,

there is an exception when an e-mail with attachment is parsed in a specific case:
when the Content-Disposition line has a ';' at the end of the line.

Working case:
Content-Disposition: attachment; filename="abc.xyz"

Error case:

Content-Disposition: attachment; filename="abc.xyz";
                                                   ^ ooops

It's fixable by modifying the parse_attachment() implementation. I hesitate for 2 way to fix it:

  • using second element to dispositions only:
    for param in dispositions[1]:
  • checking if param is not falsy inside the for loop.

According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
Content-Disposition: form-data; name="fieldName"; filename="filename.jpg" is valid so I think the second solution is better.

What do you think about it ?
Ok for such a pull-request?

The trace in ipython:

<ipython-input-21-dcfccc808ef9> in <module>()
----> 1 spam = imbox.parser.parse_email(tspam)

~/src/vieux-emails/local.virtualenv/lib/python3.4/site-packages/imbox/parser.py in parse_email(raw_email, policy)
    163                 body['html'].append(content)
    164             elif content_disposition:
--> 165                 attachment = parse_attachment(part)
    166                 if attachment:
    167                     attachments.append(attachment)

~/src/vieux-emails/local.virtualenv/lib/python3.4/site-packages/imbox/parser.py in parse_attachment(message_part)
     99             for param in dispositions[1:]:
    100                 print("PARAM", param)
--> 101                 name, value = decode_param(param)
    102 
    103                 if 'file' in name:

~/src/vieux-emails/local.virtualenv/lib/python3.4/site-packages/imbox/parser.py in decode_param(param)
     60 
     61 def decode_param(param):
---> 62     name, v = param.split('=', 1)
     63     values = v.split('\n')
     64     value_results = []

ValueError: need more than 1 value to unpack

Exception raised by the logging if there is an encoding problem in sender name

In imbox/parser.py, the decoding of the name and e-mail address is done by decode_mail_header(). It's done 3 times, by imbox.utils.str_encode() function:

  • line 36: logger.debug() formats the name of the sender. errors parameter is not set at the call so it's in strict mode.
  • lines 38 and 42, in order to set headers[index]: Encoding problems are fixed by the replace value to third parameter of the str_decode() call ().

I have a case where it's a problem: the content of the name is not correct utf-8 string so I get an UnicodeDecodeError due to the logger and not the functional code of the library. If I set errors='replace' for the str_encode() call of the logger, it works properly.

In my opinion, setting errors parameter to replace is a good way to fix it. What do you think about it?
I can do a PR with my testcase for unit testing it.

Push notification support

Interesting! We really need something like requests for IMAP. Is support for Push notifications planned?

How does one tell if an email is read/unread?

I see the function to mark items as read... but how do you tell if a message is read or unread already? For example, say I'm going to list all the emails in the inbox, and I need to somehow flag read items with a different background color or something... A pretty basic requirement. However, I don't see this information in the output from messages() or any other fetch_* method. Am I missing something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.