gmail 계정 연동 python smtplib로 이메일 전송하기
# gamil_python_test.py import smtplib from email.mime.text import MIMEText
def send_email(senders='test@gmail.com', receiver='test@gmail.com', content='Hello.'): print('email 전송') s = smtplib.SMTP(host='smtp.gmail.com', port=int(587)) # TLS:587 SSL:465 s.ehlo() # say Hello s.starttls() # TLS 보안 시작 s.login(user=str('USER ID'), password=str('USER PASSWORD'), initial_response_ok=True) # 로그인 인증 print('login 완료') msg = MIMEText(str(content)) # 보낼 메시지 설정 msg['Subject'] = str('TITLE 입력') msg['To'] = receiver s.sendmail(senders, receiver, msg.as_string()) # 메일 보내기 s.quit() # 세션 종료
print('email 전송 완료') send_email() |
gmail의 아이디와 패스워드가 맞음에도
"smtplib.SMTPAuthenticationError" 에러가 나는 경우
아래 URL 통해 "보안 수준이 낮은 앱 허용: 사용" 체크
If you got "smtplib.SMTPAuthenticationError",
you need to check which allow low security app button in the below URL.
https://myaccount.google.com/lesssecureapps?pli=1
로그인 - Google 계정
하나의 계정으로 모든 Google 서비스를 Google 계정으로 로그인
accounts.google.com