1 | ## $Id: utils.py 7359 2011-12-16 07:32:42Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """General helper utilities for SIRP. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | import smtplib |
---|
22 | from email.mime.text import MIMEText |
---|
23 | from waeup.sirp.interfaces import ISIRPUtils |
---|
24 | |
---|
25 | class SIRPUtils(grok.GlobalUtility): |
---|
26 | """A collection of methods subject to customization. |
---|
27 | """ |
---|
28 | grok.implements(ISIRPUtils) |
---|
29 | |
---|
30 | def sendMail(self,fullname,username,usertype,portal,body, |
---|
31 | email_from,email_to,subject): |
---|
32 | """Send an email with data provided by forms. |
---|
33 | """ |
---|
34 | config = grok.getSite()['configuration'] |
---|
35 | text = """Fullname: %s |
---|
36 | User Id: %s |
---|
37 | User Type: %s |
---|
38 | Portal: %s |
---|
39 | |
---|
40 | %s |
---|
41 | """ |
---|
42 | msg = MIMEText(text % (fullname,username,usertype,portal,body)) |
---|
43 | msg['From'] = '%s <%s>' % (fullname,email_from) |
---|
44 | msg['To'] = email_to |
---|
45 | msg['Subject'] = subject |
---|
46 | server = smtplib.SMTP(config.smtp_server) |
---|
47 | if config.smtp_requires_login: |
---|
48 | server.login(config.smtp_username,config.smtp_password) |
---|
49 | try: |
---|
50 | server.sendmail(email_from,email_to,msg.as_string()) |
---|
51 | except: |
---|
52 | return False |
---|
53 | server.quit() |
---|
54 | return True |
---|
55 | |
---|
56 | def fullname(self,firstname,lastname,middlename=None): |
---|
57 | # We do not necessarily have the middlename attribute |
---|
58 | if middlename: |
---|
59 | return '%s %s %s' % (firstname, middlename, lastname) |
---|
60 | else: |
---|
61 | return '%s %s' % (firstname, lastname) |
---|