
imap_8bit를 사용하여 한글을 전송할 때는, MIME 타입을 설정하여 UTF-8 인코딩을 사용하는 것을 권장합니다.
예를 들어, Python의 imaplib 라이브러리를 사용하는 경우, 다음과 같이 설정할 수 있습니다.
#hostingforum.kr
python
import email
from email.header import Header
from email.mime.text import MIMEText
import imaplib
# 메일 서버 설정
mail_server = 'imap.example.com'
mail_port = 993
mail_user = 'your_email@example.com'
mail_pass = 'your_password'
# 메일 내용 설정
subject = '한글 제목'
body = '한글 본문'
# MIME 타입 설정
msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
# 메일 서버 연결
mail = imaplib.IMAP4_SSL(mail_server)
mail.login(mail_user, mail_pass)
mail.select('inbox')
# 메일 전송
mail.store('inbox', '+FLAGS', '\Seen')
mail.sendmail(mail_user, mail_user, msg.as_string())
mail.close()
mail.logout()
한글이 깨지는 이유는, 메일 서버가 한글을 지원하지 않거나, 메일 클라이언트가 한글을 올바르게 인코딩하지 못했을 때 발생할 수 있습니다.
해결할 수 있는 방법은, 메일 서버를 변경하거나, 메일 클라이언트의 한글 설정을 확인하는 것입니다.
또한, 메일 내용을 UTF-8 인코딩으로 변환하여 전송하는 것도 한글 깨짐을 해결하는 방법 중 하나입니다.
예를 들어, Python의 chardet 라이브러리를 사용하여 메일 내용의 인코딩을 자동으로 감지할 수 있습니다.
#hostingforum.kr
python
import chardet
# 메일 내용 읽기
mail_content = mail.fetch('inbox', '(RFC822)')[1][0][1]
# 인코딩 감지
encoding = chardet.detect(mail_content)['encoding']
# 인코딩 변환
mail_content = mail_content.decode(encoding, 'replace')
# UTF-8 인코딩으로 변환
mail_content = mail_content.encode('utf-8', 'replace')
2025-04-22 11:48