test
This commit is contained in:
@@ -55,7 +55,7 @@ class MailStore:
|
||||
return False, "收件人列表为空"
|
||||
|
||||
# 打印收件人列表以进行调试
|
||||
logging.debug(f"收件人列表: {recipients_list}")
|
||||
logging.debug(f"收件人列表x2: {recipients_list}")
|
||||
|
||||
if message is None:
|
||||
logging.error("邮件内容无效,无法保存邮件")
|
||||
@@ -83,7 +83,7 @@ class MailStore:
|
||||
|
||||
if content_type == "text/plain":
|
||||
body_text = part.get_payload(decode=True).decode(part.get_content_charset() or 'utf-8', errors='replace')
|
||||
logging.debug(f"提取的纯文本内容长度: {len(body_text)}")
|
||||
logging.debug(f"提取的纯文本内容长度xb: {len(body_text)}")
|
||||
|
||||
elif content_type == "text/html":
|
||||
body_html = part.get_payload(decode=True).decode(part.get_content_charset() or 'utf-8', errors='replace')
|
||||
@@ -110,7 +110,7 @@ class MailStore:
|
||||
except Exception as e:
|
||||
logging.error(f"从原始数据提取内容失败: {str(e)}")
|
||||
|
||||
logging.info(f"提取完成: 纯文本={len(body_text)}字节, HTML={len(body_html)}字节, 附件数={len(attachments)}")
|
||||
logging.info(f"提取完成x3: 纯文本={len(body_text)}字节, HTML={len(body_html)}字节, 附件数={len(attachments)}")
|
||||
|
||||
# 保存到数据库
|
||||
session = self.db_session_factory()
|
||||
@@ -176,6 +176,9 @@ class MailStore:
|
||||
logging.error(error_msg)
|
||||
return False, error_msg
|
||||
|
||||
# 打印邮箱ID以进行调试
|
||||
logging.debug(f"使用的邮箱IDx3: {mailbox_id}")
|
||||
|
||||
# 创建邮件记录
|
||||
email_obj = Email(
|
||||
mailbox_id=mailbox_id, # 确保始终有邮箱ID
|
||||
@@ -192,38 +195,6 @@ class MailStore:
|
||||
session.add(email_obj)
|
||||
session.flush() # 获取新创建邮件的ID
|
||||
|
||||
# 保存附件
|
||||
for file_name, content in attachments:
|
||||
attachment = Attachment(
|
||||
email_id=email_obj.id,
|
||||
filename=file_name,
|
||||
content_type="application/octet-stream",
|
||||
size=len(content)
|
||||
)
|
||||
session.add(attachment)
|
||||
|
||||
# 保存附件内容到文件
|
||||
attachment_path = os.path.join(self.storage_path, 'attachments', f"attachment_{attachment.id}")
|
||||
os.makedirs(os.path.dirname(attachment_path), exist_ok=True)
|
||||
with open(attachment_path, 'wb') as f:
|
||||
f.write(content)
|
||||
|
||||
# 保存原始邮件数据
|
||||
raw_path = os.path.join(self.storage_path, 'emails', f"email_{email_obj.id}.eml")
|
||||
os.makedirs(os.path.dirname(raw_path), exist_ok=True)
|
||||
|
||||
# 写入原始邮件
|
||||
with open(raw_path, 'w', encoding='utf-8', errors='replace') as f:
|
||||
if isinstance(message, str):
|
||||
f.write(message)
|
||||
else:
|
||||
# 如果是邮件对象,尝试获取原始文本
|
||||
try:
|
||||
f.write(message.as_string())
|
||||
except Exception:
|
||||
# 如果失败,使用提供的原始数据
|
||||
if raw_data:
|
||||
f.write(raw_data)
|
||||
|
||||
session.commit()
|
||||
logging.info(f"邮件保存成功: ID={email_obj.id}")
|
||||
@@ -231,6 +202,7 @@ class MailStore:
|
||||
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
|
||||
logging.error(f"数据库操作失败#1: {str(e)}")
|
||||
raise
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user