python(9):python发送邮件

邮件发送测试报告

前置条件:开通QQ邮箱第三方登录,并拿到密码;

步骤1:编写测试代码,先发送一个文本的邮件

在sample文件中编写线性代码:

步骤2:编写一个带附件的邮件

 代码示例:

import os
import smtplib
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders


# 先准备附件
current_path = os.path.dirname(__file__)
smtp_file_path = os.path.join(current_path,'..','reports/禅道UI自动化测试V1.0.zip')

# 登录账号密码,发件人和收件人,邮件标题,邮件内容,邮件服务器域名
# 开通QQ邮箱第三方登录,并拿到密码
smtp_server = 'smtp.qq.com'
smtp_sender = '2798413941@qq.com'
smtp_senderpassword = 'gmyiulsymlzvdcdd'
smtp_receiver = 'zz1196999489@163.com'
smtp_cc = '1196999489@qq.com'
smtp_subject = '禅道自动化测试报告'
smtp_body = '来自禅道自动化测试的结果'
smtp_file = smtp_file_path

# 发压缩包的邮件
msg = MIMEMultipart()
with open(smtp_file,'rb') as f:
    mime = MIMEBase('zip','zip',filname=smtp_file.split('/')[-1])
    mime.add_header('Content-Disposition', 'attachment',
                    filename=('gb2312', '',smtp_file.split('/')[-1]))
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    mime.set_payload(f.read())
    encoders.encode_base64(mime)
    msg.attach(mime)

msg.attach(MIMEText(smtp_body,'html','utf-8'))
msg['from'] = smtp_sender
msg['to'] = smtp_receiver
msg['Cc'] = smtp_cc
msg['subject'] = smtp_subject

# # 邮件文本消息
# msg = MIMEText(smtp_body,'html','utf-8')  # 邮件消息对象
# msg['from'] = smtp_sender
# msg['to'] = smtp_receiver
# msg['Cc'] = smtp_cc
# msg['subject'] = smtp_subject

# 发送邮件
smtp = smtplib.SMTP()
smtp.connect(smtp_server)
smtp.login(user=smtp_sender,password=smtp_senderpassword)
smtp.sendm
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值