English 中文(简体)
• 如何清除监测和评价战略的所有数据(电子邮件模块)
原标题:How to clear all data in MIMEBase (email module)
  • 时间:2011-10-02 05:54:12
  •  标签:
  • python
  • email

(一) 本法典:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os

def sendMail(to, subject, text, files=[],server="smtp.gmail.com:587"):
    assert type(to)==list
    assert type(files)==list
    fro = "psaoflamand@live.com>"

    msg = MIMEMultipart()
    msg[ From ] = fro
    msg[ To ] = COMMASPACE.join(to)
    msg[ Date ] = formatdate(localtime=True)
    msg[ Subject ] = subject

    msg.attach( MIMEText(text) )
    a=0
    username =  psaoflamand@gmail.com   
    password =  pass   

    # The actual mail send  


    smtp = smtplib.SMTP(server)
    smtp.starttls()  
    smtp.login(username,password)

    for file in files:
        a+=1
        print a
        part = MIMEBase( application , "octet-stream")
        part.set_payload( open(file,"rb").read() )
        Encoders.encode_base64(part)
        part.add_header( Content-Disposition ,  attachment; filename="%s" 
                       % os.path.basename(file))
        msg.attach(part)
        if a==21:
            smtp.sendmail(fro, to, msg.as_string() )
            a=0
            print  sent 


    smtp.quit()


sendMail(
        ["psaoflamand@live.com"],
        "hello","cheers",
        ["Thousands of one megabyte files"]

在该法典中,它及时发送了21份档案,以避免超出电子邮件信息限额。 但是,问题在于,监测和评价战略中的数据保持不变......我的问题是,能否删除监测和评价战略中的所有数据? 令人担心的是,诱惑是错误的

最佳回答

问题在于:

  1. Create a msg.
  2. Append 21 files to msg.
  3. Send it.
  4. Append 21 more files, so that it has 42 files now attached.
  5. Send it again; this second message is twice as large as the first.
  6. Append 21 more files, bringing the total to 63.
  7. Send it again; it s getting pretty huge now.
  8. And so forth.

<代码>a=21 您应从新的<代码>sg的物体开始,而不是继续将更多的档案传给旧文件。

Alternately, you could try removing the 21 attachments already there before attaching the new ones; but just starting over might be simpler, since you already have the code in place to start a new message with the right headers — it just needs refactoring into a little “start a new message” function.

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签