2015年9月6日 星期日
Python-如何利用Gmail發信
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('寄件人@gmail.com','Gmail應用程式密碼','收件人@gmail.com','subject','Gino Test')
Ps, 記得先至Gmail申請應用程式密碼,才能順利發信。
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言