发送邮件是一个常见的需求,一般会采用SMTP代理的形式,进行邮件自动发送
在发送邮件时,主要有如下准备工作:
????????① 发件人、收件人、抄送人
????????② 发件人邮箱授权码
????????③ 邮件主题
????????④ 邮件正文
????????⑤ 附件
发送邮件上,由浅到深,可以大致分为以下5种格式:
????????① 只有正文;
? ? ? ? ②?正文+html+超链接;
? ? ? ? ③?正文+正文图片;
? ? ? ? ④?正文+附件;
? ? ? ? ⑤?正文+html+可跳转url+正文图片+附件(这里会规范写一下)
其中,授权码的获取这里需要各自操作一下,示例如:QQ邮箱的授权码,需要在电脑上登录自己的QQ邮箱,在“设置” - “账户” - “POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”中,开启相关服务,获取授权码
如图所示操作,点击生成授权码,发送短信即可获取到SMTP邮件代理授权码,将发送人邮箱授权码复制下来以便后面SMTP代理使用
?
接下来用QQ邮箱,示例自动发送以上类型的邮件(注意:带邮件和不带邮件中,实例不一样)
1、发送只有文本正文的邮件
# 发送只有文本正文的邮件
import smtplib
from email.mime.text import MIMEText
# 配置邮件信息
email = {
'from': '111@qq.com',
'to': ['222@qq.com'],
'cc': ['333@qq.com'],
'subject': '自动发送邮件',
'license': '这里是你们的授权码',
'text': '示例:发送只含有文本的邮件' ,
}
# 创建一个文本实例
# 正文
msg = MIMEText(email['text'], 'plain', 'utf-8')
# 发件人
msg['From'] = email['from']
# 收件人
msg['To'] = ','.join(email['to'])
# 抄送
msg['Cc'] = ','.join(email['cc'])
# 邮件标题
msg['Subject'] = email['subject']
# 登录邮箱服务器
email_server = smtplib.SMTP_SSL('smtp.qq.com', 465)
email_server.login(email['from'], email['license'])
# 发送邮件
try:
email_server.sendmail(email['from'], email['to'], msg.as_string())
print('发送成功')
except:
print('Error:发送失败')
2、发送包含正文、超链接的邮件
# 发送包含正文、可跳转URL链接的邮件
import smtplib
from email.mime.text import MIMEText
# 配置邮件信息
email = {
'from': '111@qq.com',
'to': ['222@qq.com'],
'cc': ['333@qq.com'],
'subject': '自动发送邮件',
'license': '这里是你们的授权码',
'text': '示例:发送只含有文本的邮件' ,
'html': '<p>欢迎进入我的博客:  <a href="https://blog.csdn.net/wang1qqqq">点击进入</a></p>',
}
# 创建一个文本实例
# 添加文本(可有可无)
msg = MIMEText(email['text'], 'plain', 'utf-8')
# 添加带url的html文本链接
msg_html = MIMEText(email['html'], 'html', 'utf-8')
msg.attach(msg_html)
# 发件人
msg['From'] = email['from']
# 收件人
msg['To'] = ','.join(email['to'])
# 抄送
msg['Cc'] = ','.join(email['cc'])
# 邮件标题
msg['Subject'] = email['subject']
# 登录邮箱服务器
email_server = smtplib.SMTP_SSL('smtp.qq.com', 465)
email_server.login(email['from'], email['license'])
# 发送邮件
try:
email_server.sendmail(email['from'], email['to'], msg.as_string())
print('发送成功')
except:
print('Error:发送失败')
3、发送包含正文、图片的邮件
# 发送包含正文、图片的邮件
import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
# 配置邮件信息
email = {
'from': '111@qq.com',
'to': ['222@qq.com'],
'cc': ['333@qq.com'],
'subject': '自动发送邮件',
'license': '这里是你们的授权码',
'text': '示例:发送只含有文本的邮件' ,
}
# 创建一个文本实例
# 添加文本(可有可无)
msg = MIMEText(email['text'], 'plain', 'utf-8')
# 插入图片html,命名图片id为i_image
img_html = '<p><img src="cid:i_image"></p>'
msg.attach(MIMEText(img_html, 'html', 'utf-8'))
# 打开图片
img_open = open(i_attach, 'rb')
img = MIMEImage(img_open.read())
# 插入图片
img.add_header('Content-ID', '<i_image>')
msg.attach(img)
# 关闭图片
img_open.close()
# 发件人
msg['From'] = email['from']
# 收件人
msg['To'] = ','.join(email['to'])
# 抄送
msg['Cc'] = ','.join(email['cc'])
# 邮件标题
msg['Subject'] = email['subject']
# 登录邮箱服务器
email_server = smtplib.SMTP_SSL('smtp.qq.com', 465)
email_server.login(email['from'], email['license'])
# 发送邮件
try:
email_server.sendmail(email['from'], email['to'], msg.as_string())
print('发送成功')
except:
print('Error:发送失败')
4、发送包含正文、附件的邮件
# 发送包含正文、附件的邮件
import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
# 配置邮件信息
email = {
'from': '111@qq.com',
'to': ['222@qq.com'],
'cc': ['333@qq.com'],
'subject': '自动发送邮件',
'license': 'autmmyrqmqpcbace',
'text': '示例:发送只含有文本的邮件' ,
}
# 创建一个带附件的示例
msg = MIMEMultipart()
# 打开图片
img_open = open(i_attach, 'rb')
# 设置图片信息
att = MIMEText(img_open.read(), 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream'
# 添加到附件
att.add_header('Content-Disposition', 'attachment', filename='疫情大数据.png')
msg.attach(att)
# 关闭图片
img_open.close()
# 发件人
msg['From'] = email['from']
# 收件人
msg['To'] = ','.join(email['to'])
# 抄送
msg['Cc'] = ','.join(email['cc'])
# 邮件标题
msg['Subject'] = email['subject']
# 登录邮箱服务器
email_server = smtplib.SMTP_SSL('smtp.qq.com', 465)
email_server.login(email['from'], email['license'])
# 发送邮件
try:
email_server.sendmail(email['from'], email['to'], msg.as_string())
print('发送成功')
except:
print('Error:发送失败')
5、发送包含正文、html、超链接、正文图片、附件的邮件
# 发送包含正文、html、可点击url、正文图片、附件的邮件
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def email_header(email):
# 设置邮件信息
# 无附件邮件使用:msg = MIMEText(email['text'], 'plain', 'utf-8')
# MIMEMultipart:创建一个带附件的实例
msg = MIMEMultipart()
# 可以自定义发件人抬头(不理想,QQ邮箱列表上看不到发件人名称):Header('发送机器人', 'utf-8')
msg['From'] = email['from']
msg['To'] = ','.join(email['to'])
msg['Cc'] = ','.join(email['cc'])
msg['Subject'] = email['subject']
return msg
def email_body(email, msg):
# 添加文本(如果有html,文本会被覆盖)
# msg.attach(MIMEText(email['text'], 'plain', 'utf-8'))
# 添加html
msg.attach(MIMEText(email['text'], 'html', 'utf-8'))
msg.attach(MIMEText(email['html'], 'html', 'utf-8'))
for i_attach in email['attach']:
# 添加正文图片(图片需要在html的情况下使用)
img_html = '<p><img src="cid:i_image"></p>'
msg.attach(MIMEText(img_html, 'html', 'utf-8'))
img_open = open(i_attach, 'rb')
img = MIMEImage(img_open.read())
img.add_header('Content-ID', '<i_image>')
msg.attach(img)
# 关闭图片
img_open.close()
def email_att(email, msg):
# 添加附件
for i_attach in email['attach']:
img_open = open(i_attach, 'rb')
att = MIMEText(img_open.read(), 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment', filename='中国疫情大数据.png')
msg.attach(att)
# 关闭图片
img_open.close()
def email_login(email):
# 登录邮箱服务器
email_server = smtplib.SMTP_SSL('smtp.qq.com', 465)
email_server.login(email['from'], email['license'])
return email_server
def email_send(email, email_server):
# 发送邮件
try:
email_server.sendmail(email['from'], email['to'], msg.as_string())
print('发送成功')
except:
print('Error:发送失败')
if __name__ == '__main__':
# 配置附件路径
img_path = 'auto_img/'
# 定义发送邮件信息
email = {
'from': '111@qq.com',
'to': ['222@qq.com'],
'cc': ['333@qq.com'],
'subject': '每日疫情数据推送',
'license': '这里是你们的授权码',
'text': '每日疫情数据推送:' + str(datetime.datetime.now().strftime("%Y-%m-%d")),
'html': '<p>欢迎进入我的博客:  <a href="https://blog.csdn.net/wang1qqqq">点击进入</a></p>',
'attach': [img_path + '中国疫情大数据.png']
}
# 配置收件人、发件人、抄送人、标题
msg = email_header(email)
# 配置正文内容
email_body(email, msg)
# 配置附件
email_att(email, msg)
# 登录邮件服务器
email_server = email_login(email)
# 发送邮件
email_send(email, email_server)
如上,就是自动发送邮件的示范,可以根据自身情况,选择发送带正文、html、超链接、正文图片、附件的邮件。
|