add email_login unit test

This commit is contained in:
Zhou Tuo 2024-02-07 07:59:54 +00:00
parent f31f371d53
commit 4a991b4cd4
3 changed files with 79 additions and 27 deletions

View file

@ -1,8 +1,29 @@
from imap_tools import MailBox
from metagpt.logs import logger
from metagpt.tools.tool_registry import register_tool
from metagpt.tools.tool_type import ToolType
# Define a dictionary mapping email domains to their IMAP server addresses
IMAP_SERVERS = {
"outlook.com": "imap-mail.outlook.com", # Outlook
"163.com": "imap.163.com", # 163 Mail
"qq.com": "imap.qq.com", # QQ Mail
"gmail.com": "imap.gmail.com", # Gmail
"yahoo.com": "imap.mail.yahoo.com", # Yahoo Mail
"icloud.com": "imap.mail.me.com", # iCloud Mail
"hotmail.com": "imap-mail.outlook.com", # Hotmail (同 Outlook)
"live.com": "imap-mail.outlook.com", # Live (同 Outlook)
"sina.com": "imap.sina.com", # Sina Mail
"sohu.com": "imap.sohu.com", # Sohu Mail
"yahoo.co.jp": "imap.mail.yahoo.co.jp", # Yahoo Mail Japan
"yandex.com": "imap.yandex.com", # Yandex Mail
"mail.ru": "imap.mail.ru", # Mail.ru
"aol.com": "imap.aol.com", # AOL Mail
"gmx.com": "imap.gmx.com", # GMX Mail
"zoho.com": "imap.zoho.com", # Zoho Mail
}
@register_tool(tool_type=ToolType.EMAIL_LOGIN.type_name)
def email_login_imap(email_address, email_password):
@ -17,34 +38,14 @@ def email_login_imap(email_address, email_password):
object: The imap_tools's MailBox object returned after successfully connecting to the mailbox through imap_tools, including various information about this account (email, etc.), or None if login fails.
"""
# Define a dictionary mapping email domains to their IMAP server addresses
imap_servers = {
"outlook.com": "imap-mail.outlook.com", # Outlook
"163.com": "imap.163.com", # 163 Mail
"qq.com": "imap.qq.com", # QQ Mail
"gmail.com": "imap.gmail.com", # Gmail
"yahoo.com": "imap.mail.yahoo.com", # Yahoo Mail
"icloud.com": "imap.mail.me.com", # iCloud Mail
"hotmail.com": "imap-mail.outlook.com", # Hotmail (同 Outlook)
"live.com": "imap-mail.outlook.com", # Live (同 Outlook)
"sina.com": "imap.sina.com", # Sina Mail
"sohu.com": "imap.sohu.com", # Sohu Mail
"yahoo.co.jp": "imap.mail.yahoo.co.jp", # Yahoo Mail Japan
"yandex.com": "imap.yandex.com", # Yandex Mail
"mail.ru": "imap.mail.ru", # Mail.ru
"aol.com": "imap.aol.com", # AOL Mail
"gmx.com": "imap.gmx.com", # GMX Mail
"zoho.com": "imap.zoho.com", # Zoho Mail
}
# Extract the domain from the email address
domain = email_address.split("@")[-1]
# Determine the correct IMAP server
imap_server = imap_servers.get(domain)
imap_server = IMAP_SERVERS.get(domain)
if not imap_server:
print(f"IMAP server for {domain} not found.")
logger.error(f"IMAP server for {domain} not found.")
return None
# Attempt to log in to the email account