Changeset 1798
- Timestamp:
- 12/29/05 16:12:54 (3 years ago)
- Files:
-
- django/trunk/django/core/mail.py (modified) (1 diff)
- django/trunk/docs/email.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/mail.py
r1795 r1798 5 5 import smtplib 6 6 7 class BadHeaderError(ValueError): 8 pass 9 7 10 class SafeMIMEText(MIMEText): 8 11 def __setitem__(self, name, val): 9 12 "Forbids multi-line headers, to prevent header injection." 10 13 if '\n' in val or '\r' in val: 11 raise ValueError, "Header values can't contain newlines (got %r for header %r)" % (val, name)14 raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) 12 15 MIMEText.__setitem__(self, name, val) 13 16 django/trunk/docs/email.txt
r1795 r1798 128 128 by forbidding newlines in header values. If any ``subject``, ``from_email`` or 129 129 ``recipient_list`` contains a newline, the e-mail function (e.g. 130 ``send_mail()``) will raise `` ValueError`` and, hence, will not send the131 e-mail. It's your responsibility to validate all data before passing it to the 132 e-mail functions.130 ``send_mail()``) will raise ``django.core.mail.BadHeaderError`` (a subclass of 131 ``ValueError``) and, hence, will not send the e-mail. It's your responsibility 132 to validate all data before passing it to the e-mail functions. 133 133 134 134 Here's an example view that takes a ``subject``, ``message`` and ``from_email`` … … 136 136 "/contact/thanks/" when it's done:: 137 137 138 from django.core.mail import send_mail 138 from django.core.mail import send_mail, BadHeaderError 139 139 140 140 def send_email(request): … … 142 142 message = request.POST.get('message', '') 143 143 from_email = request.POST.get('from_email', '') 144 if subject and message and from_email \ 145 and '\n' not in subject and '\n' not in message 146 and '\n' not in from_email: 147 send_mail(subject, message, from_email, ['admin@example.com']) 144 if subject and message and from_email: 145 try: 146 send_mail(subject, message, from_email, ['admin@example.com']) 147 except BadHeaderError: 148 return HttpResponse('Invalid header found.') 148 149 return HttpResponseRedirect('/contact/thanks/') 149 150 else:
