使用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 hengtai.nie@ercisson.com
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 = 'hengtai.nie@ericsson.com'

    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 = ['1904719059@qq.com', 'hengtai.nie@ericsson.com']

    # following receiver's address format
    # toAddress = 'hengtai.nie@ericsson.com'
    # toAddress = '1904719059@qq.com' + ';' + 'hengtai.nie@ericsson.com'
    toAddress = 'tao.sha@ericsson.com' + ';' + 'ning.n.zhang@ericsson.com'
    # cc address list
    # cc = '1904719059@qq.com' + ';' + 'just649@gmail.com'

    # 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()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值