python-发送邮件

2017/7/30 posted in  python
#coding:utf-8
__author__ = 'zhangbo'

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# reciver: 收件人的邮箱,为列表,支持多个收件人 
# title: 邮件标题
# html: 发送的html代码
def send_html_email(reciver, title, html):
    # 发件人的邮箱
    me = "sender@mailserver.com"
    msg = MIMEMultipart('alternative')
    msg['Subject'] = title
    msg['From'] = me
    msg['To'] = ','.join(reciver)

    part2 = MIMEText(html, 'html', 'utf-8')
    msg.attach(part2)

    try:
        s = smtplib.SMTP()
        # 发件服务器
        s.connect("smtp.mailserver.com", 25)
        # 登陆发件人的账号和密码
        s.login(me, 'password')
        s.sendmail(me, reciver, msg.as_string())
        s.quit()
        return True
    except Exception as e:
        print e
        return False