PYTHONメールパケット送信メールの使い方

1441 ワード

from email.Header import Header
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import smtplib, datetime

#          
msg = MIMEMultipart()
#    
att = MIMEText('  ', 'plain', 'gb2312')
msg.attach(att)
path = r'D:\Downloads\  .xls'
att = MIMEApplication(open(path, 'rb').read(), 'ms-excel')
att["Content-Disposition"] = 'attachment; filename="  .xls"'
msg.attach(att)
path = r'D:\Downloads\  1.xls'
att = MIMEApplication(open(path, 'rb').read(), 'ms-excel')
att["Content-Disposition"] = 'attachment; filename="  1.xls"'
msg.attach(att)

#    
msg['to'] = '1501942xxxx <[email protected]>'
msg['from'] = '   <[email protected]>'
msg['subject'] = Header('   (2011-12-20)', 'gb2312')
msg['date'] = 'Thu, 1 Dec 2011 16:43:45 +0800'
#    
server = smtplib.SMTP('mx2.mail.139.com')
#server.login('1501942xxxx','***')
#server.set_debuglevel(1)
server.sendmail('[email protected]', msg['to'], msg.as_string())
server.close()