import smtplib
import string
from email.mime.text import MIMEText
from email.utils import formataddr
host = ‘smtp.263.net‘ ##定义smtp主机
SUBJECT = "test smtplib" #定义邮件主题
TO = "[email protected]" ##邮件接收人
FROM = "[email protected]" ##邮件发送人
text = "content" #邮件内容
###拼接邮件主体内容
BODY = "\r\n".join(("From %s:" % FROM,"To: %s" % TO,"Subject:%s" % SUBJECT,"",text)).encode("utf-8")
server = smtplib.SMTP() #创建smtp对象
server.connect(host,"25") ##通过connect方法连接smtp主机
server.starttls() ##启动安全传输模式
server.login("[email protected]",‘[email protected]‘) ##邮箱账号登录
server.sendmail(FROM,[TO],BODY)
server.quit() ##断开smtp连接