所以我有这个代码:
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个文件,以避免超过gmail邮件的限制。但问题是MIMEBase中的数据。。。我的问题是有没有一种方法可以删除MIMEBase中的所有数据?很抱歉,压痕错了
看来你的问题是:
msg
。在msg
追加21个文件。在当
a==21
时,您应该从一个新的msg
对象开始,而不是继续向旧对象追加越来越多的文件。在或者,您可以尝试在附加新的附件之前删除已经存在的21个附件;但是重新开始可能会更简单,因为您已经准备好了启动具有正确标头的新邮件的代码—它只需要重构为一个小的“start a new message”函数。在
相关问题 更多 >
编程相关推荐