2017-09-14 3 views
1

Gmail에서 보내지는 정확한 텍스트를 얻으려고하지만 다음 코드도 있습니다.python을 사용하여 전자 메일 본문 읽기 3.6

import imaplib 

server = imaplib.IMAP4_SSL ('imap.gmail.com', 993) 
username = 'email' 
password = 'pass' 
server.login(username, password) 
stat , content = server.select('Inbox') 
stat , data = server.fetch(content[0],'(UID BODY[TEXT])') 
print (data[0][1].decode()) 
server.close() 
server.logout() 

이 경우 내 출력은

--001a11443f6015ac310559254049 
Content-Type: text/plain; charset="UTF-8" 

hello 

--001a11443f6015ac310559254049 
Content-Type: text/html; charset="UTF-8" 

<div dir="auto">hello</div> 

--001a11443f6015ac310559254049-- 

입니다하지만 내 메시지는

+0

https://stackoverflow.com/questions/2230037/how-to-fetch-an-email-body-using-imaplib-in-python – IsaacDj

답변

0

안녕하세요 내가 사용했던이 방법이었다.

v = data[0][1].decode() 
w = v.split('\n') 

message = w[5] 


은 물론, 내 메시지는 줄 바꿈이 없습니다. 그러나 당신이 그들을 가지고 있다면 이것을 시도 할 수 있습니다. 나는 고급 수준의 지식을 가지고 있지 않지만 지금이 하나가 완벽하게 작동되도록
v = data[0][1].decode() 
w = v.split('\n') 

message = '' 

for i in range(i-5,(len(w)-2)): 
    message = message + w[i] + '\n' 

는 파이썬에 아주 새로운 해요.

는 도움이되기를 바랍니다.

관련 문제