Skip to content

Commit

Permalink
feat(auto_send_bill_to_shpr): 添加自动发送账单给货主的功能
Browse files Browse the repository at this point in the history
- 新增 email_list 类,实现邮箱登录、获取邮件列表等功能
- 添加数据库操作相关代码,用于存储和查询邮件信息
- 优化代码结构,提高可维护性和可扩展性
  • Loading branch information
jerryliang122 committed Jan 8, 2025
1 parent 5f7eeee commit e7f0724
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
1 change: 1 addition & 0 deletions utils/auto_send_bill_to_shpr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .sql import Session, email_list
82 changes: 73 additions & 9 deletions utils/auto_send_bill_to_shpr/email_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,81 @@
import imbox
from imap_tools import imap_utf7
import json
import logging
from sql import email_list, Session
import datetime

logger = logging.getLogger("my_logger")


# 读取邮箱配置
with open("conf/auto_send_bill_to_shpr/email_config.json", "r", encoding="utf-8") as f:
email_config = json.load(f)

# 登录邮箱
mail = imbox.Imbox(
email_config["host"],
username=email_config["username"],
password=email_config["password"],
ssl=True,
ssl_context=None,
starttls=False,
)
# 数据库操作方法
session = Session()
Agent = email_list


class email_list:
def __init__(self):
# 登录邮箱
self.mail = imbox.Imbox(
email_config["host"],
username=email_config["username"],
password=email_config["password"],
ssl=True,
ssl_context=None,
starttls=False,
)

def get_email_floder(self):
# 获取邮箱文件夹
folders = self.mail.folders()
folders = [imap_utf7.decode(folder["name"]) for folder in folders]
return folders

# 检查时间
def check_date(self):
# 获取今天的日期
today = datetime.datetime.now().date()
# 推算一个月前的今天
last_month = today - datetime.timedelta(days=30)
return today, last_month

def get_email_list(self, folder, date, unread=False):
email_lists = []
# 获取邮件列表
for uid, message in self.mail.messages(
folder=folder, unread=unread, date__gt=date
):
# 获取邮件主题
subject = message.subject
# 获取发件人
sender = message.sent_from
# 获取收件人
receiver = message.sent_to
# 获取邮件日期
date = message.date
email_lists.append(
{
"uid": uid,
"subject": subject,
"sender": sender,
"receiver": receiver,
"date": date,
}
)
return email_lists

def main(self):
# 检查缓存在数据库中的邮件,以及邮件时间。
get_email_list = self.get_email_list(
"INBOX", self.check_date()[1], unread=False
)
print(get_email_list)


if __name__ == "__main__":
email_list = email_list()
email_list.main()
4 changes: 2 additions & 2 deletions utils/auto_send_bill_to_shpr/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@


class email_list(Base):
_tablename_ = "email_list"
id = Column(Integer, primary_key=True, autoincrement=True)
__tablename__ = "email_list"
id = Column(Integer, primary_key=True)
subject = Column(TEXT)
sender = Column(TEXT)
receiver = Column(TEXT)
Expand Down

0 comments on commit e7f0724

Please sign in to comment.