利用163邮箱发邮件 带附件附件

import os
import smtplib
from email.header import Header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def sentemail(receiver):
    # 设置发件服务器地址
    host = 'smtp.163.com'
    # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式
    port = 465
    # 设置发件邮箱,一定要自己注册的邮箱
    sender = '17682303516@163.com'
    # 设置发件邮箱的密码,163邮箱的授权码,等会登陆会用到
    pwd = 'yu17682303516'
    # 设置邮件接收人,可以是扣扣邮箱
    receiver0 = [sender, sender, sender]  # 写多个收件人
    # 设置邮件正文,这里是支持HTML的
    body = '<h1> 请扫描二维码 登入 WeGame </h1>'
    # 设置正文为符合邮件格式的HTML内容
    msg = MIMEText(body, 'html')
    message = MIMEMultipart()
    # 设置邮件标题
    message['subject'] = Header('请扫描二维码 登入 WeGame', 'utf-8')
    # 设置发送人
    message['from'] = Header(sender, 'utf-8')
    # 设置接收人
    message['to'] = Header(sender, 'utf-8')
    message.attach(msg)
    # 构造附件1,传送当前目录下的 filename 文件
    path = os.path.dirname(os.path.abspath(__file__))
    filename = '{}/iamges/erweima.png'.format(path)
    # filename = '{}/iamges/程序接入.doc'.format(path)
    att1 = MIMEImage(open(filename, 'rb').read(), 'utf-8')
    # att1 = MIMEText(open(filename, 'rb').read(), 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    att1["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)  # ewm.png
    message.attach(att1)
    # 注意!如果是使用SSL端口,这里就要改为SMTP_SSL
    s = smtplib.SMTP_SSL(host, port)
    s.login(sender, pwd)
    # 发送邮件!
    s.sendmail(sender, receiver0, message.as_string())
    print('邮件发送成功')


if __name__ == '__main__':
    sentemail('1064145110')