Skip to content

Instantly share code, notes, and snippets.

@selfboot
Created March 21, 2017 11:34
Show Gist options
  • Save selfboot/e0a0f4e8dccb3b7d3d226fd753dfa7ca to your computer and use it in GitHub Desktop.
Save selfboot/e0a0f4e8dccb3b7d3d226fd753dfa7ca to your computer and use it in GitHub Desktop.
Python 简单发送邮件
#! /usr/local/bin/python
SMTPserver = 'smtp.sina.com'
sender = '<[email protected]>'
destination = ['[email protected]']
USERNAME = "[email protected]"
PASSWORD = "*******"
# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'
content="""\
Test message
"""
subject="Sent from Python"
import sys
import os
import re
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
# old version
# from email.MIMEText import MIMEText
from email.mime.text import MIMEText
try:
msg = MIMEText(content, text_subtype)
msg['Subject']= subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
conn = SMTP(SMTPserver, 465)
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.quit()
print "Send successfully!"
except Exception, exc:
sys.exit( "mail failed; %s" % str(exc) ) # give a error message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment