- 论坛徽章:
- 0
|
给你参考一下,这个可以发送附件。
其中 MIMEText 类型可以解决你的问题。
[code]
msg=MIMEMultipart()
msg['From'] = '''pppp@sina.com.cn'''
msg['To'] = '''happy@gmail.com'''
msg['Subject'] = '''corp backup files: ''' + target_bakfile
msg['Reply-To'] = '''pppp@sina.com.cn'''
msg['Date'] = time.ctime(time.time())
msg['X-Priority'] = '''3'''
msg['X-MSMail-Priority'] = '''Normal'''
msg['X-Mailer'] = '''Microsoft Outlook Express 6.00.2900.2180'''
msg['X-MimeOLE'] = '''Produced By Microsoft MimeOLE V6.00.2900.2180'''
body=MIMEText(target,_subtype='plain',_charset='utf-8')
msg.attach(body)
fp = open(target,'rb')
ctype,encoding = mimetypes.guess_type(target)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype,subtype = ctype.split('/',1)
m = MIMEBase(maintype,subtype)
m.set_payload(fp.read())
fp.close()
Encoders.encode_base64(m)
m.add_header('Content-disposition','attachment; filename="%s"' % os.path.basename(target))
msg.attach(m)
smtpServer = smtplib.SMTP('smtp.sina.com.cn')
smtpServer.login('username','pass')
smtpServer.sendmail(msg['From'],msg['To'],msg.as_string())
smtpServer.close()
[/code] |
|