728x90
728x90
파이썬을 이용하여 이메일 보내는 방법 (smtplib, email)
들어가며
- 파이썬 코드를 이용하여 간단하게 이메일을 보내보자.
- 파이썬에 기본으로 내장되어 있는
smtplib
모듈과email
모듈을 이용할 것이다.

방법
필요한 모듈 불러오기
- 이메일을 불러오기 위해 필요한 모듈을 불러온다.
- 파이썬에 기본으로 내장되어 있는
smtplib
모듈과email
모듈을 불러올 것이다.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.application import MIMEApplication
메일 수신자, 발신자 설정하기
recipients
변수에 수신자 이메일들을 넣어준다.
recipients = ["수신자1_이메일", "수신자2_이메일"] message = MIMEMultipart() message['Subject'] = "메일 발송 테스트" # 메일 제목 message['From'] = "helloworld@naver.com" # 메일 발신자 (예: 네이버) message['To'] = ",".join(recipients) # 메일 수신자
메일 내용 입력하기
- HTML 형식으로 메일을 보낼 것이다.
- 메일의 내용을 HTML 형식으로
message_content
변수에 넣어준다.
message_content = """ <html> <body> <h2>메일 전송 테스트</h2> <p>메일 전송 테스트입니다</p> </body> </html> """ imetext = MIMEText(message_content, 'html') message.attach(mimetext)
메일 발신자 SMTP 아이디/비밀번호 입력 및 전송하기
- 메일 발신자의 SMTP 아이디와 비밀번호를 입력한다.
- 아래의 코드에는 발신자의 SMTP 계정이 네이버(
smtp.naver.com
)로 설정한 경우이다.- 네이버 아이디를
email_id
변수에, 네이버 비밀번호를email_pw
에 넣어준다.
- 네이버 아이디를
sendmail
함수를 통해 메일이 전송된다.
email_id = "발신자_SMTP_ID" email_pw = "발신자_SMTP_PW" server = smtplib.SMTP('smtp.naver.com', 587) # 예: 네이버 server.ehlo() server.starttls() server.login(email_id, email_pw) server.sendmail(message['From'], recipients, message.as_string()) # 메일 전송 server.quit()
전체 코드
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.application import MIMEApplication # 메일 수신자, 발신자 설정하기 recipients = ["수신자1_이메일", "수신자2_이메일"] message = MIMEMultipart() message['Subject'] = "메일 발송 테스트" # 메일 제목 message['From'] = "helloworld@naver.com" # 메일 발신자 message['To'] = ",".join(recipients) # 메일 수신자 # 메일 내용 입력하기 message_content = """ <html> <body> <h2>메일 전송 테스트</h2> <p>메일 전송 테스트입니다</p> </body> </html> """ imetext = MIMEText(message_content, 'html') message.attach(mimetext) # 메일 발신자 SMTP 아이디, 비밀번호 입력 및 메일 보내기 email_id = "발신자_SMTP_ID" email_pw = "발신자_SMTP_PW" server = smtplib.SMTP('smtp.naver.com', 587) # 예: 네이버 server.ehlo() server.starttls() server.login(email_id, email_pw) server.sendmail(message['From'], recipients, message.as_string()) # 메일 전송 server.quit()
참고
smtlib
모듈에 관한 자세한 내용은 아래의 공식 레퍼런스 문서를 참고한다.
smtplib — SMTP protocol client
Source code: Lib/smtplib.py The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon. For details of SMTP...
docs.python.org
email
모듈에 관한 자세한 내용은 아래의 공식 레퍼런스 문서를 참고한다.
email — An email and MIME handling package
Source code: Lib/email/__init__.py The email package is a library for managing email messages. It is specifically not designed to do any sending of email messages to SMTP ( RFC 2821), NNTP, or othe...
docs.python.org
728x90
728x90
'Programming > Python' 카테고리의 다른 글
[Python] self (0) | 2023.11.29 |
---|---|
[Python] ORM(Object Relational Mapping) 라이브러리 사용해보기 (SQLAlchemy) (0) | 2023.11.14 |
[Python] HTML 코드를 이미지로 변환하는 방법 (Html2Image) (0) | 2023.11.06 |
[Python] 파이썬을 이용하여 텔레그램(Telegram) 메시지 보내는 방법 (0) | 2023.11.06 |
[Python] 파이썬에서 / 연산자와 // 연산자의 차이점 (0) | 2023.10.09 |
[Python] 맵(Map)과 리스트(List)의 차이점 (0) | 2023.10.08 |
[Python] try except 문을 사용할 때, 예외(Exception) 정보 출력하는 방법 (0) | 2023.09.08 |
[Python] 삼항 연산자(Ternary Conditional Operator) (0) | 2023.08.23 |