利用python自动发邮件
2019-07-26 10:52 冻雨冷雾 阅读(381) 评论(0) 编辑 收藏 举报工作中有时长时间运行代码时需要监控进度,或者需要定期发送固定格式邮件时,可以使用下面定义的邮件函数。
该函数调用了outlook和qqmail的接口,只需要放置到python的环境目录中即可 import 并使用。这里建议所有自己定义的函数放置在python3的文件下 而非site-package文件下。
也可以放置在任意默认环境路径下。默认路径查询:
import site; site.getsitepackages()
自己定义的函数将py文件命名为send_mail.py然后放置在上面显示的其中一个路径下即可。
代码:
def send_outlook(title,body,receivers,attachments): ''' ========================================================== title='this is a title!' body= ' Mates! Bon Apetite ' receivers=['123211855@qq.com','ext.wenzhe.tian@uaes.com'] attachments=['a.py','b.py']; send_outlook(title,body,receivers,attachments) ========================================================== ''' import win32com.client as win32 import warnings import pythoncom warnings.filterwarnings('ignore') pythoncom.CoInitialize() sub=title; body = body outlook = win32.Dispatch('outlook.application') receivers = receivers for i in receivers: mail = outlook.CreateItem(0) mail.To = i mail.Subject = sub mail.Body = body for j in attachments: mail.Attachments.Add(j) mail.Send() def send_qqmail(user,password,title,body,image,receivers,attachments): ''' ========================================================== user='123211855@qq.com'; password='zpkfkgxvdrnvbgcj' title='this is a title!' body= ' Mates! Bon Apetite ' receivers=['123211855@qq.com','ext.wenzhe.tian@uaes.com'] attachments=[]; send_qqmail(user,password,title,body,image,receivers,attachments) ========================================================== ''' import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart # 设置邮箱的域名 HOST = 'smtp.qq.com' # 设置邮件标题 SUBJECT = title # 设置发件人邮箱 FROM = user password=password #'zpkfkgxvdrnvbgcj' # 设置收件人邮箱 TO = receivers message = MIMEMultipart('related') #--------------------------------------发送文本----------------- # 发送邮件主体到对方的邮箱中 # message_html = MIMEText('<h2 style="color:red;font-size:100px">CSDN博客超级好</h2><img src="cid:big">','html','utf-8') message_text=MIMEText(body,'plain','utf-8') message.attach(message_text) #-------------------------------------发送图片-------------------- # rb 读取二进制文件 # 要确定当前目录有1.jpg这个文件 for i in image: image_data = open(i,'rb') # 设置读取获取的二进制数据 message_image = MIMEImage(image_data.read()) # 关闭刚才打开的文件 image_data.close() message_image.add_header('Content-ID','big') # 添加图片文件到邮件信息当中去 message.attach(message_image) #-------------------------------------添加文件--------------------- # 要确定当前目录有table.xls这个文件 # message_xlsx = MIMEText(open(r'C:\Users\ext.wenzhe.tian\Desktop\Dynamic_Data_Analysis_v3\Static_data_read_v2.py','rb').read(),'base64','utf-8') for i in attachments: message_xlsx = MIMEText(open(i,'rb').read(),'base64','utf-8') # 设置文件在附件当中的名字 message_xlsx['Content-Disposition'] = 'attachment;filename='+i message.attach(message_xlsx) # 设置邮件发件人 message['From'] = FROM # 设置邮件收件人 message['To'] = TO # 设置邮件标题 message['Subject'] = SUBJECT # 获取简单邮件传输协议的证书 email_client = smtplib.SMTP_SSL() # 设置发件人邮箱的域名和端口,端口为465 email_client.connect(HOST,'465') # ---------------------------邮箱授权码------------------------------ result = email_client.login(FROM,password) print('登录结果',result) email_client.sendmail(from_addr=FROM,to_addrs=TO.split(','),msg=message.as_string()) # 关闭邮件发送客户端 email_client.close()
以上代码分outlook邮件和qq邮件发送,qq邮箱发送需开启smtp的服务。
调用方法如下:
from send_mail import send_outlook from send_mail import send_qqmail help(send_outlook) help(send_qqmail) ''' ========================================================== title='this is a title!' body=' Mates! Bon Apetite ' receivers=['xxx@qq.com','xxx@xx.com'] attachments=['a.py','b.py']; send_outlook(title,body,receivers,attachments) ==================================================================== ========================================================== user='xxxxx@qq.com'; password='xxxxx' title='this is a title!' body=' Mates! Bon Apetite ' receivers=['xx@qq.com','xx@xx.com'] attachments=[]; send_qqmail(user,password,title,body,image,receivers,attachments) ========================================================== '''
——————————————活在当下,首先就是要做好当下的事.