Python 发送邮件
1、案例一 (发送普通邮件)
import smtplib
from email.mime.text import MIMEText
# 发送普通邮件
# POP3 服务器地址:pop.qq.com
# SMTP 服务器地址:smtp.qq.com
class SendEmail:
def __init__(self):
# 发送邮件的用户
self.send_user = '[email protected]'
# SMTP 服务器地址:smtp.qq.com
self.email_host = 'smtp.qq.com'
# 服务器端口 465
self.email_port = 465
# 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码
self.password = 'xxxxxxxxx'
def send_email(self, title, data, user_list):
try:
message = MIMEText(data, _subtype='plain', _charset='utf-8')
message['Subject'] = title
message['From'] = self.send_user
message['To'] = ";".join(user_list)
server = smtplib.SMTP_SSL()
server.connect(self.email_host, self.email_port)
server.login(self.send_user, self.password)
server.sendmail(self.send_user, user_list, message.as_string())
server.close()
print("发送邮件成功,请查收")
except Exception as e:
print("发送邮件失败,报错信息:", e)
if __name__ == '__main__':
user_list = ["[email protected]"]
title = "邮件标题"
data = "邮件内容"
send = SendEmail()
send.send_email(title, data, user_list)
2、案例二 (发送带附件的邮件)
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 发送带附件的邮件
# POP3 服务器地址:pop.qq.com
# SMTP 服务器地址:smtp.qq.com
class SendEmail:
def __init__(self):
# 发送邮件的用户
self.send_user = '[email protected]'
# SMTP 服务器地址:smtp.qq.com
self.email_host = 'smtp.qq.com'
# 服务器端口 465
self.email_port = 465
# 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码
self.password = 'xxxxxxxxx'
def send_email(self, title, data, file_path_list, user_list):
try:
# 创建一个带附件的实例
message = MIMEMultipart()
message.attach(MIMEText(data, 'plain', 'utf-8'))
message['Subject'] = title
message['From'] = self.send_user
message['To'] = ";".join(user_list)
for file_path in file_path_list:
# 构造附件
attrs = MIMEText(open(file_path, 'rb').read(), 'base64', 'utf-8')
attrs["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
attrs["Content-Disposition"] = 'attachment; filename={}'.format(str(file_path).split('/')[-1])
message.attach(attrs)
server = smtplib.SMTP_SSL()
server.connect(self.email_host, self.email_port)
server.login(self.send_user, self.password)
server.sendmail(self.send_user, user_list, message.as_string())
server.close()
print("发送邮件成功,请查收")
except Exception as e:
print("发送邮件失败,报错信息:", e)
if __name__ == '__main__':
user_list = ["[email protected]"]
title = "邮件标题"
data = "邮件内容"
file_path_list = ['demo.jpg']
send = SendEmail()
send.send_email(title, data, file_path_list, user_list)
3、案例三 (发送一个HTML片段)
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# 发送一个HTML片段
# POP3 服务器地址:pop.qq.com
# SMTP 服务器地址:smtp.qq.com
class SendEmail:
def __init__(self):
# 发送邮件的用户
self.send_user = '[email protected]'
# SMTP 服务器地址:smtp.qq.com
self.email_host = 'smtp.qq.com'
# 服务器端口 465
self.email_port = 465
# 开启POP3/SMTP服务,在第三方客户端登录时,生成的授权码
self.password = 'xxxxxxxxx'
def send_email(self, title, data, user_list):
try:
message = MIMEMultipart('related')
content = MIMEText(data, 'html', 'utf-8')
message.attach(content)
message['Subject'] = title
message['From'] = self.send_user
message['To'] = ";".join(user_list)
file = open("demo.jpg", "rb")
img_data = file.read()
file.close()
img = MIMEImage(img_data)
img.add_header('Content-ID', 'image')
message.attach(img)
server = smtplib.SMTP_SSL()
server.connect(self.email_host, self.email_port)
server.login(self.send_user, self.password)
server.sendmail(self.send_user, user_list, message.as_string())
server.close()
print("发送邮件成功,请查收")
except Exception as e:
print("发送邮件失败,报错信息:", e)
if __name__ == '__main__':
user_list = ["[email protected]"]
title = "邮件标题"
data = """
<p>全是BUG</p>
<p><a href="https://www.cnblogs.com/xingxingnbsp/">菜鸟程序员博客</a></p>
<p>图片演示:</p>
<p><img src="cid:image"></p>
"""
send = SendEmail()
send.send_email(title, data, user_list)
标签:Python,self,list,send,发送,user,message,email,邮件
From: https://www.cnblogs.com/xingxingnbsp/p/17446028.html