
imap_fetchstructure 함수는 메일 서버에서 메일 구조를 가져올 때, 메일 헤더, 본문, 첨부 파일 등 메일의 구조를 가져올 수 있습니다.
이 함수는 메일의 구조를 가져올 때, 다음의 형식의 데이터를 반환합니다.
* 메일 헤더: 메일의 헤더를 가져올 때, '0'을 파라미터로 전달하면 메일의 헤더만 가져올 수 있습니다.
* 메일 본문: 메일 본문을 가져올 때, '1'을 파라미터로 전달하면 메일 본문을 가져올 수 있습니다.
* 메일 첨부 파일: 메일 첨부 파일을 가져올 때, '2'를 파라미터로 전달하면 메일 첨부 파일을 가져올 수 있습니다.
예를 들어, 메일 헤더만 가져오려면 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
python
import imaplib
import email
# 메일 서버와 연결
mail = imaplib.IMAP4('imap.gmail.com')
mail.login('your_email@gmail.com', 'your_password')
mail.select('inbox')
# 메일 헤더만 가져오기
status, messages = mail.search(None, 'ALL')
for num in messages[0].split():
status, data = mail.fetch(num, '(RFC822'))
raw_email = data[0][1][:]
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
print(email_message['Subject'])
이러한 예제를 통해 imap_fetchstructure 함수를 사용하여 메일 구조를 가져올 때, 어떤 형식의 데이터를 반환하는지 이해할 수 있습니다.
2025-04-13 10:37