我的任务是通过电子邮件发送200多M的电子书。呵呵,我的邮箱可没有那么大,能把整部书都容得下。那就把“月饼”先切成14块,再一个一个发出去。步骤如下:
1)用7zip分割成14份。每份18M呵呵~~
2)我的运行环境是RedHat 5.7。Python脚本如下:
#! /usr/bin/env python
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import COMMASPACE, formatdate
from email.mime.base import MIMEBase
from email import Encoders
def send_mail(From, To, Subject, Body, Files=[], Server = 'localhost', File_As_Body = None):
assert type(To) == list
assert type(Files) == list
msg = MIMEMultipart()
msg['Subject'] = Subject
msg['From'] = From
msg['To'] = COMMASPACE.join(To)
msg['Date'] = formatdate(localtime=True)
if File_As_Body:
fab = open(File_As_Body, 'rb')
f = fab.read()
msg.attach(MIMEText(f))
else:
msg.attach(MIMEText(Body))
for f in Files:
part = MIMEBase('application', 'octet-stream')
fp = open(f, 'rb')
part.set_payload(fp.read())
fp.close()
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' %os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(Server, 1025)
smtp.login('quinn@email.com', 'password')
#smtp.ehlo()
#smtp.helo()
smtp.sendmail(From, To, msg.as_string())
smtp.close()
book = ['MySQL.7z.001','MySQL.7z.002','MySQL.7z.003','MySQL.7z.004','MySQL.7z.005','MySQL.7z.006','MySQL.7z.007', 'MySQL.7z.008','MySQL.7z.009','MySQL.7z.010','MySQL.7z.011','MySQL.7z.012','MySQL.7z.013','MySQL.7z.014']
for part in book:
send_mail('quinn@email.com',
['user@user.com'],
'MySQL book (14 parts)',
'attached',
[part],
'smtp.server.com')
print part + ' Sent OK!'
print 'All done'
不用多说了,您一看就懂了:)