从电子邮件附件中提取“To:”标头
我正在使用 python 打开服务器上的电子邮件(POP3)。每封电子邮件都有一个附件,该附件本身就是转发的电子邮件。
我需要从附件中获取“收件人:”地址。
我正在使用 python 来尝试帮助我学习这门语言,但我还不是那么好!
我已经有了的代码是这样的
import poplib, email, mimetypes
oPop = poplib.POP3( 'xx.xxx.xx.xx' )
oPop.user( '[email protected]' )
oPop.pass_( 'xxxxxx' )
(iNumMessages, iTotalSize ) = oPop.stat()
for thisNum in range(1, iNumMessages + 1):
(server_msg, body, octets) = oPop.retr(thisNum)
sMail = "\n".join( body )
oMsg = email.message_from_string( sMail )
# now what ??
我知道我有电子邮件作为电子邮件类的实例,但我不确定如何获取附件
我知道使用
sData = 'To'
if sData in oMsg:
print sData + "", oMsg[sData]
可以从主消息中获取“收件人:”标头但是我如何从附件中获取它?
我已经尝试过,
for part in oMsg.walk():
oAttach = part.get_payload(1)
但我不确定如何处理 oAttach 对象。我尝试将其转换为字符串,然后将其传递给
oMsgAttach = email.message_from_string( oAttach )
但这没有任何作用。我对 python 文档有点不知所措,需要一些帮助。提前致谢。
I am using python to open an email on the server (POP3). Each email has an attachment which is a forwarded email itself.
I need to get the "To:" address out of the attachment.
I am using python to try and help me learn the language and I'm not that good yet !
The code I have already is this
import poplib, email, mimetypes
oPop = poplib.POP3( 'xx.xxx.xx.xx' )
oPop.user( '[email protected]' )
oPop.pass_( 'xxxxxx' )
(iNumMessages, iTotalSize ) = oPop.stat()
for thisNum in range(1, iNumMessages + 1):
(server_msg, body, octets) = oPop.retr(thisNum)
sMail = "\n".join( body )
oMsg = email.message_from_string( sMail )
# now what ??
I understand that I have the email as an instance of the email class but I'm not sure how to get to the attachment
I know that using
sData = 'To'
if sData in oMsg:
print sData + "", oMsg[sData]
gets me the 'To:' header from the main message but how do I get that from the attachment ?
I've tried
for part in oMsg.walk():
oAttach = part.get_payload(1)
But I'm not sure what to do with the oAttach object. I tried turning it into a string and then passing it to
oMsgAttach = email.message_from_string( oAttach )
But that does nothing. I'm a little overwhelmed by the python docs and need some help. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我的收件箱中没有一封具有代表性的电子邮件,则很难完成此操作(我从未使用过 poplib)。话虽如此,一些事情可能会对我的一点点调查有所帮助:
首先,充分利用 python 的命令行界面以及 dir() 和 help() 函数:这些可以告诉你很多关于即将发生的事情。您始终可以在代码中插入
help(oAttach)
、dir(oAttach)
和print oAttach
来了解正在发生的情况循环。如果您在命令行界面中逐行输入它,在这种情况下会更容易。我认为您需要做的是仔细检查每个附件并找出它是什么。对于传统的电子邮件附件,它可能是 base64 编码的,因此类似的内容可能会有所帮助:
请注意,您可能需要在每种情况下检查 oAttach 以检查它是否看起来像一条消息。当您获得
sMail
变量后,将其打印到屏幕上。然后您可以在其中查找类似Content-Transfer-Encoding: base64
的内容,这将为您提供有关附件如何编码的线索。正如我所说,我没有使用过任何 poplib、email 或 mimetypes 模块,所以我不确定这是否有帮助,但我认为它可能会为您指明正确的方向。
Without having an email in my inbox that is representative, it's difficult to work this one through (I've never used poplib). Having said that, some things that might help from my little bit of investigation:
First of all, make lots of use of the command line interface to python and the
dir()
andhelp()
functions: these can tell you lots about what's coming out. You can always inserthelp(oAttach)
,dir(oAttach)
andprint oAttach
in your code to get an idea of what's going on as it loops round. If you're typing it into the command line interface line-by-line, it's even easier in this case.What I think you need to do is to go through each attachment and work out what it is. For a conventional email attachment, it's probably base64 encoded, so something like this might help:
Note that you probably need to check oAttach in each case to check that it looks like a message. When you've got your
sMail
variable, print it out to the screen. Then you can look for something likeContent-Transfer-Encoding: base64
in there, which will give you a clue to how the attachment is encoded.As I said, I've not used any of the poplib, email or mimetypes modules, so I'm not sure whether that'll help, but I thought it might point you in the right direction.