2013-07-26 7 views
1

은 원시 이메일은 보통 내가 파이썬 스크립트를 코딩하고 싶었 그래서 경우이파이썬 : 파이썬

From [email protected] Thu Jul 25 19:28:59 2013 
Received: from a1.local.tld (localhost [127.0.0.1]) 
    by a1.local.tld (8.14.4/8.14.4) with ESMTP id r6Q2SxeQ003866 
    for <[email protected]>; Thu, 25 Jul 2013 19:28:59 -0700 
Received: (from [email protected]) 
    by a1.local.tld (8.14.4/8.14.4/Submit) id r6Q2Sxbh003865; 
    Thu, 25 Jul 2013 19:28:59 -0700 
From: [email protected] 
Subject: ooooooooooooooooooooooo 
To: [email protected] 
Cc: 
X-Originating-IP: 192.168.15.127 
X-Mailer: Webmin 1.420 
Message-Id: <[email protected]> 
Date: Thu, 25 Jul 2013 19:28:59 -0700 (PDT) 
MIME-Version: 1.0 
Content-Type: multipart/mixed; boundary="bound1374805739" 

This is a multi-part message in MIME format. 

--bound1374805739 
Content-Type: text/plain 
Content-Transfer-Encoding: 7bit 

ooooooooooooooooooooooooooooooo 
ooooooooooooooooooooooooooooooo 
ooooooooooooooooooooooooooooooo 

--bound1374805739-- 

같이 보입니다 승/원시 이메일 소스, 바디,에 : 같은 일을 구문 분석하는 방법 얻으려면

From 
To 
Subject 
Body 

이 코드는 내가 개발 한 코드입니까, 아니면 더 좋은 방법입니까?

with open('in.txt', 'r') as file: 
    raw = file.readlines() 

get_list = ['From:','To:','Subject:'] 
info_list = [] 

for i in raw: 
    for word in get_list: 
     if i.startswith(word): 
      info_list.append(i) 

지금 info_list이 될 것입니다 :

a='<title>aaa</title><title>aaa2</title><title>aaa3</title>' 

import re 
a1 = re.findall(r'<(title)>(.*?)<(/title)>', a) 
+0

PLY 또는 특히 PyParsing에 대해 들어 봤어? 수제 파서를 깨뜨리는 문자를 포함 할 수있는 이메일을 많이 작성한다면이 두 파일은 파싱 파일 용으로 설계된 훌륭한 Python 패키지입니다. 먼저 PyParsing을 시도해보십시오. 가장 쉬운 방법입니다. – refi64

답변

9

최종 코드 스 니펫이 무엇을해야하는지 이해하지 못합니다. 그 시점까지는 HTML에 대해 언급하지 않았으므로 갑자기 HTML을 구문 분석하는 예제를 제공하는 이유를 모르겠습니다. 어쨌든 정규 표현식을 사용해서는 안됩니다). 어떤 경우

, 이메일 메시지에서 헤더를 얻기에 관하여 원래의 질문에 대답, 파이썬 표준 라이브러리에서 그렇게하는 코드를 포함

import email 
msg = email.message_from_string(email_string) 
msg['from'] # '[email protected]' 
msg['to'] # '[email protected]' 
+0

나는 움직임이 간접이기보다는 직접적이기 때문에이 대답을 선택한다. (파서 등을 가져 오지 않아야합니다). 환영합니다 .- Sumer Kolcak –

0

이 같은 파일을 읽을 수있는 파일 다음

에 그 원료의 콘텐츠를 작성할 수

['From: [email protected]', 'Subject: ooooooooooooooooooooooo', 'To: [email protected]'] 

을 내가 Body:를 참조 해달라고 기본 콘텐츠로

1

당신은 아마 사용해야 email.parser

s = """ 
From [email protected] Thu Jul 25 19:28:59 2013 
Received: from a1.local.tld (localhost [127.0.0.1]) 
    by a1.local.tld (8.14.4/8.14.4) with ESMTP id r6Q2SxeQ003866 
    for <[email protected]>; Thu, 25 Jul 2013 19:28:59 -0700 
Received: (from [email protected]) 
    by a1.local.tld (8.14.4/8.14.4/Submit) id r6Q2Sxbh003865; 
    Thu, 25 Jul 2013 19:28:59 -0700 
From: [email protected] 
Subject: ooooooooooooooooooooooo 
To: [email protected] 
Cc: 
X-Originating-IP: 192.168.15.127 
X-Mailer: Webmin 1.420 
Message-Id: <[email protected]> 
Date: Thu, 25 Jul 2013 19:28:59 -0700 (PDT) 
MIME-Version: 1.0 
Content-Type: multipart/mixed; boundary="bound1374805739" 

This is a multi-part message in MIME format. 

--bound1374805739 
Content-Type: text/plain 
Content-Transfer-Encoding: 7bit 

ooooooooooooooooooooooooooooooo 
ooooooooooooooooooooooooooooooo 
ooooooooooooooooooooooooooooooo 

--bound1374805739-- 
""" 

import email.parser 

msg = email.parser.Parser().parsestr(s) 
help(msg) 
1

"Body"가 샘플 이메일에 없습니다.

email 모듈을 사용할 수 있습니다.

import email 
    msg = email.message_from_string(email_message_as_text) 

그런 다음 사용 http://docs.python.org/2.7/library/email.parser.html#email.parser.Parser

from email.parser import Parser 
parser = Parser() 

emailText = """PUT THE RAW TEXT OF YOUR EMAIL HERE""" 
email = parser.parsestr(emailText) 

print email.get('From') 
print email.get('To') 
print email.get('Subject') 

몸이 까다 롭습니다 :

print email['To'] 
print email['From'] 

... ... 는

5

다행히 파이썬이 간단하게 등. email.is_multipart()으로 전화하십시오. 그게 거짓이라면 email.get_payload()으로 전화해서 시신을 얻을 수 있습니다. 그러나 사실이라면 email.get_payload()은 메시지 목록을 반환하므로 각각에 대해 get_payload()으로 전화해야합니다.

if email.is_multipart(): 
    for part in email.get_payload(): 
     print part.get_payload() 
else: 
    print email.get_payload()