當前位置: 首頁>>代碼示例>>Python>>正文


Python EmailMessage.replace_header方法代碼示例

本文整理匯總了Python中email.message.EmailMessage.replace_header方法的典型用法代碼示例。如果您正苦於以下問題:Python EmailMessage.replace_header方法的具體用法?Python EmailMessage.replace_header怎麽用?Python EmailMessage.replace_header使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在email.message.EmailMessage的用法示例。


在下文中一共展示了EmailMessage.replace_header方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: create_mail

# 需要導入模塊: from email.message import EmailMessage [as 別名]
# 或者: from email.message.EmailMessage import replace_header [as 別名]
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)

    if gpgme_ctx is not None:
        signed_bytes = attachment_parent.as_bytes()
        hash_algo, signature = detached_signature(gpgme_ctx, signed_bytes)

        msg.add_attachment(signature, "application", "pgp-signature",
                           cte="8bit")
        # the signature part should now be the last of two parts in the
        # message, the first one being the signed part.
        signature_part = list(msg.iter_parts())[1]
        if "Content-Disposition" in signature_part:
            del signature_part["Content-Disposition"]

        msg.replace_header("Content-Type", "multipart/signed")

        micalg = hash_algorithms.get(hash_algo)
        if micalg is None:
            raise RuntimeError("Unexpected hash algorithm %r from gpgme"
                               % (signature[0].hash_algo,))

        msg.set_param("protocol", "application/pgp-signature")
        msg.set_param("micalg", micalg)

    msg.add_header("From", sender)
    msg.add_header("To", recipient)
    msg.add_header("Subject", subject)
    msg.add_header("Date", formatdate(timeval=None, localtime=True))

    # take the domain part of sender as the domain part of the message
    # ID. We assume that sender has the form [email protected], so we can
    # just the part of sender after the '@'.
    sender_domain = sender.partition("@")[-1]
    if not sender_domain:
        raise RuntimeError("Could not extract the domain from the sender (%r)"
                           " for the Message-ID" % (sender,))
    msg.add_header("Message-Id", make_msgid(domain=sender_domain))

    return msg
開發者ID:Intevation,項目名稱:intelmq-mailgen,代碼行數:53,代碼來源:mail.py


注:本文中的email.message.EmailMessage.replace_header方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。