用Python透過Google的SMTP Server進行send email

用Python透過Google的SMTP Server進行send email

import smtplib
from email.mime.text import MIMEText
from email.header import Header

#SMTP Settings (Google as Sample)
smtpHost = "smtp.gmail.com"
smtpPort = 587
#SMTP Connection Account Settings
smtpUserName = "connectionAccount@gmail.com"
smtpUserPassword = "connectionAccountPassword"
#Email Account Setting
senderEmail = "senderEmail@gmail.com"
senderEmailPassword = "senderPassword"
receiverEmail = ["receiverOne@gmail.com", "receiverTwo@gmail.com"]
#Email Content and Message Setting

#Send Email As Plain Text
#message = MIMEText("Test Send Email By Python", "plain", "utf-8")

#Send Email in HTML Format
mail_msg = """
<p>Testing Sending Email By Python in HTML Format</p>
<p><b>This is a Bold Line</b></p>
<p><a href="https://www.google.com/">This is a link to Google</a></p>
"""
message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header("SenderName", "utf-8")
message['To'] = Header("ReceiverName", "utf-8")
subject = "Python SMTP Email Test"
message['Subject'] = Header(subject, "utf-8")
#smtpObj = smtplib.SMTP( [host [, post [, local_hostname]]])

try:
	#smtpObj = smtplib.SMTP(smtpHost, smtpPort)
	smtpObj = smtplib.SMTP()
	print "SMTP Init"
	smtpObj.connect(smtpHost, smtpPort)
	print "SMTP Connect"
	smtpObj.ehlo()
	smtpObj.starttls()
	smtpObj.ehlo()
	print "SMTP ehlo"
	smtpObj.login(smtpUserName, smtpUserPassword)
	print "SMTP Login"
	smtpObj.sendmail(senderEmail, receiverEmail, message.as_string())
	print "Email Sent"
except smtplib.SMTPException, error:
	print str(error)
	print "Error: Cannot Send Email"
 

若不想發送HTML內文的可將
message = MIMEText(mail_msg, 'html', 'utf-8')
取代成以下
message = MIMEText('Message', 'plain', 'utf-8')

需要注意的是帳戶的"低安全性應用程式存取權"有可能需要設定為開啟才可使用
詳細的可參考以下連結更改設定
低安全性應用程式和您的 Google 帳戶