Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。
Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。
- 发送纯文本内容:
# 引入相应的模块
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email import encoders
from email.utils import parseaddr, formataddr
class SendMail(object):
def __init__(self):
self.fromUser = "xxxxxx@qq.com"
self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码
self.smtpAddr = "smtp.qq.com"
# 构造邮件结构
# toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容
def mailStructure(self, toAddrs, subject, msg):
# 邮件对象:
mailMsg = MIMEMultipart()
mailMsg['Subject'] = ("Hello <%s>" % subject)
mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser)
mailMsg['To'] = ','.join(toAddrs)
# 邮件正文是MIMEText :
mailMsg.attach(MIMEText(msg, 'html', 'utf-8'))
return mailMsg.as_string()
# 发送邮件
def sendMail(self, toAddrs, subject, msg):
mailMsg_as_string = self.mailStructure(toAddrs, subject, msg)
# 连接服务器发送邮件
try:
server = smtplib.SMTP_SSL(self.smtpAddr, 465)
server.connect(self.smtpAddr) # 连接smtp服务器
server.login(self.fromUser, self.userPasswd) # 登录邮箱
server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件
server.quit()
except Exception:
print("Error: unable to send email")
a = SendMail()
a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
-
效果图
如果Email中要加上附件怎么办?带附件的邮件可以看做包含若干部分的邮件:文本和各个附件本身,所以,可以构造一个MIMEMultipart对象代表邮件本身,然后往里面加上一个MIMEText作为邮件正文,再继续往里面加上表示附件的MIMEBase对象即可:
其实就是打开图片并引入(加上这段内容)
with open(r"C:\Users\YJ\Desktop\favicon.png", "rb") as f:
# 设置附件的MIME和文件名,这里是png类型:
mime = MIMEBase("image", "png", filename="test.png")
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename='test.png')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
mailMsg.attach(mime)
- 发送附件图片 (完整代码如下:)
# 引入相应的模块
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email import encoders
from email.utils import parseaddr, formataddr
class SendMail(object):
def __init__(self):
self.fromUser = "xxxxxx@qq.com"
self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码
self.smtpAddr = "smtp.qq.com"
# 构造邮件结构
# toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容
def mailStructure(self, toAddrs, subject, msg):
# 邮件对象:
mailMsg = MIMEMultipart()
mailMsg['Subject'] = ("Hello <%s>" % subject)
mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser)
mailMsg['To'] = ','.join(toAddrs)
# 邮件正文是MIMEText :
mailMsg.attach(MIMEText(msg, 'html', 'utf-8'))
# 发送图片附件, 需要用到附件对象MIMEBase对象, 需要引入from email.mime.multipart import MIMEBase
# 添加附件就是加上一个MIMEBase,从本地读取一个图片:
with open(r"C:\Users\YJ\Desktop\favicon.png", "rb") as f:
# 设置附件的MIME和文件名,这里是png类型:
mime = MIMEBase("image", "png", filename="test.png")
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename='test.png')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
mailMsg.attach(mime)
return mailMsg.as_string()
# 发送邮件
def sendMail(self, toAddrs, subject, msg):
mailMsg_as_string = self.mailStructure(toAddrs, subject, msg)
# 连接服务器发送邮件
try:
server = smtplib.SMTP_SSL(self.smtpAddr, 465)
server.connect(self.smtpAddr) # 连接smtp服务器
server.login(self.fromUser, self.userPasswd) # 登录邮箱
server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件
server.quit()
except Exception:
print("Error: unable to send email")
a = SendMail()
a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
-
效果图
如果要把一个图片嵌入到邮件正文中怎么做?直接在HTML邮件中链接图片地址行不行?答案是,大部分邮件服务商都会自动屏蔽带有外链的图片,因为不知道这些链接是否指向恶意网站。
要把图片嵌入到邮件正文中,我们只需按照发送附件的方式,先把邮件作为附件添加进去,然后,在HTML中通过引用src="cid:0"就可以把附件作为图片嵌入了。如果有多个图片,给它们依次编号,然后引用不同的cid:x即可。
其实就是将MIMEText 格式改变一下
mailMsg.attach(MIMEText('<html><body><h1>%s</h1>' % (msg) + '<p><img src="cid:0"></p>' + '</body></html>', 'html','utf-8'))
- 将附件图片包含在邮件内容中(完整代码如下:)
# 引入相应的模块
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email import encoders
from email.utils import parseaddr, formataddr
class SendMail(object):
def __init__(self):
self.fromUser = "xxxxxx@qq.com"
self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码
self.smtpAddr = "smtp.qq.com"
# 构造邮件结构
# toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容
def mailStructure(self, toAddrs, subject, msg):
# 邮件对象:
mailMsg = MIMEMultipart()
mailMsg['Subject'] = ("Hello <%s>" % subject)
mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser)
mailMsg['To'] = ','.join(toAddrs)
# 邮件正文是MIMEText :
# 加载图片并引入到邮件内容中来
mailMsg.attach(MIMEText('<html><body><h1>%s</h1>' % (msg) + '<p><img src="cid:0"></p>' + '</body></html>', 'html','utf-8'))
# 发送图片附件, 需要用到附件对象MIMEBase对象, 需要引入from email.mime.multipart import MIMEBase
# 添加附件就是加上一个MIMEBase,从本地读取一个图片:
with open(r"C:\Users\YJ\Desktop\favicon.png", "rb") as f:
# 设置附件的MIME和文件名,这里是png类型:
mime = MIMEBase("image", "png", filename="test.png")
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename='test.png')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
mailMsg.attach(mime)
return mailMsg.as_string()
# 发送邮件
def sendMail(self, toAddrs, subject, msg):
mailMsg_as_string = self.mailStructure(toAddrs, subject, msg)
# 连接服务器发送邮件
try:
server = smtplib.SMTP_SSL(self.smtpAddr, 465)
server.connect(self.smtpAddr) # 连接smtp服务器
server.login(self.fromUser, self.userPasswd) # 登录邮箱
server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件
server.quit()
except Exception:
print("Error: unable to send email")
a = SendMail()
a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
-
效果图
- 发送纯文本附件
# 引入相应的模块
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email import encoders
from email.utils import parseaddr, formataddr
class SendMail(object):
def __init__(self):
self.fromUser = "xxxxxx@qq.com"
self.userPasswd = "xxxxxx" # 此处是qq邮箱授权码, 不是登录密码
self.smtpAddr = "smtp.qq.com"
# 构造邮件结构
# toAddrs 收件人可以是多个,["xxx@qq.com","xxx@qq.com"], subject 邮件的主题, msg 邮件的内容
def mailStructure(self, toAddrs, subject, msg):
# 邮件对象:
mailMsg = MIMEMultipart()
mailMsg['Subject'] = ("Hello <%s>" % subject)
mailMsg['From'] = ("Python管理员 <%s>" % self.fromUser)
mailMsg['To'] = ','.join(toAddrs)
# 邮件正文是MIMEText :
mailMsg.attach(MIMEText(msg, 'html', 'utf-8'))
# 发送文件附件, 需要用到附件对象MIMEBase对象, 需要引入from email.mime.multipart import MIMEBase
# 添加附件就是加上一个MIMEBase,从本地读取一个文件:
with open(r"C:\Users\YJ\Desktop\aaa.txt", "rb") as f:
# 设置附件的MIME和文件名,这里是png类型:
mime = MIMEBase("txta", "txt", filename="test.txt")
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename='test.txt')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
mailMsg.attach(mime)
return mailMsg.as_string()
# 发送邮件
def sendMail(self, toAddrs, subject, msg):
mailMsg_as_string = self.mailStructure(toAddrs, subject, msg)
# 连接服务器发送邮件
try:
server = smtplib.SMTP_SSL(self.smtpAddr, 465)
server.connect(self.smtpAddr) # 连接smtp服务器
server.login(self.fromUser, self.userPasswd) # 登录邮箱
server.sendmail(self.fromUser, toAddrs, mailMsg_as_string) # 发送邮件
server.quit()
except Exception:
print("Error: unable to send email")
a = SendMail()
a.sendMail(["381347268@qq.com", "44934875@qq.com"], "测试主题", "测试内容")
-
效果图