Ignore:
Timestamp:
18 Dec 2011, 15:32:10 (13 years ago)
Author:
uli
Message:

Dirty hack to enable unicode-aware SMTP handling. This is still not
sufficient.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/utils.py

    r7368 r7382  
    2424from email.mime.text import MIMEText
    2525from waeup.sirp.interfaces import ISIRPUtils
     26
     27from email.Header import Header
     28from email.Utils import parseaddr, formataddr
     29
    2630
    2731class SIRPUtils(grok.GlobalUtility):
     
    7175                 email_to,subject):
    7276        """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.
    7382        """
    7483        config = grok.getSite()['configuration']
     84        header_charset = 'ISO-8859-1'
    7585        text = """Dear %s,
    7686
     
    8696your password secret!
    8797
     98Please also note that passwords are case-sensitive.
     99
    88100Regards
    89101"""
    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)
    94125        server = smtplib.SMTP(config.smtp_server)
    95126        if config.smtp_requires_login:
     
    97128        try:
    98129            server.sendmail(config.email_admin,email_to,msg.as_string())
    99         except:
     130        except Exception, e:
     131            # XXX: log error message
    100132            return False
    101133        server.quit()
    102134        return True
    103 
Note: See TracChangeset for help on using the changeset viewer.