我使用 smtplib 来做到这一点。
# here i check if i send succesfully.
server = smtplib.SMTP(HOST)
# server.login(username, password) # optional
try:
toaddr = TO
cc = [CC]
bcc = ['']
toaddrs = [toaddr] + cc + bcc
failed = server.sendmail(FROM, toaddrs, msg.as_string())
server.close()
statusemail= "Email enviado com sucesso para %s" % TO
print statusemail
return statusemail
except Exception, e:
errorMsg = "Unable to send email. Error: %s" % str(e)
return errorMsg
##############################################################
#example code to test smtplib
import smtplib
# Specifying the from and to addresses
fromaddr = 'fromuser@gmail.com'
toaddrs = 'touser@gmail.com'
# Writing the message (this message will appear in the email)
msg = 'Enter you message here'
# Gmail Login
username = 'username'
password = 'password'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

来源:链接