Skip to content
This repository

Moved to use EmailMessage #13

Closed
wants to merge 2 commits into from

1 participant

Vitaly Babiy
Vitaly Babiy
vbabiy commented

This allows us to support the reply-to header.

Vitaly Babiy vbabiy closed this
Vitaly Babiy
vbabiy commented

to many commits, going to recreate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Apr 25, 2012
Vitaly Babiy vbabiy Email template is configurable 78aec03
Jun 07, 2012
Vitaly Babiy vbabiy Use EmailMessage and set reply-to header. 1fbf7c3
This page is out of date. Refresh to see the latest.
11 envelope/forms.py
@@ -28,16 +28,21 @@ class BaseContactForm(forms.Form):
28 28 message = forms.CharField(label=_("Message"), max_length=1000,
29 29 widget=forms.Textarea())
30 30
  31 + def __init__(self, *args, **kwargs):
  32 + self.email_template = kwargs.pop('email_template')
  33 + super(BaseContactForm, self).__init__(*args, **kwargs)
  34 +
31 35 def save(self):
32 36 u"""
33 37 Sends the message.
34 38 """
35 39 subject = settings.ENVELOPE_SUBJECT_INTRO + self.cleaned_data['subject']
36 40 context = self.get_context()
37   - message = render_to_string('envelope/email_body.txt', context)
  41 + message = render_to_string(self.email_template, context)
38 42 try:
39   - mail.send_mail(subject, message, settings.ENVELOPE_FROM_EMAIL,
40   - settings.ENVELOPE_EMAIL_RECIPIENTS)
  43 + mail.EmailMessage(subject, message, settings.ENVELOPE_FROM_EMAIL,
  44 + settings.ENVELOPE_EMAIL_RECIPIENTS,
  45 + headers = {'Reply-To': self.cleaned_data['email']}).send()
41 46 logger.info(_("Contact form submitted and sent (from: %s)") %
42 47 self.cleaned_data['email'])
43 48 except SMTPException:
25 envelope/tests/forms.py
@@ -53,14 +53,14 @@ def test_all_fields_valid(self):
53 53 u"""
54 54 When all required fields are supplied, the form is valid.
55 55 """
56   - form = BaseContactForm(self.form_data)
  56 + form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
57 57 self.assertTrue(form.is_valid())
58 58
59 59 def test_get_context(self):
60 60 u"""
61 61 get_context() returns a copy of form's cleaned_data.
62 62 """
63   - form = BaseContactForm(self.form_data)
  63 + form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
64 64 self.assertTrue(form.is_valid())
65 65 context = form.get_context()
66 66 self.assertEqual(context, form.cleaned_data)
@@ -70,7 +70,8 @@ def test_save(self):
70 70 u"""
71 71 A call to save() on a valid form sends the message.
72 72 """
73   - form = BaseContactForm(self.form_data)
  73 + form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
  74 +
74 75 self.assertTrue(form.is_valid())
75 76 result = form.save()
76 77 self.assertTrue(result)
@@ -81,27 +82,27 @@ def test_save_smtp_error(self):
81 82 u"""
82 83 If the email backend raised an error, the message is not sent.
83 84 """
84   - form = BaseContactForm(self.form_data)
  85 + form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
85 86 self.assertTrue(form.is_valid())
86   - old_send_mail = mail.send_mail
  87 + old_send_mail = mail.EmailMessage
87 88
88   - def new_send_mail(*args):
  89 + def new_send_mail(*args, **kwargs):
89 90 raise SMTPException
90 91
91 92 try:
92   - mail.send_mail = new_send_mail
  93 + mail.EmailMessage = new_send_mail
93 94 result = form.save()
94 95 self.assertFalse(result)
95 96 self.assertEqual(len(mail.outbox), 0)
96 97 finally:
97   - mail.send_mail = old_send_mail
  98 + mail.EmailMessage = old_send_mail
98 99
99 100 def _test_required_field(self, field_name):
100 101 u"""
101 102 Check that the form does not validate without a given field.
102 103 """
103 104 del self.form_data[field_name]
104   - form = BaseContactForm(self.form_data)
  105 + form = BaseContactForm(self.form_data, email_template='envelope/email_body.txt')
105 106 self.assertFalse(form.is_valid())
106 107 self.assertIn(field_name, form.errors)
107 108
@@ -125,7 +126,7 @@ def test_category_field(self):
125 126 Message field is required.
126 127 """
127 128 del self.form_data['category']
128   - form = ContactForm(self.form_data)
  129 + form = ContactForm(self.form_data, email_template='envelope/email_body.txt')
129 130 self.assertFalse(form.is_valid())
130 131 self.assertIn('category', form.errors)
131 132
@@ -133,7 +134,7 @@ def test_get_context(self):
133 134 u"""
134 135 get_context() is overridden and adds a 'category' variable.
135 136 """
136   - form = ContactForm(self.form_data)
  137 + form = ContactForm(self.form_data, email_template='envelope/email_body.txt')
137 138 self.assertTrue(form.is_valid())
138 139 context = form.get_context()
139 140 self.assertIn('category', context)
@@ -143,7 +144,7 @@ def test_get_category_display(self):
143 144 A non-integer field value selects a category labeled "Other".
144 145 """
145 146 self.form_data['category'] = 'not-an-integer'
146   - form = ContactForm(self.form_data)
  147 + form = ContactForm(self.form_data, email_template='envelope/email_body.txt')
147 148 self.assertFalse(form.is_valid())
148 149 self.assertEqual(form.get_category_display(), _("Other"))
149 150
6 envelope/views.py
@@ -52,6 +52,7 @@ class ContactView(FormView):
52 52 """
53 53 form_class = ContactForm
54 54 template_name = 'envelope/contact.html'
  55 + email_template = 'envelope/email_body.txt'
55 56 success_url = None
56 57
57 58 def get_success_url(self):
@@ -81,6 +82,11 @@ def get_initial(self):
81 82 })
82 83 return initial
83 84
  85 + def get_form(self, form_class):
  86 + kwargs = self.get_form_kwargs()
  87 + kwargs['email_template'] = self.email_template
  88 + return form_class(**kwargs)
  89 +
84 90 def form_valid(self, form):
85 91 u"""
86 92 Sends the message and redirects the user somewhere.

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.