发送电子邮件信息

smtplib模块发送邮件

import smtplib
from email.mime.text import MIMEText
from smtplib import SMTP_SSL

to_list = ['xxx@163.com', 'xxx@qq.com']
server_host = 'smtp.163.com'

username = '你的账户'
password = '你的密码|授权码'


def send(to_list, sub, content):
    '''
    :param to_list: 收件人邮箱
    :param sub: 邮件标题
    :param content: 内容
    '''
    me = "manager" + "<" + username + ">"
    # _subtype 可以设为html,默认是plain
    msg = MIMEText(content, _subtype='html')
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ','.join(to_list)
    try:
        # server = smtplib.SMTP_SSL()     465 SSL端口
        server = smtplib.SMTP()
        server.connect(server_host)
        server.login(username, password)
        server.sendmail(me, to_list, msg.as_string())
        server.close()
    except Exception as e:
        print(str(e))


if __name__ == '__main__':
    send(to_list, "这个是一个邮件", "<h1>Hello, It's test email.</h1>")

Last updated