gmail
gmail copied to clipboard
AttributeError: 'int' object has no attribute 'add_label' when trying to add label
Problem occurs when I get multiple emails and then loop through them and try to add the a label to each email.
example:
mails = g.inbox().mail(sender=sender, prefetch=True)
for m in mails:
m.add_label("Label")
This returns: AttributeError: 'int' object has no attribute 'add_label'
Did you login before? Can you paste more complete code? What is happening there is that "m" is an integer instead of a Message object. Integers don't have add_label method. Make sure you are logged in before doing anything else.
try adding m.fetch() in the loop before add_label is called. Perhaps just the prefetch has failed.
I have logged in before the loop of course. The problem occurs when I do some other things in for loop before I add the label to the emails. For example I'm pulling some data from the headers and some text search in the body of the email. When that is done I want to add the label and remove it from inbox but got the before mentioned error.
I resolved it in ugly way by adding a new loop under the first one with only add_label in it. I know, ugly but it's the only thing that worked.
I will try with m.fetch method and see if it will work.
Thanks!