准备需要发送邮件的邮箱账号
如果是163邮箱只需要准备账号和密码,如果是QQ邮箱,需要准备账号和授权码(密码不好用),可以去QQ邮箱官网的帮助中心去看授权码的获取方式
一、邮件发送的基本流程
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2022/5/10 15:38# @Author : cheney# @File : 01-邮件发送的基本流程.py# @Software: win7 python3.7.2# @Describe: Noneimport smtplib# 创建邮件对象(真正被发送的东西)from email.mime.multipart import MIMEMultipart# 邮件的主题from email.header import Headerfrom email.mime.text import MIMEText# 1. 连接邮箱服务器,登录邮箱# 1)连接邮箱服务器:smtplib.SMTP_SSL("smtp.邮箱类型名.com")# 163邮箱的连接地址:stmp.163.com# qq邮箱的连接地址:stmp.qq.com# 端口:25/465(如有垃圾邮箱提示,可以切换使用)con = smtplib.SMTP_SSL("smtp.163.com",465)# 2)登录邮箱# 连接对象.login(账号,密码)# 密码 - 163邮箱;QQ邮箱密码有授权码con.login("OneCheney","FQMLIXRZOTADTPLE")# 2. 准备数据# 1)创建邮件对象msg = MIMEMultipart()# 2)设置邮件主题# Header("邮件的标题")subject = Header("发送邮件","utf-8").encode()msg['Subject'] = subject# 设置邮件发送人msg['From'] = 'OneCheney@163.com <OneCheney@163.com>'# 设置邮件的接收人# '收件人1;收件2;收件人3'msg['To'] = '1040024585@qq.com'# 设置邮件正文(邮件需要发送的内容)# 普通文本:MIMEText(文字内容,文本类型,编码方式)# 文本内容 - plain(普通文字),html(超链接),base64(二进制文件)text = MIMEText("smtplib发送邮件",'plain','utf-8')msg.attach(text)# 3. 发送邮件# 连接对象.sendmail(发件人,收件人,字符串类型的邮件对象)con.sendmail('onecheney@163.com','1040024585@qq.com',msg.as_string())con.quit()
二、html内容的发送
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2022/5/10 17:11# @Author : cheney# @File : 02-html内容的发送.py# @Software: win7 python3.7.2# @Describe: Noneimport smtplibfrom email.mime.multipart import MIMEMultipartfrom email.header import Headerfrom email.mime.text import MIMEText# 1.连接和登录con = smtplib.SMTP_SSL('smtp.163.com',465)con.login("onecheney@163.com","FQMLIXRZOTADTPLE")# 2.准备数据msg = MIMEMultipart()# 主题subject = Header("html发送邮件")msg['Subject'] = subject# 收件人和发件人msg['from'] = 'onecheney@163.com <onecheney@163.com>'msg['To'] = '1040024585@qq.com'# ==============================构建html内容===========================content = """<h1>正文的标题</h1><p>我是邮件正文</p><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109250135543096-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654767215&t=e52abef376bd52863a7573dfabefb7e6"><a href='https://www.baidu.com'>点击跳转</a>"""html = MIMEText(content,'html','utf-8')msg.attach(html)# 3.发送con.sendmail("onecheney@163.com","1040024585@qq.com",msg.as_string())con.quit()
三、发送图片
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2022/5/10 17:43# @Author : cheney# @File : 03-发送图片.py# @Software: win7 python3.7.2# @Describe: Noneimport smtplibfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.header import Header# 1.连接服务器,登录邮箱账号from email.mime.text import MIMETextcon = smtplib.SMTP_SSL('smtp.163.com',465)con.login('onecheney@163.com',"FQMLIXRZOTADTPLE")# 2.准备数据# 创建邮件对象msg = MIMEMultipart()# 设置主题msg['subject'] = Header("发送图片",'utf-8').encode()# 设置发件人和收件人msg['from'] = 'onecheney@163.com <necheney@163.com>'msg['To'] = '1040024585@qq.com'# 设置内容# =====================发送图片=================# 1,发送附件(图片)# a.获取图片数据,创建图片对象image_data1 = open("./2.jpg",'rb').read()image = MIMEImage(image_data1)# b.设置附件名(名字不要用中文)image['Content-Disposition'] = 'attachment; filename="myname"'# c.添加到邮件对象中msg.attach(image)# 2)发送图片内容(需要使用html内容来发送)# a.准备图片image_data2 = open("./2.jpg",'rb').read()image2 = MIMEImage(image_data2)image2.add_header('Content-ID','<img1>')msg.attach(image2)content = """<p>这是一条发送图片的邮件</p><img src="cid:img1">"""html = MIMEText(content,'html','utf-8')msg.attach(html)# 3.发送邮件con.sendmail('onecheney@163.com','1040024585@qq.com',msg.as_string())con.quit()
四、发送附件
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2022/5/10 22:17# @Author : cheney# @File : 04-文件附件.py# @Software: win7 python3.7.2# @Describe: Noneimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.header import Header# 连接和登录con = smtplib.SMTP_SSL("smtp.163.com",465)con.login("onecheney@163.com","FQMLIXRZOTADTPLE")# 准备数据msg = MIMEMultipart()msg['Subject'] = Header("发送邮件",'utf-8').encode()msg['From'] = 'onecheney@163.com <onecheney@163.com>'msg['To'] = '1040024585@qq.com'# ===================准备附件==================content1 = open("./静夜思.txt",'rb').read()file1 = MIMEText(content1,'base64','utf-8')file1['Content-Disposition'] = 'attachment; filename="pil.txt"'msg.attach(file1)# 3.发送邮件con.sendmail('onecheney@163.com','1040024585@qq.com',msg.as_string())con.quit()
