Python发送邮件

# 发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def send_mail(receivers, text):
    # 第三方 SMTP 服务
    mail_host = 'smtp.sina.com'  # 设置服务器
    mail_user = 'xxxxxx@sina.com'  # 用户名
    mail_pass = 'xxxxxx'  # 口令

    message = MIMEText(text, 'plain', 'utf-8')
    message['From'] = Header(mail_user)
    message['To'] = Header(','.join(receivers), 'utf-8')
    message['Subject'] = Header('天气提醒', 'utf-8')

    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
        log('连接服务器成功..')
        smtpObj.login(mail_user, mail_pass)
        log('登录邮箱服务器成功..')
        smtpObj.sendmail(from_addr=mail_user, to_addrs=receivers, msg=message.as_string())
        log('邮件发送成功..')
    except smtplib.SMTPException as e:
        log('无法发送邮件...' + e.message)

推荐阅读更多精彩内容