如何修复python脚本(电子邮件侦听器)

如何修复python脚本(电子邮件侦听器),python,gmail,imap,Python,Gmail,Imap,我有两个python脚本要在gmail上做侦听器,以获取传入的电子邮件,但当我运行它时,我得到了以下错误,欢迎任何帮助。 此外,如果anu的其他脚本将是welcom 已经谢谢了。 第一个脚本: import email_listener # Set your email, password, what folder you want to listen to, and where to save attachments email = "example@gmail.com"

我有两个python脚本要在gmail上做侦听器,以获取传入的电子邮件,但当我运行它时,我得到了以下错误,欢迎任何帮助。 此外,如果anu的其他脚本将是welcom 已经谢谢了。 第一个脚本:

import email_listener

# Set your email, password, what folder you want to listen to, and where to save attachments
email = "example@gmail.com"
app_password = "password"
folder = "Inbox"
attachment_dir = "/path/to/attachments"
el = email_listener.EmailListener(email, app_password, folder, attachment_dir)

# Log into the IMAP server
el.login()

# Get the emails currently unread in the inbox
messages = el.scrape()
print(messages)

# Start listening to the inbox and timeout after an hour
timeout = 60
el.listen(timeout)
输出错误:

C:\Users\PC Sony>"C:/Users/PC Sony/AppData/Local/Programs/Python/Python39/python.exe" "c:/Users/PC Sony/Desktop/elzero/elzero/import email_listener.PY"
Traceback (most recent call last):
  File "c:\Users\PC Sony\Desktop\elzero\elzero\import email_listener.PY", line 11, in <module>
    con.login()
  File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\email_listener\__init__.py", line 83, in login
    self.server = IMAPClient('imap.gmail.com')
  File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\imapclient\imapclient.py", line 254, in __init__
    self._imap = self._create_IMAP4()
  File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\imapclient\imapclient.py", line 288, in _create_IMAP4
    return tls.IMAP4_TLS(self.host, self.port, self.ssl_context,
  File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site-packages\imapclient\tls.py", line 44, in __init__
    imaplib.IMAP4.__init__(self, host, port)
  File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 202, in __init__
    self.open(host, port, timeout)
TypeError: open() takes 3 positional arguments but 4 were given
C:\Users\PC-Sony>“C:/Users/PC-Sony/AppData/Local/Programs/Python/Python39/Python.exe”“C:/Users/PC-Sony/Desktop/elzero/import-email\u-listener.PY”
回溯(最近一次呼叫最后一次):
文件“c:\Users\PC Sony\Desktop\elzero\elzero\import email\u listener.PY”,第11行,在
con.login()
文件“C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site packages\email\u listener\uuuuu init\uuuuuu.py”,第83行,登录
self.server=IMAPClient('imap.gmail.com')
文件“C:\Users\PC Sony\AppData\Local\Programs\Python\39\lib\site packages\imapclient\imapclient.py”,第254行,在uu init中__
self.\u imap=self.\u create\u IMAP4()
文件“C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site packages\imapclient\imapclient.py”,第288行,在_create\u IMAP4中
返回tls.IMAP4_tls(self.host、self.port、self.ssl_上下文、,
文件“C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\site packages\imapclient\tls.py”,第44行,在\uuu init中__
imaplib.IMAP4.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“C:\Users\PC-Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py”,第202行,在__
self.open(主机、端口、超时)
TypeError:open()接受3个位置参数,但给出了4个
第二个脚本: 从这里得到的

错误:

Traceback (most recent call last):
      File "c:\Users\PC Sony\Desktop\elzero\elzero\import imaplib.py", line 68, in <module>
        result, data = server.uid('fetch', uid, '(RFC822)')  # fetch entire message
      File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 890, in uid
        typ, dat = self._simple_command(name, command, *args)
      File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 1230, in _simple_command
        return self._command_complete(name, self._command(name, *args))
      File "C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py", line 988, in _command
        data = data + b' ' + arg
    TypeError: can't concat int to bytes
回溯(最近一次呼叫最后一次):
文件“c:\Users\PC Sony\Desktop\elzero\elzero\import imaplib.py”,第68行,在
结果,data=server.uid('fetch',uid'(RFC822)#获取整个消息
文件“C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py”,uid第890行
典型,dat=self.\u简单\u命令(名称,命令,*args)
文件“C:\Users\PC-Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py”,第1230行,在\u simple\u命令中
返回self.\u命令\u complete(名称,self.\u命令(名称,*args))
文件“C:\Users\PC Sony\AppData\Local\Programs\Python\Python39\lib\imaplib.py”,第988行,在_命令中
数据=数据+b''+参数
TypeError:无法将int转换为字节

email\u listener
适用于
python3.8
,但不适用于
python3.9

我发现
email\u listener
使用的模块
IMAPClient
与标准模块
imaplib
中的新
IMAP\u SSL.open()
存在问题

在第
IMAPClient
页上,您可以看到

官方支持Python版本2.7和3.4到3.7


因此,您必须使用
Python 3.8
并等待
IMAPClient
修复它。

我测试了第一个脚本,它在Linux Mint 20、Python 3.8上运行没有问题。可能这只是Windows上的问题,或者只有在Python 3.9I中,我使用
Python 3.9
测试了Python 3.9,它给出了错误。可能他们在
imaplib
中更改了一些内容d
email\u listener
有问题。我知道
Python 3.9
是一个非常新的版本,所以最好使用稍旧的
3.8
。你也可以将这个问题发送给
email\u listener
的作者,也许它会在下一个版本中修复。我切换到3.8它的工作很好,我认为只有最后一件事与这个错误有关解码,任何帮助或必须创建一个新的问题