Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
';至';在Python中解析电子邮件时的字段_Python_Parsing_Email - Fatal编程技术网

';至';在Python中解析电子邮件时的字段

';至';在Python中解析电子邮件时的字段,python,parsing,email,Python,Parsing,Email,我正在使用python标准库的电子邮件模块来解析电子邮件。类似这样的内容允许我确定发件人: msg = email.message_from_string(data) sender = msg.get_unixfrom() 但是我很难确定邮件是给谁的 想法?您可以始终使用\uuuu getitem\uuuu(名称),其中name是要检索的标题字段的名称;在这种情况下,“收件人”、“抄送”和“密件抄送” 有关更多信息,请访问:将获取给定标题名称的所有值 print msg.get_all('to

我正在使用python标准库的电子邮件模块来解析电子邮件。类似这样的内容允许我确定发件人:

msg = email.message_from_string(data)
sender = msg.get_unixfrom()
但是我很难确定邮件是给谁的


想法?

您可以始终使用
\uuuu getitem\uuuu(名称)
,其中name是要检索的标题字段的名称;在这种情况下,“收件人”、“抄送”和“密件抄送”

有关更多信息,请访问:

将获取给定标题名称的所有值

print msg.get_all('to')

您可以访问邮件的所有标题,例如
msg[“From”]
。对于收件人,请使用
msg.get_all(“To”)
,因为可能有多个值

还应注意以下事项:

标头以保留大小写的形式存储和返回,但不区分大小写进行匹配


交互式会话示例:

>>> import email
>>> msg = email.message_from_string("from: me\nto: you\n\nTest\n")
>>> msg.get_all('to')
['you']
>>> msg['to']
'you'
>>> 

可以使用反斜杠转义下划线。或者使用记号将其标记为代码;)密件抄送头将在发送前删除,据我所知,它仅存在于smtp会话中请注意,在您的示例中,
发件人
是邮件的“发件人:”头,而不是邮件的“发件人:”头。