修复邮件系统保存邮件的问题:1. 添加邮箱ID查询逻辑 2. 调整SMTP客户端连接方式
This commit is contained in:
@@ -161,11 +161,40 @@ class MailStore:
|
|||||||
# 保存到数据库
|
# 保存到数据库
|
||||||
session = self.db_session_factory()
|
session = self.db_session_factory()
|
||||||
try:
|
try:
|
||||||
|
# 查找收件人对应的邮箱
|
||||||
|
mailbox_id = None
|
||||||
|
recipients_list = recipients if isinstance(recipients, list) else [recipients]
|
||||||
|
|
||||||
|
for recipient in recipients_list:
|
||||||
|
# 提取域名和用户名
|
||||||
|
if '@' in recipient:
|
||||||
|
username, domain = recipient.split('@', 1)
|
||||||
|
logging.info(f"查找邮箱: 用户名={username}, 域名={domain}")
|
||||||
|
|
||||||
|
# 查询域名
|
||||||
|
domain_obj = session.query(Domain).filter(Domain.name == domain).first()
|
||||||
|
if domain_obj:
|
||||||
|
# 查询邮箱
|
||||||
|
mailbox = session.query(Mailbox).filter(
|
||||||
|
Mailbox.domain_id == domain_obj.id,
|
||||||
|
Mailbox.address == username
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if mailbox:
|
||||||
|
mailbox_id = mailbox.id
|
||||||
|
logging.info(f"找到邮箱ID: {mailbox_id}")
|
||||||
|
break
|
||||||
|
|
||||||
|
if not mailbox_id:
|
||||||
|
logging.error(f"收件人 {recipients} 没有对应的邮箱记录")
|
||||||
|
return False, "收件人邮箱不存在"
|
||||||
|
|
||||||
# 创建新邮件记录
|
# 创建新邮件记录
|
||||||
new_email = Email(
|
new_email = Email(
|
||||||
|
mailbox_id=mailbox_id, # 设置邮箱ID
|
||||||
subject=subject,
|
subject=subject,
|
||||||
sender=sender,
|
sender=sender,
|
||||||
recipients=','.join(recipients) if isinstance(recipients, list) else recipients,
|
recipients=','.join(recipients_list) if len(recipients_list) > 1 else recipients_list[0],
|
||||||
body_text=body_text,
|
body_text=body_text,
|
||||||
body_html=body_html,
|
body_html=body_html,
|
||||||
received_at=datetime.now()
|
received_at=datetime.now()
|
||||||
|
|||||||
@@ -49,7 +49,8 @@ def send_verification_email(smtp_host, smtp_port, to_email):
|
|||||||
|
|
||||||
# 连接SMTP服务器并发送
|
# 连接SMTP服务器并发送
|
||||||
try:
|
try:
|
||||||
server = smtplib.SMTP(smtp_host, smtp_port)
|
# 使用显式的源地址
|
||||||
|
server = smtplib.SMTP(smtp_host, smtp_port, timeout=30, source_address=('127.0.0.1', 0))
|
||||||
server.set_debuglevel(1) # 开启调试模式
|
server.set_debuglevel(1) # 开启调试模式
|
||||||
print(f"连接到SMTP服务器: {smtp_host}:{smtp_port}")
|
print(f"连接到SMTP服务器: {smtp_host}:{smtp_port}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user