logo

Python SMTP를 사용하여 이메일 보내기

Simple Mail Transfer Protocol(SMTP)은 Python을 사용하여 이메일 전송을 처리하는 프로토콜로 사용됩니다. 이메일 서버 간에 이메일을 라우팅하는 데 사용됩니다. 사용자가 다른 사람에게 메일을 보낼 수 있도록 하는 응용 프로그램 계층 프로토콜입니다. 수신자는 프로토콜을 사용하여 이메일을 검색합니다. POP(우체국 프로토콜) 그리고 IMAP(인터넷 메시지 액세스 프로토콜) .

Python SMTP를 사용하여 이메일 보내기

서버가 클라이언트의 TCP 연결을 수신하면 포트 587에서 연결을 시작합니다.

파이썬은 smtplib 인터넷 시스템에 이메일을 보내는 데 사용되는 SMTP 클라이언트 세션 개체를 정의하는 모듈입니다. 이를 위해서는 다음을 수입해야 합니다. smtplib import 문을 사용하는 모듈.

우분투 빌드 필수
 $ import smtplib 

이메일 전송에는 SMTP 개체가 사용됩니다. 다음 구문은 smtplib 개체를 만드는 데 사용됩니다.

 import smtplib smtpObj = smtplib.SMTP(host, port, local_hostname) 

다음 매개변수를 허용합니다.

    주인:SMTP 서버를 실행하는 시스템의 호스트 이름입니다. 여기에서 ( https://www.javatpoint.com ) 또는 localhost와 같은 서버의 IP 주소를 지정할 수 있습니다. 선택적 매개변수입니다.포트:호스트 시스템이 SMTP 연결을 수신하는 포트 번호입니다. 기본값은 25입니다.로컬_호스트 이름:SMTP 서버가 로컬 시스템에서 실행 중인 경우 로컬 시스템의 호스트 이름을 언급할 수 있습니다.

SMTP 개체의 sendmail() 메서드는 메일을 원하는 시스템으로 보내는 데 사용됩니다. 구문은 아래와 같습니다.

 smtpObj.sendmail(sender, receiver, message) 

 #!/usr/bin/python3 import smtplib sender_mail = '[email protected]' receivers_mail = ['[email protected]'] message = '''From: From Person %s To: To Person %s Subject: Sending SMTP e-mail This is a test e-mail message. '''%(sender_mail,receivers_mail) try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender_mail, receivers_mail, message) print('Successfully sent email') except Exception: print('Error: unable to send email') 

Gmail에서 이메일 보내기

Gmail SMTP 서버를 사용하여 이메일을 보내는 경우가 있습니다. 이 경우 포트 587과 함께 로컬 호스트를 사용하는 대신 Gmail을 SMTP 서버로 전달할 수 있습니다.

다음 구문을 사용하십시오.

 $ smtpObj = smtplib.SMTP('gmail.com', 587) 

여기서는 Gmail 사용자 이름과 비밀번호를 사용하여 Gmail 계정에 로그인해야 합니다. 이를 위해 smtplib는 보낸 사람의 사용자 이름과 비밀번호를 허용하는 login() 메서드를 제공합니다.

Gmail을 사용하는 경우 Gmail에서 보안 수준이 낮은 앱에 대한 액세스를 요청할 수 있습니다. 이 기능이 작동하려면 이 기능을 일시적으로 켜야 합니다.

Python SMTP를 사용하여 이메일 보내기

다음 예를 고려하십시오.

 #!/usr/bin/python3 import smtplib sender_mail = '[email protected]' receivers_mail = ['[email protected]'] message = '''From: From Person %s To: To Person %s Subject: Sending SMTP e-mail This is a test e-mail message. '''%(sender_mail,receivers_mail) try: password = input('Enter the password'); smtpObj = smtplib.SMTP('gmail.com',587) smtpobj.login(sender_mail,password) smtpObj.sendmail(sender_mail, receivers_mail, message) print('Successfully sent email') except Exception: print('Error: unable to send email') 

이메일로 HTML 보내기

HTML을 보내기 위해 MIME 버전, 콘텐츠 유형 및 문자 집합을 지정하여 메시지의 HTML 형식을 지정할 수 있습니다.

다음 예를 고려하십시오.

 #!/usr/bin/python3 import smtplib sender_mail = &apos;[email protected]&apos; receivers_mail = [&apos;[email protected]&apos;] message = &apos;&apos;&apos;From: From Person %s To: To Person %s MIME-Version:1.0 Content-type:text/html Subject: Sending SMTP e-mail <h3>Python SMTP</h3> <strong>This is a test e-mail message.</strong> &apos;&apos;&apos;%(sender_mail,receivers_mail) try: smtpObj = smtplib.SMTP(&apos;localhost&apos;) smtpObj.sendmail(sender_mail, receivers_mail, message) print(&apos;Successfully sent email&apos;) except Exception: print(&apos;Error: unable to send email&apos;)