Differences between revisions 1 and 2
Revision 1 as of 2007-12-31 06:58:33
Size: 2360
Editor: ZoomQuiet
Comment:
Revision 2 as of 2009-11-28 14:49:53
Size: 2342
Editor: Elias
Comment: 删除对PageComment2组件的引用
Deletions are marked like this. Additions are marked like this.
Line 89: Line 89:
[[PageComment2]]

::-- ZoomQuiet [DateTime(2007-12-31T06:58:33Z)] TableOfContents

Include(CPUGnav)

1. SMTP 带附件

{{{fdcn <[email protected]> reply-to [email protected], to [email protected], date Dec 31, 2007 2:28 PM subject [CPyUG:37544] Re: smtp 附件添加 }}}

  • 示例:

       1 import os
       2 import sys
       3 from smtplib import SMTP
       4 from email.MIMEMultipart import MIMEMultipart
       5 from email.mime.application import MIMEApplication
       6 import time
       7 
       8 def sendFildByMail(config):
       9 
      10     print 'Preparing...', 
      11 
      12     message = MIMEMultipart( ) 
      13     message['from'] = config['from'] 
      14     message['to'] = config['to'] 
      15     message['Reply-To'] = config['from'] 
      16     message['Subject'] = config['subject'] 
      17     message['Date'] = time.ctime(time.time()) 
      18 
      19     message['X-Priority'] =  '3' 
      20     message['X-MSMail-Priority'] =  'Normal' 
      21     message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180' 
      22     message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180' 
      23    
      24     #注意这一段
      25     f=open(config['file'], 'rb') 
      26     file = MIMEApplication(f.read())
      27     f.close()
      28     file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file'])) 
      29     message.attach(file) 
      30    
      31     print 'OK' 
      32     print 'Logging...', 
      33    
      34     smtp = SMTP(config['server'], config['port'])
      35     smtp.ehlo()
      36     smtp.starttls()
      37     smtp.ehlo()
      38     smtp.login(config['username'], config['password'])
      39    
      40     print 'OK'
      41     print 'Sending...',
      42    
      43     smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())
      44    
      45     print 'OK'
      46    
      47     smtp.close()
      48    
      49     time.sleep(1)
      50 
      51 if __name__ == "__main__":
      52     if len(sys.argv) < 2: 
      53         print 'Usage: python %s <file path>' % os.path.basename(sys.argv[0]) 
      54         #sys.exit(-1) 
      55     else:
      56         #587,  25
      57         sendFildByMail({ 
      58             'from': "[email protected]",
      59             'to': '[email protected]', 
      60             'subject': '[pysend]Send file %s' % sys.argv[1], 
      61             'file': sys.argv[1], 
      62             'server': 'smtp.xxx.com', 
      63             'port': 587,
      64             'username': 'username', 
      65             'password': 'password'})
      66     wait=raw_input("end.")
    

MicroProj/2007-12-31 (last edited 2009-12-25 07:13:01 by localhost)