import datetime
import smtplib
from email.mime.text import MIMEText
import logging
import os
logging.basicConfig(filename='E:\code\Plan_m\log.log', level=logging.INFO,filemode = 'a', format = '【%(asctime)s】 【%(levelname)s】 >>> %(message)s', datefmt = '%Y-%m-%d %H:%M')
# 邮箱属性配置
def send_email(text_msg):
# 邮箱服务端
mailserver = 'smtp.163.com'
# 发件人-填写自己的邮箱
userName_SendMail = '[email protected]'
# 邮箱发件授权码-为发件人生成的授权码,详见下文
userName_AuthCode = 'BOEFGGZHRAHEXGRN'
# 定义邮件的接收者-我随便写的,若收件人较多,可用列表表示
per_email = email_dict.get(text_msg.get("跟进人","None"),"None")
print(per_email)
received_mail = ['[email protected]'] # 加入祖总 刘总 张总邮箱
# final_received_mail = received_mail+per_email
# print(final_received_mail)
# 发送一封简单的邮件,处理邮件内容
content = f'你有一个工厂订单即将预期,请及时处理。\n 工厂:{text_msg.get("工厂",None)},客户:{text_msg.get("客户",None)},样品单号:{text_msg.get("样品单号",None)},到期时间:{text_msg.get("计划完成时间",None)},样品明细:{text_msg.get("样品明细",None)},责任人:{text_msg.get("跟进人",None)}'
print(content)
# 纯文本形式的邮件内容的定义,通过MIMEText进行操作,plain为默认的文本的展示形式
email = MIMEText(content, 'plain', 'utf-8')
email['Subject'] = '即将延期的批量样品单提醒' # 定义邮件主题
email['From'] = "通知机器人" # 发件人
email['To'] = ','.join(received_mail) # 收件人(可以添加多个,若只有一个收件人,可直接写邮箱号)
# 发送邮件
# QQ邮箱的端口号是465,其他邮箱的端口号可自行百度,非QQ邮箱,一般使用SMTP即可,不需要有SSL
smtp = smtplib.SMTP_SSL(mailserver, port=465)
smtp.login(userName_SendMail, userName_AuthCode)
smtp.sendmail(userName_SendMail, ','.join(received_mail), email.as_string())
smtp.quit()
logging.info('恭喜,邮件发送成功了')
today = datetime.date.today()
data_dir = 'E:\code\Plan_m\批量样品跟进'
for excl in os.listdir(data_dir):
try:
excl_path = os.path.join(data_dir,excl)
data = pd.read_excel(excl_path)
data['计划完成时间'] = pd.to_datetime(data['计划完成时间'])
for i,row in data.iterrows():
try:
diff_day = (row['计划完成时间']-pd.Timestamp(today)).days
# print(dict(row))
logging.info(diff_day)
if diff_day