ps:第一次写博客,仅供个人学习参考
python库自带的两个模块,email和smtplib模块,直接import就能用
smtplib:主要用于连接邮箱服务器,登录邮箱,发送邮件(指定发件人,收件人,内容)
email:主要用于构建邮件内容(文本、html、图片文件附件等)
# 普通连接,明文传输
smtp = smtplib.SMTP('email_server', port)
# SSL加密,用于QQ等邮件发送
smtp = smtplib.SMTP_SSL('email_server', port)
smtp.login('username', 'password')
# receiver可以是一个列表;msg.as_string()是email模块所构建的内容
smtp.sendmail('sender', 'receiver', msg.as_string())
# 结束回话
smtp.quit()
邮箱开启smtp服务才能成功连接,普通连接端口是25,SSL加密连接端口是其他,如下图
注意:这里的password并非邮箱密码,而是开启smtp服务后的授权码
常用的模块有:MIMEText,MIMEImage,MIMEMultipart
2.1 MIMEText
纯文本
# 构造文字内容
text = "this is a test"
text_plain = MIMEText(text, 'plain', 'utf-8')
超文本
# html
html = """
this is a test,don't scared!
click it plz!
"""
msg_html = MIMEText(html, 'html', 'utf-8')
文件附件
file = open(u'数字权利激活.rar','rb').read()
msg_file = MIMEText(file,'base64','utf-8')
msg_file['Content-Disposition'] = "attachment;filename='数字权利激活.rar'"
msg_file['Content-Type'] = 'application/otcet-stream'
2.2 MIMEImage
photo_file = open(rage your dream.jpg', 'rb').read()
msg_image = MIMEImage(photo_file)
msg_image['Content-Disposition'] = "attachment;filename='rage your dream.jpg'"
2.3 MIMEMultipart
常见的multipart类型有三种:multipart/alternative,multipart/related和multipart/mixed。
#我们一般用
msg = MIMEMultipart(‘mixed’)
我们必须把Subject,From,To,Date添加到MIMEText对象或者MIMEMultipart对象中,邮件中才会显示主题,发件人,收件人,时间(若无时间,就默认一般为当前时间,该值一般不设置)。
sender = '[email protected]'
password = 'ddlwzfotwvtwbeeg'
fromer = 'xx'
subject = u'这只是一个测试'
receiver = ['[email protected]']
server = 'smtp.qq.com'
port = 465
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['To'] = ';'.join(receiver)
msg['From'] = fromer
msg['Date'] = '2019-2-20'
说明:
以上的构造的文本,超文本,附件,图片都何以添加到MIMEMultipart(‘mixed’)中:
msg.attach(msg_plain)
msg.attach(msg_html)
msg.attach(msg_file)
msg.attach(msg_image)
# coding: utf-8
# @Time : 2019/2/19 19:58
# @Author : lsn
# @Email : [email protected]
# @File : emali_auto_send.py
# @Software: PyCharm
# function : 自动发送邮件,文本图片附件
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import smtplib
if __name__ == '__main__':
username = '[email protected]'
password = 'ddlwzfotwvtwbeeg'
fromer = 'xx'
subject = u'这只是一个测试'
receiver = ['[email protected]']
server = 'smtp.qq.com'
port = 465
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['To'] = ';'.join(receiver)
msg['From'] = fromer
msg['Date'] = '2019-2-20'
# html
html = """
this is a test,don't scared!
click it plz!
"""
msg_html = MIMEText(html, 'html', 'utf-8')
msg.attach(msg_html)
# 图片
photo_file = open('rage panda.jpg', 'rb').read()
msg_image = MIMEImage(photo_file)
msg_image['Content-Disposition'] = "attachment;filename='rage your dream.jpg'"
msg.attach(msg_image)
# file 文件
file = open(u'数字权利激活.rar','rb').read()
msg_file = MIMEText(file,'base64','utf-8')
msg_file['Content-Disposition'] = "attachment;filename='数字权利激活.rar'"
msg_file['Content-Type'] = 'application/otcet-stream'
msg.attach(msg_file)
# 普通连接,明文传输
# smtp = smtplib.SMTP(server, port)
# SSL加密,用于QQ等邮件发送
smtp = smtplib.SMTP_SSL(server, port)
smtp.login(username, password)
# receiver可以是一个列表;msg.as_string()是email模块所构建的内容
smtp.sendmail(username, receiver, msg.as_string())
# 结束回话
smtp.quit()
pass
参考:https://blog.csdn.net/xc_zhou/article/details/81021525