使用python脚本实现带附件的自动邮件发送

import datetime
import os

import win32com.client as win32

print("Whether to customize input time:Y/N")
user_defined = input()
if user_defined == "Y":
    print("Please input year:")
    year = int(input())
    print("Please input month:")
    month = int(input())
    print("Please input day:")
    day = int(input())
else:
    solution_time = datetime.datetime.now()
    year = solution_time.year
    month = solution_time.month
    day = solution_time.day


# send fail info to [email protected]
def send_fail_info():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)

    sub = 'Discourse Score on ' + str(month) + '-' + str(day) + ',' + str(year) + " send failed."
    body = 'Hi, myself\n' \
           'The score result of discourse on ' + str(month) + '-' + str(day) + ',' + str(
        year) + ' send file failed, and please check it. \n' \
                'BR \n' \
                'Hengtai Nie'

    toAddress = '[email protected]'

    mail.To = toAddress
    mail.Subject = sub
    mail.Body = body
    mail.Send()


# send email automically with attachments
def sendmail():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)

    sub = 'Discourse Score on ' + str(month) + '-' + str(day) + ',' + str(year)
    body = 'Hi, \n' \
           'This is score result on discourse on ' + str(month) + '-' + str(day) + ',' + str(
        year) + ', and please note it. \n' \
                'BR \n' \
                'Hengtai Nie'

    # 可以使用循环列表逐个发送
    # receivers = ['[email protected]', '[email protected]']

    # following receiver's address format
    # toAddress = '[email protected]'
    # toAddress = '[email protected]' + ';' + '[email protected]'
    toAddress = '[email protected]' + ';' + '[email protected]'
    # cc address list
    # cc = '[email protected]' + ';' + '[email protected]'

    # mail.To = receivers
    mail.To = toAddress
    mail.Subject = sub
    mail.Body = body

    # current dir
    abs_folder = os.path.abspath('.')

    txt_path = abs_folder + "\\" + str(year) + "_" + str(month) + "_" + str(
        day) + "\\" + str(year) + "_" + str(month) + "_" + str(day) + "_" + "discourse.txt"
    png_path = abs_folder + "\\" + str(year) + "_" + str(month) + "_" + str(
        day) + "\\" + str(year) + "_" + str(month) + "_" + str(day) + "_" + "discourse_score.png"

    # specify a dir
    # txt_path = "C:\\MySoftWare\\PycharmTest\\mk_folder\\" + str(year) + "_" + str(month) + "_" + str(
    #     day) + "\\" + str(year) + "_" + str(month) + "_" + str(day) + "_" + "discourse.txt"
    # png_path = "C:\\MySoftWare\\PycharmTest\\mk_folder\\" + str(year) + "_" + str(month) + "_" + str(
    #     day) + "\\" + str(year) + "_" + str(month) + "_" + str(day) + "_" + "discourse_score.png"

    # 这里增加一个检测 是否存在要发送的文件,如果有则发送,如果没有则发送一个fail的提示
    file_exist = os.path.isfile(txt_path)
    png_exist = os.path.isfile(png_path)

    if file_exist and png_exist:
        mail.Attachments.Add(txt_path)
        mail.Attachments.Add(png_path)
        mail.Send()
        print("Send  Email Success!")
    else:
        print("File not exist, please check it!")
        send_fail_info()


if __name__ == '__main__':
    sendmail()

你可能感兴趣的:(Python,自动发送邮件)