Post

SMTP

SMTP(Simple Mail Transfer Protocol)

SMTP

SMTP(Simple Mail Transfer Protocol)

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. smtplib 모듈은 SMTP 또는 ESMTP 수신 대기 데몬이 있는 모든 인터넷 기기에 메일을 보내는 데 사용할 수 있는 SMTP 클라이언트 세션 개체를 정의한다. docs.python - smtplib

1
class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None)

SMTP 인스턴스는 SMTP 연결을 캡슐화

  • SMTP 및 ESMTP 작업의 전체 레파토리를 지원하는 메서드가 있다. 선택적 호스트 및 포트 매개변수가 지정된 경우, 초기화 중에 이러한 매개변수를 사용하여 SMTP connect() 메서드가 호출 지정된 경우, local_hostname은 HELO/EHLO 명령에서 로컬 호스트의 FQDN으로 사용
  • 그렇지 않으면, 로컬 호스트 이름은 socket.getfqdn()을 사용하여 찾는다. connect() 호출이 성공 코드 이외의 값을 반환하면, SMTPConnectError가 발생 선택적 timeout 매개변수는 연결 시도와 같은 차단 작업에 대한 초 단위의 제한 시간을 지정(지정되지 않은 경우, 전역 기본 timeout 설정이 사용). 제한 시간이 경과하면 TimeoutError가 발생.

예시 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
##################### Hard Starting Project ######################
import os
import smtplib

from dotenv import load_dotenv

load_dotenv()

# 내 이메일 ID와 비밀번호 변수 지정
my_email = "songyeongrok11@gmail.com"
password = os.environ.get("PRIVATE_KEY")

# email send
with smtplib.SMTP("smtp.gmail.com") as connection:
	# SMTP 연결을 TLS (Transport Layer Security) 모드로 전환합니다. 
    connection.starttls()
    # login(user,password,*,initial_response_ok=True) : 인증이 필요한 SMTP 서버에 로그인합니다. 인자는 인증할 사용자 이름과 비밀번호입니다. 
    connection.login(my_email, password) 
    # 
    connection.sendmail(from_addr=my_email,
    					to_addrs=birthday_person["email"],
                        msg=f"Subject:HappyBirthday\n\n{contents}")
                        #msg= "제목:{내용}\n\n{본문 내용}"
This post is licensed under CC BY 4.0 by the author.