Ejemplo n.º 1
0
 def _parse(cls, values: Sequence[Sequence[bytes]]) \
         -> Iterable[BaseHeader]:
     for value in values:
         lines = [line.decode('ascii', 'surrogateescape')
                  for line in value]
         hdr_name, hdr_val = SMTP.header_source_parse(lines)
         yield cls._registry(hdr_name, hdr_val)
Ejemplo n.º 2
0
    def test_parse_email_with_policy(self):
        if not SMTP:
            return

        message_object = email.message_from_bytes(
            raw_email_encoded_needs_refolding,
            policy=SMTP.clone(refold_source='all')
        )

        self.assertEqual([
            {'email': '*****@*****.**', 'name': 'Receiver'},
            {'email': '*****@*****.**', 'name': 'Second Receiver'}
        ], get_mail_addresses(message_object, 'to'))
Ejemplo n.º 3
0
 def _parse_content_type(cls, header: str) -> ContentTypeHeader:
     return SMTP.header_fetch_parse('Content-Type', header)  # type: ignore
Ejemplo n.º 4
0
    def set_content(self, msg, obj, *args, **kw):
        replace_from = False
        if (isinstance(obj, str)
                and kw.get("cte") not in ("quoted-printable", "base64")
                and re.search("^From ", obj, re.MULTILINE) is not None):
            kw["cte"] = "quoted-printable"
            replace_from = True
        raw_data_manager.set_content(msg, obj, *args, **kw)
        if (msg.get("content-transfer-encoding") == "quoted-printable"
                and replace_from):
            content = msg.get_payload(decode=False)
            from_escaped = content.replace("From ", "From=20")
            msg.set_payload(from_escaped)


openpgp_policy = SMTP.clone(cte_type="7bit",
                            content_manager=FromQuotingContentManager())


def create_mail(sender, recipient, subject, body, attachments, gpgme_ctx):
    """Create an email either as single or multi-part with attachments.
    """
    msg = EmailMessage(policy=openpgp_policy)
    msg.set_content(body)
    msg.make_mixed()
    signed_part = next(msg.iter_parts())

    if attachments:
        for args, kw in attachments:
            signed_part.add_attachment(*args, **kw)

    if gpgme_ctx is not None:
Ejemplo n.º 5
0
 def _parse(cls, values: Sequence[Sequence[bytes]]) \
         -> Iterable[BaseHeader]:
     for value in values:
         lines = [line.decode('ascii', 'surrogateescape') for line in value]
         hdr_name, hdr_val = SMTP.header_source_parse(lines)
         yield cls._registry(hdr_name, hdr_val)
Ejemplo n.º 6
0
    def get_content(self, msg, *args, **kw):
        return raw_data_manager.get_content(msg, *args, **kw)

    def set_content(self, msg, obj, *args, **kw):
        if isinstance(obj, str):
            kw["cte"] = "quoted-printable"

        raw_data_manager.set_content(msg, obj, *args, **kw)

        if msg.get("content-transfer-encoding") == "quoted-printable":
            content = msg.get_payload(decode=False)
            from_escaped = content.replace("From ", "From=20")
            msg.set_payload(from_escaped)


mailgen_policy = SMTP.clone(cte_type="7bit",
                            content_manager=MailgenContentManager())


def create_mail(sender, recipient, subject, body, attachments, gpgme_ctx):
    """Create an email either as single or multi-part with attachments.
    """
    msg = EmailMessage(policy=mailgen_policy)
    msg.set_content(body)
    attachment_parent = msg
    if gpgme_ctx is not None:
        msg.make_mixed()
        attachment_parent = next(msg.iter_parts())

    if attachments:
        for args, kw in attachments:
            attachment_parent.add_attachment(*args, **kw)