简体   繁体   English

如何让 email.utils.make_msgid 从地址域而不是 Python 中的本地主机名中选择

[英]How can I get email.utils.make_msgid to pick from address domain instead of local hostname in Python

I am trying to get the message-id to pick the domain portion of the from address and not sure how to do it.我正在尝试获取消息 ID 以选择来自地址的域部分,但不知道该怎么做。 I can hardcode it but I would like it pick up whatever the domain portion of the from address我可以对其进行硬编码,但我希望它获取来自地址的任何域部分

message = MIMEMultipart("alternative")
message['From'] = email.utils.formataddr(('User1', 'aniga@domain.com')) # header address
message['Date'] = email.utils.formatdate(timeval=None, localtime=False, usegmt=False)
message['Message-ID'] = email.utils.make_msgid("".join(random.choice("0123456789ABCDEF") for i in range(16)), domain=pickfrom from address domain
message['Subject'] = 'Testing from Python'
message['To'] = email.utils.formataddr((recpient, recpient@example.com))

There is no built-in support for doing that;没有内置支持这样做; if you want to extract the domain part from the From: address, you have to do that separately.如果要从From:地址中提取域部分,则必须单独进行。

There should be no need for you to pick the idstring yourself, by the way;顺便说一句,您应该不需要自己选择idstring let Python take care of that.让 Python 来处理。

You'll probably also want to upgrade to use the Python 3.6+ updated EmailMessage API for new code.您可能还想升级以使用 Python 3.6+ 更新的EmailMessage API 获取新代码。

from email.message import EmailMessage
import email.utils

message = EmailMessage()
message['From'] = email.utils.formataddr(('User1', 'aniga@domain.com'))
message['Date'] = email.utils.formatdate(timeval=None, localtime=False, usegmt=False)
# Notice change here
message['Message-ID'] = email.utils.make_msgid(domain=message['From'].groups[0].addresses[0].domain)
message['Subject'] = 'Testing from Python'
# Trivial typo + syntax edit here
message['To'] = email.utils.formataddr(('recipient', 'recipient@example.com'))

If what you have isn't a formataddr it won't have a groups attribute;如果你所拥有的不是formataddr它就没有groups属性; if you simply have a str containing the sender's address, a quick and dirty heuristic is to simply split on @ and take everything up through the first > or whitespace character as probably the domain name.如果你只是有一个包含发件人地址的str ,一个快速而肮脏的启发式方法是简单地拆分@并通过第一个>或空格字符将所有内容作为可能的域名。 But this has a number of pesky corner cases you really want to avoid - it's better to parse the From: header (again, if necessary) and be reasonably sure that you really get it properly parsed.但这有一些你真的想避免的讨厌的极端情况 - 最好解析From: header (再次,如果需要),并合理地确定你真的得到了正确的解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从python电子邮件对象获取实际的电子邮件地址作为字符串 - How can I get actual email address as a string from python email object 如何从 Python 中的 FQDN 中提取主机名和(子)域? - How do I extract the hostname and the (sub)domain from a FQDN in Python? 如何使用 rfc822msgid 搜索电子邮件: - How do I search email with rfc822msgid: 如何使用 sqlite3 使 email 地址在 python 中唯一 - How can I make email address unique in python using sqlite3 如何使用 Python 获取系统主机名? - How can I use Python to get the system hostname? 如何使用 Python 从本地文件生成数据 url? - How can I make a data url from a a local file with Python? 如何从 ip(例如 192.168.1.3)Linux Shell 或 python3 在本地找到设备主机名 - How can I find devices hostname in local area from ip (ex. 192.168.1.3) Linux Shell or python3 如何从 Python 的 Counter 类中获得加权随机选择? - How can I get a weighted random pick from Python's Counter class? 如何获取 email.utils.formataddr 以显示带有 twisted.mail.smtp.sendmail 的发件人姓名? - How can I get email.utils.formataddr to show a sender name with twisted.mail.smtp.sendmail? Python:翻译文本显示msgid而不是msgstr - Python: Translating text shows msgid instead msgstr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM