def gmail_attach_file(_from, _passwd, to_list, subject, content, file_chunk_map, host='smtp.gmail.com', port=587): outer = MIMEBase('multipart', 'mixed') outer['Subject'] = Header(subject.encode('utf-8'), 'utf-8') outer['From'] = _from outer['To'] = ', '.join(to_list) outer.preamble = 'This is a multi-part message in MIME format.\n\n' outer.epilogue = '' msg = MIMEText(content.encode('utf-8'), _charset='utf-8') outer.attach(msg) for file, chunk in file_chunk_map.items(): ctype, encoding = guess_type(file) maintype, subtype = ctype.split('/', 1) msg = MIMEApplication(chunk, _subtype=subtype) msg.add_header('Content-Disposition', 'attachment', filename=file) outer.attach(msg) s = smtplib.SMTP(host, port) s.ehlo() s.starttls() s.ehlo() s.login(_from, _passwd) s.sendmail(_from, to_list, outer.as_string()) return s.quit()
from email.mime.base import MIMEBase from email.mime.audio import MIMEAudio from email.mime.image import MIMEImage from email.mime.text import MIMEText from email.mime.application import MIMEApplication HOST = 'smtp.server' # SMTP 서버 me = 'gslee@localhost' # 내 주소 receiver = ['gslee@localhost', 'jangc@localhost'] # 받을 사람 주소 리스트 subject = '첨부 파일 메일 보내기' outer = MIMEBase('multipart', 'mixed') outer['Subject'] = Header(subject.encode('utf-8'), 'utf-8') outer['From'] = me outer['To'] = ', '.join(receiver) # 수신자 문자열 만들기 outer.preamble = 'This is a multi-part message in MIME format.\n\n' outer.epilogue = '' # 이렇게 하면 멀티파트 경계 다음에 줄 바꿈 코드가 삽입 됨 msg = MIMEText('파일들을 첨부합니다.'.encode('utf-8'), _charset='utf-8') outer.attach(msg) files = glob.glob('*.*') for fileName in files: ctype, encoding = mimetypes.guess_type(fileName) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) if maintype == 'text': fd = open(fileName, encoding='utf-8') # utf-8이라 가정.. msg = MIMEText(fd.read().encode('utf-8'), _subtype=subtype, _charset='utf-8')