def get_mail_detail(self, mail_id: int) -> MailDetailData: if not self.confirm_pop_server_is_on(): raise POPServerUseless('POP3 服务未运行') mail = self._mail.get_mail_by_id(mail_id) # 一定要判断是否存在这封邮件 if mail is None: raise MailNotExist('邮件不存在') to_user = None for item in mail.to_user: if item.to_user_id == self._user_id and item.is_to_del == 0: to_user = item.to_user break if to_user is None: raise NotYourMail('不是你的邮件') data = Protocol().get_mail_detail(mail.file_name, to_user) decode = MailDecode(data, mail.title) return MailDetailData( from_addr=f'{mail.user.username}@wghtstudio.cn', to_addr=list(map(lambda x: f'{x.to_user.username}@wghtstudio.cn', mail.to_user)), content=decode.get_content(), subject=decode.get_subject(), time=mail.create_at.strftime('%Y-%m-%d %H:%M') )
def receive_delete(self, mail_id: int): if self._mail.get_mail_by_id(mail_id) is None: raise MailNotExist("邮件不存在") # 依次删除本身及多对多 self._mail.del_user_mail(mail_id) self._mail.del_mail_weak(mail_id) session_commit()
def get_mail_detail(self, mail_id: int) -> MailDetailData: mail = self._mail.get_mail_by_id(mail_id) # 一定要判断是否存在这封邮件 if mail is None: raise MailNotExist('邮件不存在') # 通过解码类解码 decode = MailDecode(s=bytes(mail.content, 'utf-8'), title=mail.title) return MailDetailData( from_addr=f'{mail.user.username}@wghtstudio.cn', to_addr=list( map(lambda x: f'{x.to_user.username}@wghtstudio.cn', mail.to_user)), content=decode.get_content(), subject=decode.get_subject(), time=mail.create_at.strftime('%Y-%m-%d %H:%M'))
def get_mail_detail(self, mail_id: int) -> MailDetailData: mail = self._mail.get_mail_by_id(mail_id) if mail is None: raise MailNotExist('邮件不存在') message = email.message_from_string(mail.content) content = '' for part in message.walk(): if not part.is_multipart(): content = part.get_payload(decode=True) return MailDetailData( from_addr=f'{mail.user.username}@wghtstudio.cn', to_addr=list( map(lambda x: f'{x.to_user.username}@wghtstudio.cn', mail.to_user)), content=str(content, encoding='utf-8'), subject=self._decode_str(mail.title), time=mail.create_at.strftime('%Y-%m-%d %H:%M'))
def send_delete(self, mail_id=None) -> None: if self._mail.get_mail_by_id(mail_id) is None: raise MailNotExist("邮件不存在") self._mail.del_send_user_mail(mail_id) return