그냥 한글을 인코딩해서 gmail 전송하면 한글이 전부 깨져버리는 문제가 발생
구글링해서 찾아보면 python2.x 코드가 있음
그래서 python3.5에서 만들어서 시험한 코드를 이곳에 기록함
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def send_with_gmail(body):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
gmail_user = 'my_id' # 실제 google 로그인할 때 쓰는 ID
gmail_pw = 'my_pw' # 실제 google 로그인할 때 쓰는 Password
from_addr = 'sender@gmail.com' # 보내는 사람 주소
to_addr = 'iam.byungwoo@gmail.com' # 받는 사람 주소
msg=MIMEMultipart('alternative')
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = 'Send email with Gmail' # 제목
msg.attach(MIMEText(body, 'plain', 'utf-8')) # 내용 인코딩
########################
# https://www.google.com/settings/security/lesssecureapps
# Make sure less_secure_apps select 'use'
########################
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pw)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
print('successfully sent the mail')
except BaseException as e:
print("failed to send mail", str(e))
if __name__ == '__main__':
send_msg = '''
multi
L
I
N
E
'''
send_with_gmail(send_msg)
반응형
'엔지니어' 카테고리의 다른 글
| 삼각형 패턴 만들기 (10) | 2017.02.03 |
|---|---|
| [TS 24.229]Procedures at the S-CSCF (12) | 2017.01.25 |
| Windows10에서 Telnet 기능 켜기 (9) | 2016.12.15 |
| ifup, ifdown과 ifconfig up, ifconfig down의 차이 (RHET5 기준) (1752) | 2016.12.13 |
| [UDP] 특성 (1796) | 2016.12.06 |