简体   繁体   English

使用 Python 中的 imaplib 在 Gmail 中创建草稿邮件

[英]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.我想编写一个 python 模块,将数据发送到 G-mail 帐户中的草稿消息。 I have written a script about two weeks ago that worked perfectly using imaplib.大约两周前我写了一个脚本,它使用 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:它使用了 .append 函数,但是今天当我运行该模块时,它产生了以下错误:

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.它成功地创建了正文中带有字符串“Test”的草稿消息。 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.由于此脚本曾经可以工作,因此它似乎更可能与 Google 对 G-mail 帐户 IMAP 功能所做的更改有关,但该错误似乎表明 APPEND 命令中存在错误。 I have tested the python script on two different computer to see if my library file was corrupt, but the same error remained.我已经在两台不同的计算机上测试了 python 脚本,看看我的库文件是否已损坏,但仍然存在相同的错误。

Also, I am using Python 2.6.另外,我使用的是 Python 2.6。 Any help is appreciated.任何帮助表示赞赏。

Before the conn.append , add the following:conn.append之前,添加以下内容:

import email

Then change the conn.append line to read:然后将conn.append行更改为:

conn.append("[Gmail]/Drafts",
            '',
            imaplib.Time2Internaldate(time.time()),
            str(email.message_from_string('TEST')))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM