阅读量:0
Python发送邮件
发送邮件
协议
- SMTP:发送邮件,Simple Mail Transfer Protocol
- POP3:邮局协议,Post Offic Protocol
- IMAP:网络有机胺访问协议:Internet Mail Access Protocol,比POP3实时性更强
- 上述三种协议均为明文传输,所以建议对其进行加密(TLS),生成授权码
如何发送邮件,废话不多说,直接上代码
-
# mail.py # 直接导入内置模块 import smtplib, time # smtplib模块主要用于处理SMTP协议 # email模块主要处理邮件的头和正文等数据 from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # 构建邮件的主题对象 sender = '1xxxxxxxxxx@163.com' receivers = '2xxxxxxxx@qq.com' receiver_name = '啦啦啦' msg = MIMEMultipart() msg['Subject'] = 'Python测试邮件' msg['From'] = sender msg['To'] = receivers body = ''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>重置您的 无敌软件的 密码</title> <style> body,html,div,ul,li,button,p,img,h1,h2,h3,h4,h5,h6 { margin: 0; padding: 0; } body,html { background: #fff; line-height: 1.8; } h1,h2,h3,h4,h5,h6 { line-height: 1.8; } .email_warp { height: 100vh; min-height: 500px; font-size: 14px; color: #212121; display: flex; /* align-items: center; */ justify-content: center; } .logo { margin: 3em auto; width: 200px; height: 60px; } h1.email-title { font-size: 26px; font-weight: 500; margin-bottom: 15px; color: #252525; } a.links_btn { border: 0; background: #4C84FF; color: #fff; width: 100%; height: 50px; line-height: 50px; font-size: 16px; margin: 40px auto; box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.15); border-radius: 4px; outline: none; cursor: pointer; transition: all 0.3s; text-align: center; display: block; text-decoration: none; } .warm_tips { color: #757575; background: #f7f7f7; padding: 20px; } .warm_tips .desc { margin-bottom: 20px; } .qr_warp { max-width: 140px; margin: 20px auto; } .qr_warp img { max-width: 100%; max-height: 100%; } .email-footer { margin-top: 2em; } #reset-password-email { max-width: 500px; } #reset-password-email .accout_email { color: #4C84FF; display: block; margin-bottom: 20px; } </style> </head> <body> <section class="email_warp"> <div id="reset-password-email"> <pre> __ __ ______ _ __ / /____ _____/ /_ / ____/___ ___ ____ _(_) / / __/ _ \/ ___/ __/ / __/ / __ `__ \/ __ `/ / / / /_/ __(__ ) /_ / /___/ / / / / / /_/ / / / \__/\___/____/\__/ /_____/_/ /_/ /_/\__,_/_/_/ </pre> <h1 class="email-title"> 尊敬的<span>'''+receiver_name+'''</span>您好: </h1> <p>您正在为登录邮箱为如下地址的 啦啦啦 账户重置密码:</p> <b class="accout_email">xxxx@abc.com</b> <p>请注意,如果这不是您本人的操作,请忽略并关闭此邮件。</p> <p>如您确认重置 XXXX 的账户密码,请点击下方按钮。</p> <a class="links_btn" οnclick="window.open('https:XXXXXXXXXXX')">重置密码</a> <div class="warm_tips"> <div class="desc"> 为安全起见,以上按钮为一次性链接,且仅在24小时内有效,请您尽快完成操作。 </div> <p>如有任何疑问或无法完成注册,请通过如下方式与我们联系:</p> <p>邮箱:support@XXX.cn</p> <p>微信助理:XXXXX</p> <div class="qr_warp"> <img src="https://XXXXXXXXXXXX" alt="微信二维码图片"> </div> <p>本邮件由系统自动发送,请勿回复。</p> </div> <div class="email-footer"> <p>您的智能项目助理</p> <p>XXXXXXXX</p> </div> </div> </section> </body> </html> ''' content = MIMEText(body, 'html', 'utf-8') msg.attach(content) # 添加邮件附件 attach='./001-ExampleSourceURL.zip' attachment=MIMEApplication(open(attach,'rb').read() file_name=attach.aplit('/')[-1] attachment.add_header('Content-Disposition','attachment',filename=file_name) msg.attach(attachment) # 建立与邮件服务器的链接,并发送邮件 smtpObj = smtplib.SMTP() # 如果基于SSL则smtplib.SMTP_SSL smtpObj.connect('smtp.163.com', 25) # 这里是明文密码,不安全 ''' smtpObj.login(user='1xxxxxxxxxx@163.com', password='xxxxxxxxxx') smtpObj.sendmail(sender, receivers, str(msg)) smtpObj.quit() ''' # 注意,QQ邮件服务器不支持连续不停止发送,如果要发送多封,建议之间sleep几秒钟 # 如何登陆SMTP+SSL邮件服务器 # 授权码 auth_code='XXXXXXXXXXXXXXXX' smtpObj=smtplib.SMTP_SSL('smtp.163.com',465) smtpObj.login(user='1xxxxxxxxxx@163.com', password=auth_code) smtpObj.sendmail(sender, receivers, str(msg)) smtpObj.quit()
大家可以使用Wireshark进行抓包尝试理解smtp通信过程,在不使用授权码时抓到的仅仅是经过base64编码过的数据,很容易进行解码,从而轻易登陆你的邮件服务器,但是使用授权码的情况下抓到的都是经过加密的数据,相对来说会更加安全
-
小知识点:
编码解码encode.py
import bash64 source='5oiR5bCx55+l6YGT5L2g5Lya6Kej5a+G' print(base64.b64decode(source))