Changeset 7471
- Timestamp:
- 14 Jan 2012, 12:45:17 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/utils.py
r7407 r7471 19 19 """ 20 20 import grok 21 import smtplib22 21 import string 23 22 from random import SystemRandom as r 24 from email.mime.text import MIMEText25 23 from waeup.sirp.interfaces import ISIRPUtils 26 27 from email.Header import Header 28 from email.Utils import parseaddr, formataddr 24 from waeup.sirp.smtp import send_mail as send_mail_internally 29 25 30 26 def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): 27 """Wrapper for the real SMTP functionality in :mod:`waeup.sirp.smtp`. 28 29 Merely here to stay compatible with lots of calls to this place. 31 30 """ 32 XXX: While this method is unicode-proof (i.e. it can handle 33 non-plain-ASCII chars in names and messages) other 34 methods inhere are not and will fail silently. Some 35 reorganization of SMPT-related code is needed here. 36 """ 37 38 header_charset = 'ISO-8859-1' 39 from_name = str(Header(unicode(from_name), header_charset)) 40 rcpt_name = str(Header(unicode(rcpt_name), header_charset)) 41 from_addr = from_addr.encode('ascii') 42 rpct_addr = rcpt_addr.encode('ascii') 43 for body_charset in ['US-ASCII', 'ISO-8859-1', 'UTF-8']: 44 try: 45 body.encode(body_charset) 46 except UnicodeError: 47 #print "FAILED: ", body_charset, body 48 pass 49 else: 50 break 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_addr,rcpt_addr,msg.as_string()) 60 except Exception, e: 61 # XXX: log error message 62 return False 63 server.quit() 31 mail_id = send_mail_internally( 32 from_name, from_addr, rcpt_name, rcpt_addr, 33 subject, body, config) 64 34 return True 65 66 35 67 36 class SIRPUtils(grok.GlobalUtility):
Note: See TracChangeset for help on using the changeset viewer.