
imap_fetchstructure 함수의 반환값인 imap4r_object는 메일의 구조를 나타내는 객체입니다. 이 객체는 메일의 헤더, 본문, 첨부 파일 등 다양한 정보를 포함하고 있습니다.
이 객체를 사용하려면, imap4r_object의 속성을 사용해야 합니다. 예를 들어, 메일의 헤더를 가져오려면 imap4r_object의 'envelope' 속성을 사용할 수 있습니다. 본문을 가져오려면 'body' 속성을 사용할 수 있습니다.
imap4r_object의 속성은 다음과 같습니다.
- envelope: 메일의 헤더 정보를 포함하는 객체
- body: 메일의 본문 정보를 포함하는 객체
- parts: 메일의 첨부 파일 정보를 포함하는 객체
이러한 속성을 사용하여 메일의 구조를 가져올 수 있습니다. 예를 들어, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
python
import imaplib
import email
# imaplib를 사용하여 메일 서버에 연결
mail = imaplib.IMAP4('imap.gmail.com')
mail.login('your_email@gmail.com', 'your_password')
mail.select('inbox')
# imap_fetchstructure 함수를 사용하여 메일의 구조를 가져옵니다.
raw_email = mail.fetch('1', '(RFC822)')[1][0][1]
raw_email = email.message_from_bytes(raw_email)
# imap4r_object를 사용하여 메일의 구조를 가져옵니다.
structure = raw_email.get_content_maintype()
print(structure) # text/html
# envelope 속성을 사용하여 메일의 헤더 정보를 가져옵니다.
envelope = raw_email['envelope']
print(envelope) # {'to': ['recipient@example.com'], 'cc': [], 'subject': 'Hello'}
# body 속성을 사용하여 메일의 본문 정보를 가져옵니다.
body = raw_email.get_payload()
print(body) # HTML 코드
이러한 예제를 통해 imap_fetchstructure 함수의 반환값인 imap4r_object를 사용하여 메일의 구조를 가져올 수 있습니다.
2025-04-07 08:57