python自动发送邮件

每天发日报的你是不是想找个机器人代替你做这些重复性无技术含量的工作,来试试python自动发送邮件吧

import smtplib
from email.header import Header
from email.mime.text import MIMEText# 文本文件
from email.mime.multipart import MIMEMultipart# html文件
from email.mime.image import MIMEImage# image文件
from email.mime.base import MIMEBase# 发送非文本数据
from email.mime.application import MIMEApplication  #添加excel文件调用接口API
from email import encoders

def send_email(SMTP_host, from_account, from_password, to_account, subject, content,filepath):
    # 1.实例化
    smtp=smtplib.SMTP_SSL(SMTP_host,465)

    # 2.连接服务器
    smtp.connect(SMTP_host,465)
    # smtp.ehlo()
    #smtp.starttls()

    # 3.配置发送邮件的用户名和密码
    smtp.login(from_account,from_password)

    # 4.配置发送内容msg
    msg = MIMEMultipart() # 发送者
    msg.attach(MIMEText(content, 'plain', 'utf-8'))# 主体文件内容
    msg['Subject'] = Header(subject, 'utf-8')# 标题
    msg['From'] = from_account
    # msg['To'] = to_account# 发送多人时不好设置此参数
    # msg['From'] = formataddr(["张三", "920664709@163.com"])# 设置自定的用户名
    # msg['To'] = formataddr(["李四", "920664709@163.com"])# 设置自定的用户名
    '''
    添加xlsx文件
    '''
    xlsxpart= MIMEApplication(open(filepath, 'rb').read())
    basename = "测试.xlsx"
    xlsxpart.add_header('Content-Disposition', 'attachment', filename=('gbk', '', basename))
    #注意:此处basename要转换为gbk编码,否则中文会有乱码。 
    msg.attach(xlsxpart)


    '''
    添加文本文件
    '''
    # att1 = MIMEText(open(r'E:\代码\Python文件\课件中心\数据可视化\电子商务数据集.csv', 'rb').read(), 'base64', 'utf-8')
    # att1["Content-Type"] = 'application/octet-stream'
    # # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    # att1["Content-Disposition"] = 'attachment; filename="test.csv"'
    # msg.attach(att1)

    '''
    添加html文件
    '''
    # att2 = MIMEText(open(r'E:\数据可视化\render.html', 'r' ,encoding='utf-8').read(), 'base64')
    # att2["Content-Type"] = 'application/octet-stream'
    # # 附件名称非中文时的写法
    # # att2["Content-Disposition"] = 'attachment; filename="Personas.html"'
    # # 附件名称为中文时的写法
    # att2.add_header("Content-Disposition", "attachment", filename=("gbk", "", "用户画像.html"))
    # msg.attach(att2)

    '''
    添加图片(不能修改文件名)
    '''
    # att3= MIMEImage(open("爱心.jpg",'rb').read())
    # att3.add_header('Content-ID', 'imageid')
    # msg.attach(att3)

    '''
    压缩文件
    '''
    att4=MIMEBase('application', 'octet-stream')
    # 加上必要的头信息:
    att4.add_header('Content-Disposition', 'attachment', filename=("gbk", "", '压缩包123.zip'))
    att4.add_header('Content-ID', '<0>')
    att4.add_header('X-Attachment-Id', '0')
    # 把附件的内容读进来:
    att4.set_payload(open("./压缩文件.zip",'rb').read())
    encoders.encode_base64(att4)
    msg.attach(att4)

    '''
    添加图片
    '''

    att5 = MIMEBase('application', 'octet-stream')
    # 加上必要的头信息:
    att5.add_header('Content-Disposition', 'attachment',filename=("gbk", "", '爱心.png'))
    att5.add_header('Content-ID', '<0>')
    att5.add_header('X-Attachment-Id', '0')
    # 把附件的内容读进来:
    att5.set_payload(open("./爱心.jpg", 'rb').read())
    encoders.encode_base64(att5)
    msg.attach(att5)


    # 5.配置发送邮箱,接收邮箱,以及发送内容
    smtp.sendmail(from_account, to_account, msg.as_string())

    # 6.关闭邮件服务
    smtp.quit()
mail_host='smtp.qq.com'# 服务器地址
sender='xxxxxxxxx@qq.com'# 发送用户
password='xxxxxxxxxx'# 口令-smtp授权码,非登陆密码
receivers=['xxxx@qq.com']# 接收用户可以1个或者多个
subject="【请知晓】关于Python的邮件自动发送"#邮件主题
filepath=r"d\test.xlsx"
content="""Dear All:
请查收2019年11月的自动化报表!
"""
send_email(mail_host,sender,password,receivers, subject, content,filepath)
# 主要参数:服务器地址  发送的邮箱  授权码  接收的邮箱  标题  主体内容
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诡途

你的鼓励是我 创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值