Python
1.Python安装配置
pip安装配置
Python虚拟环境
源码安装Python3.x
Pycharm设置python默认模板
2.Python模块
模块:email、smtplib
模块:time
Pandas
matplotlib安装配置
模块:elasticsearch
jupyter
Jinja2
去掉jinja2循环中的换行符
jinja2循环中的变量
jinja2中使用set定义变量
98.Django4
99.Django2
Django对接cas
ListView
Django基本操作
DjangoAdmin获取表单填写用户
本文档使用 MrDoc 发布
-
+
首页
模块:email、smtplib
# Python发送邮件 ## 功能需求 1. 使用smtplib, email两个模块 2. 可以同时发送文本与附件 3. 账号使用企业邮箱账号(注意不要把自己的账号写到wiki上) ## 提示信息 1. 需要引入的模块及类 ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.utils import formataddr ``` 2. python帮助使用方式 引入模块后,可以使用help()来查看帮助.例如: ```python import smtplib help(smtplib) ``` ## 实操 文本 import smtplib import csv from email.mime.text import MIMEText from email.header import Header from email.mime.text import MIMEText #文本(plain,html格式) from email.mime.image import MIMEImage #(附件图片) from email.mime.multipart import MIMEMultipart #(超文本,都行) from_addr = '740774622@qq.com' to_addrs = ['935523993@qq.com','740774622@qq.com'] to_addr = [] date = [['zw','935523993@qq.com'],['hmb','740774622@qq.com']] with open('/Users/huangmingbin/Desktop/to_addrs.csv','w',newline='') as f: writer = csv.writer(f) for row in date: writer.writerow(row) with open('/Users/huangmingbin/Desktop/to_addrs.csv','r',newline='') as b: reader = csv.reader(b) for row in reader: to_addr = row[1] password = 'zygwkyrktdiybcda' smtp_server = 'smtp.qq.com' test = ''' 人生苦短,我用Python 人生苦短,我用Python ''' msg = MIMEText(test,'plain','utf-8') msg['From'] = Header('Redhat') msg['To'] = Header(",".join(to_addrs)) msg['Subject'] = Header('Nice to me you ') server = smtplib.SMTP_SSL(smtp_server) server.connect(smtp_server,465) #连接smtp服务器 server.login(from_addr, password) #登录 try: server.sendmail(from_addr, to_addrs, msg.as_string()) #发送 print("successd") except: print("faild") server.quit() #退出 ``` ## 实操2 如何带附件,图片 import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from_addr = '740774622@qq.com' to_addr = '740774622@qq.com' password = 'xxxxx' #自己的授权码 smtp_server = 'smtp.qq.com' msg = MIMEMultipart('related') msg['From'] = from_addr msg['To'] = to_addr msg['Subject'] = 'Nice to me you' with open(r'/Users/huangmingbin/Desktop/proc.doc', 'rb') as p: text = MIMEText(p.read(),'base64','utf-8') #test["Content-Type"] = 'application/msword' #text.add_header('Content-Disposition','attachment', filename='proc.doc') text["Content-Disposition"] = 'attachment; filename = "proc.doc" ' with open(r'/Users/huangmingbin/Desktop/qrcode.png','rb') as bb: image = MIMEImage(bb.read()) image.add_header('content-disposition','attachment', filename='test.png') msg.attach(text) msg.attach(image) server = smtplib.SMTP_SSL(smtp_server) server.connect(smtp_server,465) #连接smtp服务器 server.login(from_addr, password) #登录 server.set_debuglevel(1) server.sendmail(from_addr, to_addr, msg.as_string()) #发送 server.quit() #退出 ## 参考链接 链接中使用的是python2.x 自行改为python3.x 版本,建议先自学: 1. [Python2发送email](https://www.runoob.com/python/python-email.html)
zhangky
2021年5月16日 11:18
分享文档
收藏文档
上一篇
下一篇
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码