Changeset 7399 for main


Ignore:
Timestamp:
20 Dec 2011, 07:39:33 (13 years ago)
Author:
Henrik Bettermann
Message:

Define send_mail function and move sendCredentials code from browser.py to utils.py.

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
3 edited

Legend:

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

    r7392 r7399  
    10591059        self.applyData(applicant, **data)
    10601060        self.context.addApplicant(applicant)
    1061         password = getUtility(ISIRPUtils).genPassword()
     1061        sirp_utils = getUtility(ISIRPUtils)
     1062        password = sirp_utils.genPassword()
    10621063        IUserAccount(applicant).setPassword(password)
    10631064        # Send email with credentials
    1064         if self.sendCredentials(applicant, password):
     1065        login_url = self.url(grok.getSite(), 'login')
     1066        if sirp_utils.sendCredentials(applicant, password, login_url):
    10651067            self.redirect(self.url(self.context, 'registration_complete',
    10661068                                   data = dict(email=applicant.email)))
     
    10691071            self.flash('Email could not been sent. Please retry later.')
    10701072        return
    1071 
    1072     def sendCredentials(self, applicant, password):
    1073         """Send credentials as email.
    1074 
    1075         Input is the applicant for which credentials are sent and the
    1076         password.
    1077 
    1078         Returns True or False to indicate successful operation.
    1079         """
    1080         sirp_utils = getUtility(ISIRPUtils)
    1081         username = applicant.applicant_id
    1082         fullname = applicant.display_fullname
    1083         subject = 'Your SIRP credentials'
    1084         msg = 'You have successfully been registered for the'
    1085         email_to = applicant.email
    1086         login_url = self.url(grok.getSite(), 'login')
    1087         success = sirp_utils.sendPassword(fullname,msg,username,
    1088             password,login_url,email_to,subject)
    1089         return success
    10901073
    10911074class ApplicantRegistrationEmailSent(SIRPPage):
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/browser_templates/applicantregemailsent.pt

    r7379 r7399  
    66  <p>
    77    An email with your user name and password
    8     has been sent to <span tal:replace="view/email">user@foo.baz</span>
     8    has been sent to <span tal:replace="view/email">user@foo.baz</span>.
    99  </p>
    1010  <p>
     
    1212  </p>
    1313  <p>
    14     Thanks for using SIRP.
     14    Thanks for using SIRP!
     15  </p>
    1516</div>
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/utils.py

    r7382 r7399  
    2727from email.Header import Header
    2828from email.Utils import parseaddr, formataddr
     29
     30def send_mail(from_name,from_email,rcpt_name,rcpt_email,subject,body,config):
     31
     32    header_charset = 'ISO-8859-1'
     33    from_ = '%s <%s>' % (from_name,from_email)
     34    to_ = '%s <%s>' % (rcpt_name,rcpt_email)
     35    from_name, from_addr = parseaddr(from_)
     36    rcpt_name, rcpt_addr = parseaddr(to_)
     37    from_name = str(Header(unicode(from_name), header_charset))
     38    rcpt_name = str(Header(unicode(rcpt_name), header_charset))
     39    from_addr = from_addr.encode('ascii')
     40    rpct_addr = rcpt_addr.encode('ascii')
     41
     42    for body_charset in ['US-ASCII', 'ISO-8859-1', 'UTF-8']:
     43        try:
     44            body.encode(body_charset)
     45        except UnicodeError:
     46            print "FAILED: ", body_charset, text
     47            pass
     48        else:
     49            break
     50
     51    msg = MIMEText(body.encode(body_charset), 'plain', body_charset)
     52    msg['From'] = formataddr((from_name, from_addr))
     53    msg['To'] = formataddr((rcpt_name, rcpt_addr))
     54    msg['Subject'] = Header(unicode(subject), header_charset)
     55    server = smtplib.SMTP(config.smtp_server)
     56    if config.smtp_requires_login:
     57        server.login(config.smtp_username,config.smtp_password)
     58    try:
     59        server.sendmail(from_email,rcpt_email,msg.as_string())
     60    except Exception, e:
     61        # XXX: log error message
     62        return False
     63    server.quit()
     64    return True
    2965
    3066
     
    72108        return ''.join([r().choice(chars) for i in range(length)])
    73109
    74     def sendPassword(self,fullname,msg,username,password,login_url,
    75                  email_to,subject):
    76         """Send an email with user credentials.
     110
     111    def sendCredentials(self, applicant, password, login_url):
     112        """Send credentials as email.
     113
     114        Input is the applicant for which credentials are sent and the
     115        password.
     116
     117        Returns True or False to indicate successful operation.
    77118
    78119        XXX: While this method is unicode-proof (i.e. it can handle
     
    81122             reorganization of SMPT-related code is needed here.
    82123        """
    83         config = grok.getSite()['configuration']
    84         header_charset = 'ISO-8859-1'
     124        username = applicant.applicant_id
     125        subject = 'Your SIRP credentials'
     126        msg = 'You have successfully been registered for the'
    85127        text = """Dear %s,
    86128
     
    100142Regards
    101143"""
    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')
     144        config = grok.getSite()['configuration']
     145        from_name = config.name_admin
     146        from_email = config.email_admin
     147        rcpt_name = applicant.display_fullname
     148        rcpt_email = applicant.email
     149        body = text % (
     150                rcpt_name, msg,config.name,username,password,login_url)
     151        return send_mail(
     152            from_name,from_email,rcpt_name,rcpt_email,subject,body,config)
    109153
    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
    120154
    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)
    125         server = smtplib.SMTP(config.smtp_server)
    126         if config.smtp_requires_login:
    127             server.login(config.smtp_username,config.smtp_password)
    128         try:
    129             server.sendmail(config.email_admin,email_to,msg.as_string())
    130         except Exception, e:
    131             # XXX: log error message
    132             return False
    133         server.quit()
    134         return True
     155
Note: See TracChangeset for help on using the changeset viewer.