[英]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.