-2

编辑新问题的问题 我能够收到电子邮件但这是服务器端的延迟。**

我正在编写非常小的应用程序,用于在 python 中发送带有 excel 文件附件的电子邮件。它包含多个工作表,每个工作表都包含图表。我收到了电子邮件,但看起来文件已损坏。是否可以附加包含图形的 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()
4

1 回答 1

0

这是我用来从 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()

如果您有任何问题,请告诉我

于 2013-10-10T17:36:31.243 回答