发送带有Excel附件的电子邮件但Excel工作表已损坏。我该怎么办?

时间:2017-08-11 09:47:50

标签: python excel email

发送电子邮件的命令:scripts / emailSendWithAttachment.py '''''''每​​日报告'''$ latestFileName

虽然正在发送电子邮件,但它表示excel附件已损坏且无法打开。我在这里粘贴代码,以便你们可以看看。

emailSendWithAttachment.py

import os
import sys
import csv
import json
import logging
from datetime import datetime,timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from subprocess import Popen, PIPE


logging.basicConfig(format='[%(asctime)s]: [%(name)s]: [%(levelname)-4s] %(message)s',
                    datefmt = '%Y-%m-%d %H:%M:%S', level = logging.INFO
    )
# Get the total number of args passed to the demo.py
total = len(sys.argv)
print " Total :" + str(total)
print str(sys.argv[0]) +"  " + str(sys.argv[1]) + " " + str(sys.argv[3]) +" " +str(sys.argv[4]) +"  " + str(sys.argv[6])

if( total != 7):
    print ("The total numbers of args should be 6 as EMAIL_FROM,EMAIL_TO,subject,body,Tgt_Dir,File_Name[]")
    sys.exit(0)

if(datetime.now().strftime("%A") == 'Monday'):
        iDate = (datetime.now()+timedelta(days=-3)).strftime('%Y%m%d')
else:
        iDate = (datetime.now()+timedelta(days=-1)).strftime('%Y%m%d')
print iDate


EMAIL_FROM = str(sys.argv[1])
EMAIL_TO = str(sys.argv[2])
subject=str(sys.argv[3]) + iDate
body=str(sys.argv[4]) + iDate
Tgt_Dir =str(sys.argv[5])
File_Name=sys.argv[6]
fileNameArray=File_Name.split(',')


def sendMailMime(emailFrom,emaiTo,subject, body, tgtdir,filename):
        msg = MIMEMultipart()
        msg['From'] = emailFrom
        msg['To'] = emaiTo
        msg['Subject'] = subject
        msg.attach(MIMEText(body))
        print filename
        for file in filename :
                print file
                payload = os.path.join(tgtdir, file)
                with open(payload, 'rb') as f:
                        attachment = MIMEText(f.read())
                        attachment['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(payload)
                        msg.attach(attachment)
                        message = msg.as_string()

        p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
        p.communicate(message)

sendMailMime(EMAIL_FROM,EMAIL_TO,subject,body,Tgt_Dir,fileNameArray)

0 个答案:

没有答案