Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python을 사용하여 HTML 이메일 보내기

<시간/>

Python을 사용하여 문자 메시지를 보내면 모든 내용이 단순 텍스트로 처리됩니다. 텍스트 메시지에 HTML 태그를 포함하더라도 간단한 텍스트로 표시되며 HTML 구문에 따라 HTML 태그의 서식이 지정되지 않습니다. 그러나 Python은 HTML 메시지를 실제 HTML 메시지로 보낼 수 있는 옵션을 제공합니다.

전자 메일 메시지를 보내는 동안 HTML 전자 메일을 보낼 Mime 버전, 콘텐츠 유형 및 문자 집합을 지정할 수 있습니다.

예시

다음은 HTML 콘텐츠를 이메일로 보내는 예제입니다. 한 번 시도해 보세요 -

#!/usr/bin/python
import smtplib
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"