Python smtplib发邮件

常用邮箱SMTP、POP3域名及其端口号

发送普通文本内容的邮件

import smtplib
from email.header import Header
from email.mime.text import MIMEText

# smtp服务器信息
smtp_server = 'smtp.163.com'
server_port = 465

# 发送方信息
sender = '发送的邮箱地址'
password = '发送邮箱的smtp授权码'

# 收件人地址,列表可发给多人
receivers = ['123456@qq.com', '3123123@qq.com', '12312@qq.com']

# 邮箱的正文内容,参数分别为内容,格式(plain 为纯文本),编码
msg = MIMEText('Here is content.', 'plain', 'utf-8')

# 邮件头信息
msg['From'] = Header(sender)  # 发件人
msg['To'] = Header(', '.join(receivers))  # 发到哪里,参数为字符串
msg['Subject'] = Header('This is subject')  # 邮件标题

# 发送邮件的操作
try:
    server = smtplib.SMTP_SSL()  # SSL加密传输
    server.connect(smtp_server, server_port)  # 连接smtp,服务器和端口
    server.login(sender, password)  # 登录发信邮箱
    server.sendmail(sender, receivers, msg.as_string())  # 发送邮件
    server.quit()  # 关闭服务器
    print('发送成功')

except smtplib.SMTPException:
    print('发送失败')


发送html格式的邮件

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

发送带有附件的邮件

发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。

import smtplib
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# smtp服务器信息
smtp_server = 'smtp.163.com'
server_port = 465

# 发送方信息
sender = '发送的邮箱地址'
password = '发送邮箱的smtp授权码'

# 收件人地址,列表可发给多人
receivers = ['969831239@qq.com', '124123989@qq.com']

# 实例化,先添加正文内容
msg = MIMEMultipart()
msg.attach(MIMEText('This is content.', 'plain', 'utf-8'))

# 添加附件1
att1 = MIMEApplication(open('tips.py', 'rb').read(), 'utf-8')
att1['Content-Disposition'] = 'attachment; filename="down.py"'    # filename随便起,是接收到的附件显示名称
# att1["Content-Type"] = 'application/octet-stream'    Content-Type默认为application/octet-stream
msg.attach(att1)

# 添加附件2
att2 = MIMEApplication(open('source.jpg', 'rb').read(), 'utf-8')
att2['Content-Disposition'] = 'attachment; filename="girl.jpg"'
msg.attach(att2)

# 邮件头信息
msg['From'] = Header(sender)  # 发件人
msg['To'] = Header(', '.join(receivers))  # 发到哪里,参数为字符串
msg['Subject'] = Header('This is subject')  # 邮件标题

# 发送邮件的操作
try:
    server = smtplib.SMTP_SSL()  # SSL加密传输
    server.connect(smtp_server, server_port)  # 连接smtp,服务器和端口
    server.login(sender, password)  # 登录发信邮箱
    server.sendmail(sender, receivers, msg.as_string())  # 发送邮件
    server.quit()  # 关闭服务器
    print('发送成功')

except smtplib.SMTPException:
    print('发送失败')

发送正文带有图片的邮件

使用html格式的img标签指定图片cid显示图片,图片需要用MIMEImage上传并指定img Content-ID

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

# smtp服务器信息
smtp_server = 'smtp.qq.com'
server_port = 465

# 发送方信息
sender = '发送的邮箱地址'
password = '发送邮箱的smtp授权码'

# 收件人地址,列表可发给多人
receivers = ['dsaisu@163.com', '76das23021@qq.com', '253das58570@qq.com']

# 实例化,添加正文内容,html格式,<img>标签 指定cid属性的值等于上传图片中Content-ID的值
msg = MIMEMultipart()
msg.attach(MIMEText('<img src="cid:demo" />', 'html', 'utf-8'))    # 与下面的Content-ID的值相同

# 添加图片
img = MIMEImage(open('source.jpg', 'rb').read())
img.add_header('Content-ID', 'demo')    # 与上面的cid相同
msg.attach(img)

# 邮件头信息
msg['From'] = Header(sender)  # 发件人
msg['To'] = Header(', '.join(receivers))  # 发到哪里,参数为字符串
msg['Subject'] = Header('This is subject')  # 邮件标题

# 发送邮件的操作
try:
    server = smtplib.SMTP_SSL()  # SSL加密传输
    server.connect(smtp_server, server_port)  # 连接smtp,服务器和端口
    server.login(sender, password)  # 登录发信邮箱
    server.sendmail(sender, receivers, msg.as_string())  # 发送邮件
    server.quit()  # 关闭服务器
    print('发送成功')

except smtplib.SMTPException:
    print('发送失败')

原文地址:https://www.cnblogs.com/milesma/p/12085295.html