Python邮件发送


1.发送文本邮件

# 内容:1.用Python代码发文本邮件 2.发送html邮件 发送附件 图片

# smtp发送邮件 smtp邮件传输协议 smtplib库: 进行简单的封装 简单

import smtplib

## ①连接邮箱服务器
# 邮局 smtplib.SMTP_SSL(邮箱连接地址,端口号) smtp.xx.com
# 163邮箱;smtp.163.com 端口号
# qq邮箱; smtp.qq.com 端口号 465或者587
from email.mime.text import MIMEText

con = smtplib.SMTP_SSL('smtp.qq.com',465)
print(con)

## ②登录邮箱
# 用户名和密码 密码用授权码 qq邮箱--设置--账户--POP3/SMTP服务开启 授权码
con.login(user='834628301@qq.com',password='xxx')

## ③发送者账号
sender = '834628301@qq.com'
## ④接受者账号
reciever = '834628301@qq.com'
## ⑤发送内容  _text 内容, _subtype='plain' 文本, _charset=None 字符集
htmlContent = "<a href='http://www.baidu.com'>我是html代码</a>"
message = MIMEText(htmlContent,'html','utf-8')
# message = MIMEText('小幸运你好,我是渣渣秋','plain','utf-8')
## ⑥设置头部内容
# 标题 发件人 收件人
# 标题
message['Subject'] = '发送给小幸运的一封信'
# 发送人
message['from'] = sender
message['to'] = reciever

## ⑦发送邮件
#邮件有没有发送出去 有没有问题,担心发送的邮件有问题
try:
    # 发送邮件
    con.sendmail(sender,reciever,message.as_string())
    print('邮件发送成功')
except Exception as e:
    print('没有办法发送邮件,报错了')


# 发送html格式的邮件
# 发送测试报告,发送html的测试报告 发送给测试相关人员

2.发送html测试报告给到相关测试人员

import smtplib
from email.mime.text import MIMEText

con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='834628301@qq.com',password='xxx')
# 发送者账号
sender = '834628301@qq.com'
# 接受者账号
reciever =['834628301@qq.com','yunshaliying@sina.com']

# 发送html文件里面的内容 读取出来
with open(r'./files/2020-11-16 21-15-18test_report.html','rb') as f:
    htmlContent = f.read()
    message = MIMEText(str(htmlContent,encoding='utf-8'),'html','utf-8')

# 标题
message['Subject'] = '发送给小幸运的一封信'
# 发送人
message['from'] = sender
message['to'] = ';'.join(reciever)

con.sendmail(sender,reciever,message.as_string())

3.发送html附件

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='834628301@qq.com',password='xxx')
# 发送者账号
sender = '834628301@qq.com'
# 接受者账号
reciever ='834628301@qq.com'

# 发送内容
# 附件实例 盒子
message = MIMEMultipart()

# 拿到内容
with open(r'./files/2020-11-16 21-15-18test_report.html','rb') as f:
    content = f.read()
    # 内容写在信纸上
    files2 = MIMEText(content,'base64','utf-8')
    # 纸取个名字 不要用中文去写 用英文
    files2['Content-Disposition'] = 'attachment;filename="2.html"'
    # 把家书放到盒子中寄出去
    message.attach(files2)

    msg = MIMEText('正文内容','plain','utf-8')
    message.attach(msg)

# 标题
message['Subject'] = '这是一封家书'
# 发送人
message['from'] = sender
message['to'] = reciever

con.sendmail(sender,reciever,message.as_string())

# 注意:在日常工作中,发送html的测试报告,要压缩成压缩文件之后再发送
# 手动压缩 代码压缩 zipfile

4.发送图片

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

con = smtplib.SMTP_SSL('smtp.qq.com','465')
con.login(user='834628301@qq.com',password='xxx')

# 发送者账号
sender = '834628301@qq.com'
# 接受者账号
reciever =['834628301@qq.com']

# 抄送者账号
cs = ['yunshaliying@sina.com']

# 发送图片
message = MIMEMultipart()
# 图片内容
with open('./files/image.jpg','rb') as f:
    image1 = f.read()
    # 放在纸上,图片
    image_data = MIMEImage(image1)
    image_data['Content-Disposition'] = 'attachment;filename="2.jpg"'
    message.attach(image_data)
    msg = MIMEText('图片的正文','plain','utf-8')
    message.attach(msg)

# 标题
message['Subject'] = '这是一封家书'
# 发送
message['from'] = sender
message['to'] = ';'.join(reciever)
message['cc'] = ';'.join(cs)

con.sendmail(sender, reciever+cs, message.as_string())

5.zmail

# 安装: pip install zmail
import zmail

# subject 标题
# Content_text 邮件文本内容
# Content_html 内容
# Attachments: 附件
# 发送人
sender = {'username':'834628301@qq.com','password':'xxx'}

# 登录邮箱
server = zmail.server(sender['username'],sender['password'])

# 邮件内容
mail_content = {
    'subject':'我是标题',
    # 'Content_text':'我是邮件内容http://www.baidu.com',
    'Content_html':'<a href="http://www.baidu.com">嗨,你好</a>',
    # 'Attachments':'./files/2020-11-16 21-15-18test_report.html'
    'Attachments':'./files/test.docx'
}
# 收件人
reciever=['834628301@qq.com']

# 发送邮件
server.send_mail(reciever,mail_content,cc=['yunshaliying@sina.com'])

# 注意
# Content_text和Content_html只能选择一个使用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

司小幽

真诚赞赏,手留余香。

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

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

打赏作者

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

抵扣说明:

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

余额充值