python脚本的一部分在Windows和Linux上的运行情况不同

时间:2018-10-15 19:35:14

标签: python-3.x windows ubuntu

我编写并正在运行一个小程序,该程序可以为我自动执行一些服务器操作,然后准备电子表格报告并发送包含报告结果的电子邮件。

无论如何,当我在Windows计算机上运行该程序时,报告发送就很好了,附件是一个“ xlsx”文件(就像我想要的一样)。当我在Ubuntu服务器上运行该程序时,电子邮件和附件发送正常,但是附件没有扩展名。下载附件时,我可以添加“ xlsx”扩展名,当我这样做时,该文件就可以像电子表格一样正常打开。

这是我的附件代码,用于创建电子邮件并发送。我相信错误就在这里。从ubuntu服务器发送邮件时,如何使我的电子邮件附件具有“ xlsx”扩展名?预先感谢。

def prepEmailWithAttachment(self,
                            subject,
                            body,
                            attachment_path,
                            from_address = mFrom_address,
                            to_address = mTo_address):
    '''preps email with attachment'''

    msg = MIMEMultipart()
    msg["From"] = from_address
    msg["To"] = to_address
    msg["Subject"] = subject

    #make email body readable if it is not already a string
    if not isinstance(body, str): body = body.decode("utf-8")

    msg.attach(MIMEText(body, "plain"))

    #attach attachment
    filename = "HITS"
    part = MIMEBase("application", "xlsx")
    part.set_payload(open(attachment_path, "rb").read())
    encoders.encode_base64(part)


    part.add_header('Content-Disposition',
                    'attachment; filename="%s"' % filename)

    msg.attach(part)

    return msg

def sendEmailWithAttachment(self, message):
    '''sends email with attachment'''

    #server setup
    server = smtplib.SMTP("<my server goes here>", 587)
    server.starttls() #everything after this is encrypted
    server.login("<my email goes here>", <my password goes here>)

    #send message
    server.sendmail(self.getFromAddress(),
                    self.getToAddress(),
                    message.as_string())

    #server shutdown
    server.quit()

0 个答案:

没有答案