本文整理汇总了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