Django在标题周围发送电子邮件

时间:2010-02-28 23:32:53

标签: python django email

我在Django中为客户编写了一个简单的联系表单。但是,无论何时发送电子邮件,它都会将标头值包装在u''对象中。例如,From:标题是

From: (u'my@email.com',)

以下是发送消息的代码:

表格:

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}))

联系功能:

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@email.com']
      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
      })

它完美地发送电子邮件,我检查相应的邮箱并显示电子邮件。但是这些u''对象会阻止电子邮件的主题正确显示并阻止它被回复。

我做错了什么?

提前致谢。

1 个答案:

答案 0 :(得分:7)

在这里丢失尾随逗号:

    elif form.is_valid():
      name = form.cleaned_data['name']
      sender = form.cleaned_data['sender']
      subject = form.cleaned_data['subject']
      message = form.cleaned_data['message']