mail-instructions.py: Made it work with the UTF-8 encoded names
Jeez, Python-2.4's standard library seems to not be capable of handling unicode strings for the email libraries right away. This version is proven to work.
This commit is contained in:
		
							parent
							
								
									5c7838c5fa
								
							
						
					
					
						commit
						fc7cb36e36
					
				
					 1 changed files with 21 additions and 4 deletions
				
			
		|  | @ -35,8 +35,12 @@ import string | ||||||
| import re | import re | ||||||
| try: | try: | ||||||
|     from email.mime.text import MIMEText |     from email.mime.text import MIMEText | ||||||
|  |     from email.mime.nonmultipart import MIMENonMultipart | ||||||
|  |     from email.charset import Charset | ||||||
| except ImportError: | except ImportError: | ||||||
|     from email.MIMEText import MIMEText |     from email.MIMEText import MIMEText | ||||||
|  |     from email.Charset import Charset | ||||||
|  |     from email.MIMENonMultipart import MIMENonMultipart | ||||||
| 
 | 
 | ||||||
| re_template_fixes = [ | re_template_fixes = [ | ||||||
|     (re.compile(r'^(\s*Dear )<member>', re.MULTILINE), '\\1$member'), |     (re.compile(r'^(\s*Dear )<member>', re.MULTILINE), '\\1$member'), | ||||||
|  | @ -44,8 +48,18 @@ re_template_fixes = [ | ||||||
|     (re.compile(r'^(\s*Vote token:)', re.MULTILINE), '\\1 $token') |     (re.compile(r'^(\s*Vote token:)', re.MULTILINE), '\\1 $token') | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
|  | class MTText(MIMEText): | ||||||
|  |     def __init__(self, _text, _subtype='plain', _charset='utf-8'): | ||||||
|  |         if not isinstance(_charset, Charset): | ||||||
|  |                 _charset = Charset(_charset) | ||||||
|  |         if isinstance(_text,unicode): | ||||||
|  |             _text = _text.encode(_charset.input_charset) | ||||||
|  |         MIMENonMultipart.__init__(self, 'text', _subtype, | ||||||
|  |                                        **{'charset': _charset.input_charset}) | ||||||
|  |         self.set_payload(_text, _charset) | ||||||
|  | 
 | ||||||
| def email_it(recipients_file, instructions_file): | def email_it(recipients_file, instructions_file): | ||||||
|     instructions = file(instructions_file, "r").read().splitlines() |     instructions = file(instructions_file, "r").read().decode('utf-8').splitlines() | ||||||
| 
 | 
 | ||||||
|     from_header = instructions.pop(0) |     from_header = instructions.pop(0) | ||||||
|     subject_header = instructions.pop(0) |     subject_header = instructions.pop(0) | ||||||
|  | @ -56,12 +70,13 @@ def email_it(recipients_file, instructions_file): | ||||||
|     template = string.Template(instructions) |     template = string.Template(instructions) | ||||||
| 
 | 
 | ||||||
|     f = file(recipients_file, "r") |     f = file(recipients_file, "r") | ||||||
|  |     recipient_lines = f.read().decode('utf-8').splitlines() | ||||||
| 
 | 
 | ||||||
|     sent = 0 |     sent = 0 | ||||||
|     errors = 0 |     errors = 0 | ||||||
|     s = None |     s = None | ||||||
| 
 | 
 | ||||||
|     for line in f: |     for line in recipient_lines: | ||||||
|         l = line.strip() |         l = line.strip() | ||||||
|         if l.startswith("#") or l == "": |         if l.startswith("#") or l == "": | ||||||
|             continue |             continue | ||||||
|  | @ -74,17 +89,19 @@ def email_it(recipients_file, instructions_file): | ||||||
| 
 | 
 | ||||||
|         member_name, member_email, token = l |         member_name, member_email, token = l | ||||||
| 
 | 
 | ||||||
|         msg = MIMEText(template.substitute(member=member_name, email=member_email, token=token)) |         payload = template.substitute(member=member_name, email=member_email, token=token) | ||||||
|  |         msg = MTText(payload) | ||||||
|         msg['To'] = member_email |         msg['To'] = member_email | ||||||
|         msg['From'] = from_header |         msg['From'] = from_header | ||||||
|         msg['Subject'] = subject_header |         msg['Subject'] = subject_header | ||||||
|  |         msgstr = msg.as_string() | ||||||
| 
 | 
 | ||||||
|         if s is None: |         if s is None: | ||||||
|             s = smtplib.SMTP() |             s = smtplib.SMTP() | ||||||
|             s.connect('localhost') |             s.connect('localhost') | ||||||
| 
 | 
 | ||||||
|         try: |         try: | ||||||
|             s.sendmail(from_header, [member_email,], msg.as_string()) |             s.sendmail(from_header, [member_email,], msgstr) | ||||||
|         except smtplib.SMTPException: |         except smtplib.SMTPException: | ||||||
|             print "Error: Could not send to %s (%s)!" % (member_email, member_name) |             print "Error: Could not send to %s (%s)!" % (member_email, member_name) | ||||||
|             errors += 1 |             errors += 1 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Tobias Mueller
						Tobias Mueller