前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python自动发送邮件

python自动发送邮件

作者头像
诡途
发布2022-05-09 18:52:52
5860
发布2022-05-09 18:52:52
举报
文章被收录于专栏:诡途的python路诡途的python路

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

代码语言:javascript
复制
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)
# 主要参数:服务器地址  发送的邮箱  授权码  接收的邮箱  标题  主体内容
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-11-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档