#!/usr/bin/python
#-*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sender = 'x@163.com'
to = ["y@qq.com", "z@qq.com"]  # 多个收件人的写法
subject = 'From Python Email test'
smtpserver = 'smtp.163.com'
username = 'x'
password = 'passwordfa'
mail_postfix = '163.com'
contents = '这是一个Python的测试邮件,收到请勿回复,谢谢!!'



def send_mail(to_list, sub, content):
    me = sender
    msg = MIMEText(content, _subtype='plain', _charset='utf-8')
    msg['Subject'] = subject
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(smtpserver)
        server.login(username, password)
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False
if __name__ == '__main__':
    if send_mail(to, subject, contents):
        print "邮件发送成功! "
    else:
        print "失败!!!"
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐