利用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')
没有评论:
发表评论