Add example about os.path.walk , which may used to implete --recursive option
[xmailer.git] / test / mime.py
blob5eb0e9beb530ec7d71dfc7e19648a7a8a905962c
1 # -*- coding:cp936 -*-
2 #!/usr/bin/env python
3 """
4 MIMEÌí¼Ó¸½¼þ
5 1.Åжϸ½¼þµÄÀàÐÍguess_type·µ»ØÀàÐͺͱàÂë
6 2.Èç¹ûÊÇÎı¾´´½¨MIMEText£¬·ñÔò´´½¨MIMEBase
7 3.´´½¨MIMEMultipart
8 4.ʹÓÃattachÏòMIMEMultipartÖÐ×·¼Ó¶ÔÏó
9 """
10 from email.MIMEText import MIMEText
11 from email.MIMEMultipart import MIMEMultipart
12 from email.MIMEBase import MIMEBase
13 from email import Utils,Encoders
14 import mimetypes,sys
16 def attachment(filename):
17 fd = file(filename,"rb")
18 mimetype,mimeencoding = mimetypes.guess_type(filename)
19 if mimeencoding or (mimetype is None):
20 mimetype = "application/octet-stream"
21 maintype,subtype = mimetype.split("/")
23 if maintype == "text":
24 retval = MIMEText(fd.read(),_subtype = subtype)
25 else:
26 # Èç¹û²»ÊÇtext
27 retval = MIMEBase(maintype,subtype)
28 retval.set_payload(fd.read())
29 Encoders.encode_base64(retval)
30 retval.add_header("Content-Disposition","attachment",filename = filename)
31 fd.close()
32 return retval
34 message = """
35 hello,MIME!
36 ---jcodeer
37 """
38 # ´´½¨MIME£¬²¢Ìí¼ÓÐÅϢͷ
39 msg = MIMEMultipart()
40 msg["To"] = "jcodeer@sina.com"
41 msg["From"]="jcodeer@126.com"
42 msg["Subject"] = "Hello MIME"
43 msg["Date"] = Utils.formatdate(localtime = 1)
45 # ´´½¨MIMEText£¬²¢Ìí¼Óµ½msg
46 body = MIMEText(message,_subtype = "plain")
47 msg.attach(body)
48 # ´´½¨¸½¼þ£¬²¢Ìí¼Óµ½msg
49 fname=raw_input('input file to attach:')
50 msg.attach(attachment(fname))
52 print msg.as_string()