# 2019/9/10 查看给客服部发邮件程序日志

import datetime
import smtplib
from email.message import EmailMessage
from func_send_keyword_in_file_ck_log import func_find_file_keyword

now = datetime.datetime.now()

log_file = r'D:\\log\\send_email_log_' + str(now)[0:10]
weekday = now.weekday()
if weekday in (0, 1, 2, 3, 4):
    keyword = '邮件发送成功'
    re_dict = func_find_file_keyword(log_file, keyword)

    csm_mail_service = 'mail.csm.com.cn'
    email_msg_body = ''
    email_msg = EmailMessage()

    email_msg['From'] = 'zhouyujiang@csm.com.cn'
    email_msg['to'] = ['zhouyujiang@csm.com.cn', 'huwenhui@csm.com.cn', 'lvyidan@csm.com.cn', 'nabuqi@csm.com.cn']

    for one in re_dict:
        if len(one['filelines']) != 0:
            email_msg_body = '今日邮件已发送-' + str(now)[0:10]
            email_subj = '[已发送]-邮件发送情况'
        else:
            email_msg_body = '今日邮件没有发送' + str(now)[0:10] + \
                             '请检查日志{log_}'.format(log_='send_email_log_' + str(now)[0:10])
            email_subj = '[未发送]-邮件发送情况'

    email_msg.set_content(email_msg_body)
    email_msg['Subject'] = email_subj

    server = smtplib.SMTP(host=csm_mail_service)
    server.send_message(email_msg)
    server.quit()