Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using gmail inside a with #68

Open
ffunenga opened this issue Feb 17, 2016 · 0 comments
Open

Using gmail inside a with #68

ffunenga opened this issue Feb 17, 2016 · 0 comments

Comments

@ffunenga
Copy link

Something like this

class Gmail:

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def __enter__(self):
        # note: getpass might be better here, instead of keeping the pwd in memory
        self.g = gmail.login(self.username, self.password)
        return self.g

    def __exit__(self, *args, **kwds):
        try:
            print('logging out...')
            self.g.logout()
        except:
            raise

allows this

with Gmail(username, password) as conn:
    print('Reading all your emails from spam folder:')

    spam_box = conn.spam().mail(prefetch=True)
    n = conn.spam().count()

    for idx, email in enumerate(spam_box):
        print('fetching %d of %d...' % (idx + 1, n))

which would be nice to have implemented in the gmail package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant