Question

I want to write a python module that sends data to a draft message in a G-mail account. I have written a script about two weeks ago that worked perfectly using imaplib. A simplified example of my module is below. (I have created a test email address for anyone to test this script on.)

import imaplib
import time
conn = imaplib.IMAP4_SSL('imap.gmail.com', port = 993)
conn.login('testpythoncreatedraft@gmail.com', '123456aaa')
conn.select('[Gmail]/Drafts')
conn.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), "TEST")

It utilized the .append function, but today when I run the module and it produces the following error:

Traceback (most recent call last):
  File "C:/Windows/System32/email_append_test.py", line 6, in <module>
    conn.append("[Gmail]/Drafts", '', imaplib.Time2Internaldate(time.time()), "TEST")
  File "C:\Python26\lib\imaplib.py", line 317, in append
    return self._simple_command(name, mailbox, flags, date_time)
  File "C:\Python26\lib\imaplib.py", line 1060, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Python26\lib\imaplib.py", line 895, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: APPEND command error: BAD ['Invalid Command']

As I said before, this module worked before. It successfully created draft messages with the string "Test" in its body. Since this script used to work, it seems more likely that it has something to do with a change Google made to the G-mail accounts IMAP features, but the error seems to indicate an error in the APPEND command. I have tested the python script on two different computer to see if my library file was corrupt, but the same error remained.

Also, I am using Python 2.6. Any help is appreciated.

Was it helpful?

Solution

Before the conn.append, add the following:

import email

Then change the conn.append line to read:

conn.append("[Gmail]/Drafts",
            '',
            imaplib.Time2Internaldate(time.time()),
            str(email.message_from_string('TEST')))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top