"无法打开包含图表的Excel文件"

2024-09-21 17:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑新问题的问题我可以收到电子邮件,但服务器端延迟了。**

我正在写一个非常小的应用程序发送电子邮件与附件excel文件在python。它包含多个工作表,每个工作表都包含图形。我收到了电子邮件,但看起来文件已损坏。 是否可以附加包含图形的Excel(大小不超过2MB)

# -*- coding: iso-8859-1 -*-
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib,email,email.encoders,email.mime.text,email.mime.base

msg = MIMEMultipart()
msg['Subject'] = 'Email From Python Abhishek'
msg['From'] = 'xyz.com'
msg['To'] = 'abc.com'

fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
fileMsg.set_payload(file('Final.xlsx').read())
email.encoders.encode_base64(fileMsg)
fileMsg.add_header('Content-Disposition','attachment;filename=Final.xlsx')
msg.attach(fileMsg)

smtp = SMTP("email exchange server",25) 
#(I was able to connect with exchange server using Telnet http://www.exchangeinbox.com/article.aspx?i=93)


# Start the server:
smtp.ehlo()

I have commented below code as per one internet posting. it suggest that if you
are sending internal Email you may not require login and password.I also do not
want to write password as it is violate company policy
if I  remove comment from line. it give me error for bad authentication.
#smtp.login("abc", "password")

smtp.sendmail(msg['From'],msg['To'],msg.as_string())
#server.quit()

Tags: fromimportcomserver电子邮件emailasit
1条回答
网友
1楼 · 发布于 2024-09-21 17:40:25

这是一个我用gmail账户发送电子邮件的脚本,理论上它应该对其他人有用,但我只测试过gmail。您可以通过main中列出的参数从命令行调用它,也可以直接从另一个Python模块调用mail函数:

#!/usr/bin/python

# currently set up and tested on gmail only

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os, sys, base64

def mail(gmail_user, enc_pwd, to, subject, body, attach):
    msg = MIMEMultipart()

    msg['From'] = gmail_user
    msg['To'] = to
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'html'))

    if attach:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(attach, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition',
               'attachment; filename="%s"' % os.path.basename(attach))
        msg.attach(part)

    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, base64.b64decode(enc_pwd))
    mailServer.sendmail(gmail_user, to, msg.as_string())
    mailServer.close()

def main():
    if len(sys.argv) <6:
        print "Usage: send_email.py <from> <enc_pwd> <to> <subject> <body> " \
              "[<attachments>]"
        print "Note: Email is being sent in html mode, so any newlines should " \
              "be sent as <br/>"
        if len(sys.argv) > 1:
            print "\nThe following arguements were received:"
            for i in sys.argv:
                print i
    else:
        gmail_user  = sys.argv[1]
        gmail_pwd   = sys.argv[2]
        to          = sys.argv[3]
        subject     = sys.argv[4]
        body        = sys.argv[5]
        attach      = None
        if len(sys.argv) >= 7:
            attach  = sys.argv[6]

        mail(gmail_user, gmail_pwd, to, subject, body, attach)

if __name__ == '__main__':
    main()

如果你有任何问题请告诉我

相关问题 更多 >