使用python编写脚本向飞书邮箱发送邮件

在这里插入图片描述
然后选择Windows 电脑或者其它设备生成

有两个有用的信息一个是密码passwd ,一个是smtplib.SMTP_SSL(“smtp.feishu.cn”, 465)这里面的内容

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

msg_from = 'xxx@xxx.cn'  # 发送方邮箱
passwd = 'xxxxxxxxxx'  # 填入发送方邮箱的授权码
msg_to = 'xxx@xxx.cn'  # 收件人邮箱,我是自己发给自己
text_content = "test fail!!!"  # 发送的邮件内容
        
def send_email(self, msg_from, passwd, msg_to, text_content, file_path=None):
    msg = MIMEMultipart()
    subject = "自动化测试结果"  # 主题
    text = MIMEText(text_content)
    msg.attach(text)

    file_path = r'xxx1.txt'  #如果需要添加附件,就给定路径
    if file_path:  # 最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好
        docFile = file_path
        docApart = MIMEApplication(open(docFile, 'rb').read())
        docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
        msg.attach(docApart)

        docFile = r'xxxx2.txt'
        docApart = MIMEApplication(open(docFile, 'rb').read())
        docApart.add_header('Content-Disposition', 'attachment', filename=docFile)
        msg.attach(docApart)
        print('发送附件到', msg_to)
    msg['Subject'] = subject
    msg['From'] = msg_from
    msg['To'] = msg_to
    try:
        s = smtplib.SMTP_SSL("smtp.feishu.cn", 465)
        s.login(msg_from, passwd)
        s.sendmail(msg_from, msg_to, msg.as_string())
        print("发送成功")
    except smtplib.SMTPException as e:
        print("发送失败")
    finally:
        s.quit()


send_email(msg_from, passwd, msg_to, text_content)

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大饼酱人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值