Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Draft message in Gmail using the imaplib in Python

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('[email protected]', '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.

like image 556
Mink Avatar asked Sep 22 '11 17:09

Mink


People also ask

How do I draft my emails?

Open a draft message you never sent In the folder pane, click the Drafts folder, then double-click the message. If you want to delete a draft, right-click a message in the draft folder and select Delete.


1 Answers

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')))
like image 87
kkurian Avatar answered Oct 14 '22 00:10

kkurian