async def handle_message(self, message: email.message.EmailMessage):
        transactional_mail = False
        if message.get("Remove-List-Unsubscribe", None):
            del message["List-Unsubscribe"]
            del message["Remove-List-Unsubscribe"]
            transactional_mail = True

        del message['X-Peer']
        del message['X-MailFrom']
        del message['X-RcptTo']

        await aiosmtplib.send(message, hostname=SMTP_HOST, port=SMTP_PORT)

        if transactional_mail:
            log.info(
                f'Transactional Mail has been sent to {",".join(message.get_all("to", []))}'
            )
        else:
            log.info(
                f'Non-Transactional Mail has been sent to {",".join(message.get_all("to", []))}'
            )

        if LOG_CONTENT:
            for part in message.walk():
                # each part is a either non-multipart, or another multipart message
                # that contains further parts... Message is organized like a tree
                if part.get_content_type() == 'text/plain':
                    log.info(part.get_payload())
                if part.get_content_type() == 'text/html':
                    log.info(part.get_payload())
Beispiel #2
0
    def get_main_content(self, msg: email.message.EmailMessage):
        '''
        メール本文、フォーマット、キャラクターセットを取得する。
        '''

        try:
            body_part = msg.get_body()
            main_content = body_part.get_content()
            format_ = body_part.get_content_type()
            charset = body_part.get_content_charset()

        except Exception as error:
            print(error)
            main_content = '解析失敗'
            format_ = '不明'
            charset = '不明'
            # get_bodyでエラーになるのは文字コード設定がおかしいメールを受信した場合なので、
            # decodeせずにテキスト部分をそのまま返す。
            for part in msg.walk():
                if part.get_content_type() == 'text/plain':
                    format_ = part.get_content_type()
                    main_content = str(part.get_payload())
                    charset = part.get_content_charset()

        return main_content, format_, charset
Beispiel #3
0
    def _get_body(msg: email.message.EmailMessage) -> Optional[str]:
        candidate = None

        for part in msg.walk():
            if part.get_content_type() == "text/plain" and not part.is_multipart() and part.get(
                    "Content-Disposition") is None:
                if not candidate:
                    candidate = part.get_payload(decode=True).decode("utf-8")
                else:
                    raise Exception("two possible candidates!")

        return candidate
Beispiel #4
0
 def email_message_to_plain(em: email.message.EmailMessage) -> str:
     """
     获取EmailMessage对象中的简单文本
     @ param em: EmailMessage对象
     @ return: 给定对象中的简单文本
     """
     for part in em.walk():
         part_content_type = part.get_content_type()
         if part_content_type not in ['text/plain','text/html']:
             continue
         try:
             part_content = part.get_content()
         except Exception:
             part_content = str(part.get_payload())
         if part_content_type == 'text/plain':
             return part_content if part_content is not None else "empty"
         else:
             return Formatter.html_to_plain(part_content)