Advertisement
Guest User

Untitled

a guest
Aug 24th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import re
  2. import email
  3. from email.Utils import parseaddr
  4. from email.Header import decode_header
  5.  
  6. # email address REGEX matching the RFC 2822 spec
  7. # from perlfaq9
  8. # my $atom = qr{[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+};
  9. # my $dot_atom = qr{$atom(?:\.$atom)*};
  10. # my $quoted = qr{"(?:\\[^\r\n]|[^\\"])*"};
  11. # my $local = qr{(?:$dot_atom|$quoted)};
  12. # my $domain_lit = qr{\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\]};
  13. # my $domain = qr{(?:$dot_atom|$domain_lit)};
  14. # my $addr_spec = qr{$local\@$domain};
  15. #
  16. # Python translation
  17.  
  18. atom_rfc2822=r"[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+"
  19. atom_posfix_restricted=r"[a-zA-Z0-9_#\$&'*+/=?\^`{}~|\-]+" # without '!' and '%'
  20. atom=atom_rfc2822
  21. dot_atom=atom + r"(?:\." + atom + ")*"
  22. quoted=r'"(?:\\[^\r\n]|[^\\"])*"'
  23. local="(?:" + dot_atom + "|" + quoted + ")"
  24. domain_lit=r"\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\]"
  25. domain="(?:" + dot_atom + "|" + domain_lit + ")"
  26. addr_spec=local + "\@" + domain
  27.  
  28. email_address_re=re.compile('^'+addr_spec+'$')
  29.  
  30. raw="""MIME-Version: 1.0
  31. Received: by 10.229.233.76 with HTTP; Sat, 2 Jul 2011 04:30:31 -0700 (PDT)
  32. Date: Sat, 2 Jul 2011 13:30:31 +0200
  33. Delivered-To: alain.spineux@gmail.com
  34. Message-ID: <CAAJL_=kPAJZ=fryb21wBOALp8-XOEL-h9j84s3SjpXYQjN3Z3A@mail.gmail.com>
  35. Subject: =?ISO-8859-1?Q?Dr.=20Pointcarr=E9?=
  36. From: Alain Spineux <alain.spineux@gmail.com>
  37. To: =?ISO-8859-1?Q?Dr=2E_Pointcarr=E9?= <alain.spineux@gmail.com>
  38. Content-Type: multipart/alternative; boundary=000e0cd68f223dea3904a714768b
  39.  
  40. --000e0cd68f223dea3904a714768b
  41. Content-Type: text/plain; charset=ISO-8859-1
  42.  
  43. --
  44. Alain Spineux
  45.  
  46. --000e0cd68f223dea3904a714768b
  47. Content-Type: text/html; charset=ISO-8859-1
  48.  
  49.  
  50.  
  51. --
  52. Alain Spineux
  53.  
  54.  
  55. --000e0cd68f223dea3904a714768b--
  56. """
  57.  
  58. def getmailheader(header_text, default="ascii"):
  59. """Decode header_text if needed"""
  60. try:
  61. headers=decode_header(header_text)
  62. except email.Errors.HeaderParseError:
  63. # This already append in email.base64mime.decode()
  64. # instead return a sanitized ascii string
  65. return header_text.encode('ascii', 'replace').decode('ascii')
  66. else:
  67. for i, (text, charset) in enumerate(headers):
  68. try:
  69. headers[i]=unicode(text, charset or default, errors='replace')
  70. except LookupError:
  71. # if the charset is unknown, force default
  72. headers[i]=unicode(text, default, errors='replace')
  73. return u"".join(headers)
  74.  
  75. def getmailaddresses(msg, name):
  76. """retrieve From:, To: and Cc: addresses"""
  77. addrs=email.utils.getaddresses(msg.get_all(name, []))
  78. for i, (name, addr) in enumerate(addrs):
  79. if not name and addr:
  80. # only one string! Is it the address or is it the name ?
  81. # use the same for both and see later
  82. name=addr
  83.  
  84. try:
  85. # address must be ascii only
  86. addr=addr.encode('ascii')
  87. except UnicodeError:
  88. addr=''
  89. else:
  90. # address must match adress regex
  91. if not email_address_re.match(addr):
  92. addr=''
  93. addrs[i]=(getmailheader(name), addr)
  94. return addrs
  95.  
  96. msg=email.message_from_string(raw)
  97. subject=getmailheader(msg.get('Subject', ''))
  98. from_=getmailaddresses(msg, 'from')
  99. from_=('', '') if not from_ else from_[0]
  100. tos=getmailaddresses(msg, 'to')
  101.  
  102. print 'Subject: %r' % subject
  103. print 'From: %r' % (from_, )
  104. print 'To: %r' % (tos, )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement