Index: sitescripts/utils.py |
=================================================================== |
--- a/sitescripts/utils.py |
+++ b/sitescripts/utils.py |
@@ -113,6 +113,25 @@ |
else: |
subprocess.Popen([config.get('DEFAULT', 'mailer'), '-t'], stdin=subprocess.PIPE).communicate(mail.encode('utf-8')) |
+def encode_email_address(email): |
+ ''' |
+ Validates and encodes an email address. |
+ |
+ The validation implemented here is very rudamentery and not meant |
+ to be complete, as full email validation can get extremly complicated |
+ and is rarely needed. This function is primarily making sure that the |
+ email address contains no whitespaces and only valid ASCII characters. |
+ ''' |
+ match = re.search(r'^([^@\s]+)@([^@\s]+)$', email) |
+ if not match: |
+ raise ValueError |
+ |
+ try: |
+ return email.encode('ascii') |
+ except UnicodeEncodeError: |
+ return '%s@%s' % (match.group(1).encode('ascii'), |
+ match.group(2).encode('idna')) |
Wladimir Palant
2015/04/23 16:04:40
This seems to have two issues:
1. match.group(1).
Sebastian Noack
2015/04/23 16:29:41
issubclass(UnicodeEncodeError, ValueError) => True
|
+ |
_template_cache = {} |
def get_template(template, autoescape=True): |