views:

80

answers:

1

Hello,

I wrote a simple contact form for a client in Django. However, whenever it sends e-mail, it wraps the header values in u'' objects. For example, the From: header is

From: (u'[email protected]',)

Here's the code that sends the message:

The form:

class ContactForm(forms.Form):
  name = forms.CharField(max_length=100)
  sender = forms.EmailField()
  subject = forms.CharField(max_length=255)
  message = forms.CharField(widget=forms.widgets.Textarea(attrs={'rows':15, 'cols': 72}))

The contact function:

def contact(request):
  RECAPTCHA_PRIVATE_KEY = '******************'
  captcha_error = ''
  if request.method == 'POST':
    form = ContactForm(request.POST)
    captcha_response = captcha.submit(request.POST.get("recaptcha_challenge_field", None),
                      request.POST.get("recaptcha_response_field", None),
                      RECAPTCHA_PRIVATE_KEY,
                      request.META.get("REMOTE_ADDR", None))

    if not captcha_response.is_valid:
      captcha_error = "&error=%s" % captcha_response.error_code
    elif form.is_valid():
      name = form.cleaned_data['name'],
      sender = form.cleaned_data['sender'],
      subject = form.cleaned_data['subject'],
      message = form.cleaned_data['message']

      recipients = ['[email protected]']
      try:
        send_mail(subject, message, sender, recipients)
      except BadHeaderError:
        pass

      flash_message = 'Thank you for contacting us. We will get back to you shortly.'

      return render_to_response('pages/contact.html', {
        'form': form,
        'captcha_error': captcha_error,
        'message': flash_message
      })

It sends the e-mail perfectly, I check the appropriate mailbox and the e-mail appears. But these u'' objects prevent the e-mail's subject from appearing correctly and prevents it from being replied to.

What am I doing wrong?

Thanks in advance.

+6  A: 

Lose the trailing commas here:

    elif form.is_valid():
      name = form.cleaned_data['name']
      sender = form.cleaned_data['sender']
      subject = form.cleaned_data['subject']
      message = form.cleaned_data['message']
MattH
Trailing commas make these items `tuples` with one item.
MattH
Oh and it's an easy mistake to make especially when refactoring code.
MattH
Wow, I would never have guessed that. Thank you very much!
mjaz