python:支持附件的邮件发送函数
使用方法:
mail(邮件服务器地址,发件人,收件人,主题,邮件内容)
- import smtplib, sys, MimeWriter, StringIO, base64, os, time
- def mail(serverURL=None, sender='', to='', subject='', text=''):
- message = StringIO.StringIO()
- writer = MimeWriter.MimeWriter(message)
- writer.addheader('Subject', subject)
- writer.startmultipartbody('mixed')
- # start off with a text/plain part
- part = writer.nextpart()
- body = part.startbody('text/plain')
- body.write(text)
- # now add an attachment
- part = writer.nextpart()
- part.addheader('Content-Transfer-Encoding', 'base64')
- body = part.startbody('text/plain')
- base64.encode(open(filename, 'rb'), body)
- # finish off
- writer.lastpart()
- # send the mail
- smtp = smtplib.SMTP(serverURL)
- smtp.sendmail(sender, to, message.getvalue())
- smtp.quit()
没有评论▼