欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python—发送邮件

程序员文章站 2022-06-22 11:09:18
一、发送纯文本邮件 import smtplib from email.mime.text import MIMEText from email.header import Header subject = "邮件标题" # 邮件的主题 content = "邮件内容" # 邮件的内容 sender ......

一、发送纯文本邮件

import smtplib
from email.mime.text import mimetext
from email.header import header

subject = "邮件标题"      # 邮件的主题
content = "邮件内容"      # 邮件的内容
sender = "1548429568@qq.com"        # 发件人
password = "qnrwndesjxmmijce"       # 刚才我们在qq邮箱里设置的授权密码
receiver = "3235403488@qq.com"      # 收件人

message = mimetext(content, "plain", "utf-8")  # 发送的内容,内容的格式类型(plain与html),内容的编码方式
message["from"] = sender          # 发送邮箱
message["to"] = receiver          # 接收邮箱
message["subject"] = subject      # 邮件标题
# message['subject'] = header(subject, 'utf-8')  # subject
# message = '\n'.join(['from: {}'.format(sender), 'to: {}'.format(receiver), 'subject: {}'.format(subject), '', content])

# smtp = smtplib.smtp("smtp.qq.com", 25)       # smtp:普通的邮件发送形式
smtp = smtplib.smtp_ssl("smtp.qq.com", 465)    # smtp_ssl:qq邮箱的smtp服务器(端口465或587)
smtp.set_debuglevel(1)                         # 用set_debuglevel(1)就可以打印出和smtp服务器交互的所有信息
smtp.login(sender, password)                   # 登录smtp服务器,输入发送邮箱和密码
smtp.sendmail(sender, receiver, message.as_string())
smtp.quit()
smtp.close()

二、发送html格式的邮件

python发送html格式的邮件与发送纯文本消息的邮件不同之处就是将mimetext中_subtype设置为html。

content = '<h1>hello</h1>' + '<p>send by <a href="http://www.python.org">python</a>...</p>'
message = mimetext(content, "html", "utf-8")

三、发送文本中带图片的邮件

import smtplib
from email.mime.text import mimetext
from email.mime.image import mimeimage
from email.mime.multipart import mimemultipart

subject = "图片邮件测试"
content = """<p>python 邮件发送测试...</p><p><img src="cid:image1"></p>"""
sender = "1548429568@qq.com"    # 发送人,换成自己的!
password = "qnrwndesjxmmijce"   # 授权码,换成自己的!
receiver = "3235403488@qq.com"  # 收件人

message = mimemultipart("related")   # 构造一个mimemultipart对象代表邮件本身。related 表示使用内嵌资源的形式 将邮件发送给对方
message["from"] = sender
message["to"] = receiver
message["subject"] = subject

# msgalternative = mimemultipart('alternative')               # 接收者的别名
# msgalternative.attach(mimetext(content, 'html', 'utf-8'))   # 添加文本
# message.attach(msgalternative)
message.attach(mimetext(content, 'html', 'utf-8'))   # 一步到位,不用上面三行代码

# ---------------发送图片的第一种方式-----------------------
with open(r'c:\users\bt.cn\desktop\004.jpg', 'rb') as fp:   # 二进制模式读取图片
    msgimage = mimeimage(fp.read())
msgimage.add_header("content-id", "<image1>")  # 定义图片id,在html文本中引用
message.attach(msgimage)                       # 添加图片到邮箱信息中去

# ---------------发送图片的第二种方式-----------------------
# msgimage = mimetext(open(r'c:\users\bt.cn\desktop\004.jpg', 'rb').read(), 'base64', 'utf-8')
# msgimage['content-disposition'] = 'attachment;filename="happy.png"'
# message.attach(msgimage)

smtp = smtplib.smtp_ssl("smtp.qq.com", 465)
smtp.set_debuglevel(1)
smtp.login(sender, password)
try:
    smtp.sendmail(sender, receiver, message.as_string())
    smtp.quit()
    smtp.close()
except exception as e:
    print("邮件发送失败,错误原因[{0}]".format(str(e)))
print("邮件发送成功")

四、发送