use email.message_from_bytes instead of Parser(policy=SMTPUTF8).parsestr

This commit is contained in:
Son NK 2020-04-02 18:10:08 +02:00
parent 55b1ce2067
commit ed8caa237a
1 changed files with 6 additions and 8 deletions

View File

@ -30,20 +30,20 @@ It should contain the following info:
""" """
import email
import time import time
import uuid import uuid
from email import encoders from email import encoders
from email.message import Message from email.message import Message
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.parser import Parser
from email.policy import SMTPUTF8
from email.utils import parseaddr from email.utils import parseaddr
from io import BytesIO from io import BytesIO
from smtplib import SMTP from smtplib import SMTP
from typing import Optional from typing import Optional
from aiosmtpd.controller import Controller from aiosmtpd.controller import Controller
from aiosmtpd.smtp import Envelope
from app import pgp_utils, s3 from app import pgp_utils, s3
from app.config import ( from app.config import (
@ -846,9 +846,8 @@ def handle_spam(
) )
def handle_unsubscribe(envelope): def handle_unsubscribe(envelope: Envelope):
message_data = envelope.content.decode("utf8", errors="replace") msg = email.message_from_bytes(envelope.original_content)
msg = Parser(policy=SMTPUTF8).parsestr(message_data)
# format: alias_id: # format: alias_id:
subject = msg["Subject"] subject = msg["Subject"]
@ -895,7 +894,7 @@ def handle_unsubscribe(envelope):
class MailHandler: class MailHandler:
async def handle_DATA(self, server, session, envelope): async def handle_DATA(self, server, session, envelope: Envelope):
LOG.debug( LOG.debug(
"===>> New message, mail from %s, rctp tos %s ", "===>> New message, mail from %s, rctp tos %s ",
envelope.mail_from, envelope.mail_from,
@ -921,8 +920,7 @@ class MailHandler:
res: [(bool, str)] = [] res: [(bool, str)] = []
for rcpt_to in envelope.rcpt_tos: for rcpt_to in envelope.rcpt_tos:
message_data = envelope.content.decode("utf8", errors="replace") msg = email.message_from_bytes(envelope.original_content)
msg = Parser(policy=SMTPUTF8).parsestr(message_data)
# Reply case # Reply case
# recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix # recipient starts with "reply+" or "ra+" (ra=reverse-alias) prefix