
	                	                 
imap_fetchstructure 함수는 메일의 구조를 가져올 때, 첨부 파일의 구조를 모두 가져오지 않는 것으로 보입니다. 
이러한 문제를 해결하기 위해 imap_fetchbody 함수를 사용할 수 있습니다. imap_fetchbody 함수는 메일의 첨부 파일을 가져올 때, 첨부 파일의 구조를 모두 가져올 수 있습니다.
imap_fetchstructure 함수를 사용할 때, 첨부 파일이 여러 개 있는 메일을 가져올 때, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
python
import imaplib
import email
# imaplib를 사용하여 메일 서버에 접속합니다.
mail = imaplib.IMAP4_SSL('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]
    email_message = email.message_from_bytes(raw_email)
    # 첨부 파일의 구조를 가져옵니다.
    for part in email_message.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        if part.get('Content-Disposition') is None:
            continue
        filename = part.get_filename()
        if filename:
            print(f'첨부 파일 이름: {filename}')
            print(f'첨부 파일 크기: {len(part.get_payload())}')
이러한 코드를 사용하면, 첨부 파일이 여러 개 있는 메일을 가져올 때, 첨부 파일의 구조를 모두 가져올 수 있습니다.
2025-05-01 22:27