rickypc / robotframework-imaplibrary

IMAP email testing library for Robot Framework
https://pypi.python.org/pypi/robotframework-imaplibrary
Apache License 2.0
21 stars 35 forks source link

LookupError: 'quoted-printable' is not a text encoding; use codecs.decode() to handle arbitrary codecs #17

Open kostiantynvoiku opened 6 years ago

kostiantynvoiku commented 6 years ago

Hello there,

I'm getting this LookupError when I try to use Get Links From Email keyword:

Open Mailbox  server=imap.googlemail.com  user=user@starofservice.com  password=pass ${proWelcomeMail} =    Wait for Email  recipient=${USER_EMAIL}  subject=Welcome
Open Link From Email  ${proWelcomeMail}
...
Close Mailbox

Output:

2018-06-14 15-43-29

Is there a workaround for this please?

Ph38240 commented 5 years ago

Hy,

To avoid the error, i've just modify the Library in the src/ImpaLibrary/_init_.py file In the import section, i've add

import quopri

And I've modify the get_email_body method

    def get_email_body(self, email_index):
        """Returns the decoded email body on multipart email message,
        otherwise returns the body text.

        Arguments:
        - ``email_index``: An email index to identity the email message.

        Examples:
        | Get Email Body | INDEX |
        """
        if self._is_walking_multipart(email_index):
            print('multipart')
            body = self.get_multipart_payload(decode=True)
        else:
            **raw_body = quopri.decodestring(self._imap.uid('fetch',
                                  email_index,'(BODY[TEXT])')[1][0][1]).decode('utf-8')
            body = message_from_string(raw_body)**
        return body

I'm not sure that .decode('utf-8') work in all the case !

For my own test, I've add a new function: get_email_header

    def get_email_header(self, email_index):
        """Returns the decoded email header of the email message in a dictionary

        Arguments:
        - ``email_index``: An email index to identity the email message.

        Examples:
        | Get Email Header | INDEX |
        """
        # decode('quoted-printable')
        raw_header = quopri.decodestring(self._imap.uid('fetch',email_index,
                              '(BODY[HEADER])')[1][0][1]).decode('utf-8')
        header = message_from_string(raw_header)
        result = {}
        result['From'] = header['From']
        result['To'] = header['To']
        result['Date'] = header['Date']
        result['Subject'] = header['Subject']
        result['Content-Type'] = header['Content-Type']
        result['Content-Language'] = header['Content-Language']
        result['Return-Path'] = header['Return-Path']
        return result

Perhaps this solution might be pulled in the git ..

jasonbarr commented 5 years ago

Getting the same issue, will give this workaround a try. Looks like it's a common problem...

tigsuway commented 5 years ago

Any working work-around or fix?