2015-06-09 55 views
0

我对使用imaplib从我的邮箱中获取主题的脚本有困难。python email subject r n

这里,是我的脚本:

for mail in response[0].split(): 
    typ, response = imap.fetch(mail, '(RFC822)') 
    for response_part in response: 
     if isinstance(response_part, tuple): 
      msg = email.message_from_string(response_part[1]) 
      for header in [ 'subject']: 
       print msg["subject"] 

,但我得到的输出:

[astreinte] [2015.06.09 07:36:04] blurp : TIMESTAMP ALERT 
Disaster is PROBLEM 

我的问题是行尾之后 “TIMESTAMP ALERT”,如果我得到msg.items()我可以查看:

('Subject', '[astreinte] [2015.06.09 07:36:04] ankdo2ga22 : DOFUS TIMESTAMP ALERT\r\n Disaster is PROBLEM') 

所以,我不明白为什么\ r \ n存在?它是否存在于元数据中。但是当zabbix发送消息时不会添加。 我使用office365。

非常感谢您的帮助。

+0

你的意思是“虽然我有一条消息,其中有一行以主题结尾,当消息发送时,行结尾没有出现”?不知道什么zabbix和Office 365必须与此写作 – holdenweb

+0

我非常确定它必须像这样发送。这可能是一场意外,甚至在发送时都没有注意到。你的Python是正确的。 – JosiahDaniels

+0

这看起来像标题折叠。我会认为电子邮件解析库会为你处理这个问题。我没有为你解决头痛的问题。 – Max

回答

0

无耻插头:尝试使用我的图书馆:https://pypi.python.org/pypi/O365

如果您正在使用Office 365,应该让你的生活变得更加简单。

在回答你为什么是\ r \ n目前的问题,这是因为Office365是基于Windows。由于传统原因,Windows使用\ r \ n来换行。

+0

Umm,IMAP,SMTP和电子邮件格式都要求使用\ r \ n行结尾。这与Windows无关。 – Max

+0

真的吗?我不知道。我立场纠正。 – Narcolapser

相关问题