利用Python Gmail SMTP 寄送Email
其中
'GmailUserName' 為寄件者Gmail的帳號
'GmailPassword'為寄件者Gmail密碼
'receive@XXX.XXX.XXX'為接收者帳號,一次只能一個,若要寄多個,則必須用成List來依序寄出
'subject'寄郵件的標題
'testcontent'為郵件的內容
程式內容如下:
import os
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def sendGmailSmtp(strGmailUser,strGmailPassword,strRecipient,strSubject,strContent):
strMessage = MIMEMultipart()
strMessage['From'] = strGmailUser
strMessage['To'] = strRecipient
strMessage['Subject'] = strSubject
strMessage.attach(MIMEText(strContent))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(strGmailUser, strGmailPassword)
mailServer.sendmail(strGmailUser, strRecipient, strMessage.as_string())
mailServer.close()
return 'send successed'
print sendGmailSmtp('GmailUserName','GmailPassword','receive@XXX.XXX.XXX','subject','testcontent')