
imap_fetchmime 메시지 내용을 읽는 방법에 대해 설명드리겠습니다.
imap_fetchmime 메시지를 사용하여 메시지 내용을 읽으려면, 메시지의 MIME 타입을 확인하고, 해당 타입의 내용을 읽어야 합니다.
이 경우, 메시지 내용을 읽지 못하는 경우는 메시지의 MIME 타입이 text/html 인 경우입니다.
이 경우, 메시지 내용을 읽으려면, HTML 파서를 사용하여 HTML 내용을 텍스트로 변환해야 합니다.
Python의 경우, BeautifulSoup 라이브러리를 사용하여 HTML 파서를 만들 수 있습니다.
다음은 메시지 내용을 읽는 방법을示한 예제입니다.
#hostingforum.kr
python
import imaplib
import email
from bs4 import BeautifulSoup
# imap 서버와 연결
mail = imaplib.IMAP4('imap.example.com')
mail.login('username@example.com', 'password')
mail.select('inbox')
# 메시지 검색
status, messages = mail.search(None, 'UNSEEN')
# 메시지 읽기
for num in messages[0].split():
status, msg = mail.fetch(num, '(RFC822)')
for part in msg:
if part.get_content_maintype() == 'text':
print(part.get_payload(decode=True).decode())
elif part.get_content_maintype() == 'html':
soup = BeautifulSoup(part.get_payload(decode=True).decode(), 'html.parser')
print(soup.get_text())
이 예제는 메시지 내용을 읽는 데 성공했을 때, 메시지 내용을 읽지 못하는 경우를 처리하여 메시지 내용을 읽는 데 도움이 됩니다.
2025-04-13 08:54