Changeset 7382 for main/waeup.sirp/trunk
- Timestamp:
- 18 Dec 2011, 15:32:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/utils.py
r7368 r7382 24 24 from email.mime.text import MIMEText 25 25 from waeup.sirp.interfaces import ISIRPUtils 26 27 from email.Header import Header 28 from email.Utils import parseaddr, formataddr 29 26 30 27 31 class SIRPUtils(grok.GlobalUtility): … … 71 75 email_to,subject): 72 76 """Send an email with user credentials. 77 78 XXX: While this method is unicode-proof (i.e. it can handle 79 non-plain-ASCII chars in names and messages) other 80 methods inhere are not and will fail silently. Some 81 reorganization of SMPT-related code is needed here. 73 82 """ 74 83 config = grok.getSite()['configuration'] 84 header_charset = 'ISO-8859-1' 75 85 text = """Dear %s, 76 86 … … 86 96 your password secret! 87 97 98 Please also note that passwords are case-sensitive. 99 88 100 Regards 89 101 """ 90 msg = MIMEText(text % (fullname,msg,config.name,username,password,login_url)) 91 msg['From'] = '%s <%s>' % (config.name_admin,config.email_admin) 92 msg['To'] = email_to 93 msg['Subject'] = subject 102 from_ = '%s <%s>' % (config.name_admin,config.email_admin) 103 from_name, from_addr = parseaddr(from_) 104 rcpt_name, rcpt_addr = parseaddr(email_to) 105 from_name = str(Header(unicode(from_name), header_charset)) 106 rcpt_name = str(Header(unicode(rcpt_name), header_charset)) 107 from_addr = from_addr.encode('ascii') 108 rpct_addr = rcpt_addr.encode('ascii') 109 110 body = text % ( 111 fullname, msg,config.name,username,password,login_url) 112 for body_charset in ['US-ASCII', 'ISO-8859-1', 'UTF-8']: 113 try: 114 body.encode(body_charset) 115 except UnicodeError: 116 print "FAILED: ", body_charset, text 117 pass 118 else: 119 break 120 121 msg = MIMEText(body.encode(body_charset), 'plain', body_charset) 122 msg['From'] = formataddr((from_name, from_addr)) 123 msg['To'] = formataddr((rcpt_name, rcpt_addr)) 124 msg['Subject'] = Header(unicode(subject), header_charset) 94 125 server = smtplib.SMTP(config.smtp_server) 95 126 if config.smtp_requires_login: … … 97 128 try: 98 129 server.sendmail(config.email_admin,email_to,msg.as_string()) 99 except: 130 except Exception, e: 131 # XXX: log error message 100 132 return False 101 133 server.quit() 102 134 return True 103
Note: See TracChangeset for help on using the changeset viewer.