Python发送邮件很简单,只需要用到smtplib库和email库

实现如下,将其包装成函数:

#!/usr/bin/env python
# coding: utf-8
import smtplib
from email.mime.text import MIMEText


def mail(email_to, email_subject, content):  # 参数分别为收件人、主题、内容
    sender = 'xxxx@163.com'  # 发件人
    msg = MIMEText(content)
    msg['from'] = sender
    msg['to'] = email_to
    msg['subject'] = email_subject
    s = smtplib.SMTP('smtp.163.com')
    s.login('xxxx@163.com', 'password')  # 邮箱账号和密码
    s.sendmail(sender, email_to, msg.as_string())
    s.quit()

调用函数的方法:

mail('xxxx@qq.com', '测试 test', '  前面有两个空格!')