[英]How to write a function that that generates the login for a mailbox with imap_tools?
我创建了一个登录邮箱的助手 function。
import imap_tools
def mailbox_login():
try:
with imap_tools.MailBoxUnencrypted(ENV["IMAP4_FQDN"]).login(
ENV["RECEIVING_EMAIL_USER"], ENV["RECEIVING_EMAIL_PASSWORD"]
) as mailbox:
print("Successfully logged into the mailbox.")
return mailbox
except imap_tools.MailboxLoginError as error:
print(f"CRITICAL: Failed to login to the mailbox: {error}")
另一个function需要邮箱连接。
def email_count():
"""
Get all emails from the mail server via IMAP
"""
msgs = []
mailbox = mailbox_login()
for msg in mailbox.fetch():
msgs.append(msg)
return msgs
当我运行email_count()
时,出现以下错误:
imaplib.IMAP4.error: command SEARCH illegal in state LOGOUT, only allowed in states SELECTED
我一离开with
语句的scope,它就退出邮箱了。 离开mailbox_login()
后有什么方法可以保持连接吗?
成功连接到 IMAP-Server 后需要 select 一个邮箱。 利用
mailbox.select()
连接后和搜索前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.