MattHeisig.com | Web Design and Development in Knoxville, TN

Django send_mail() and Gmail

I’ve run into a problem with sending email through Gmail and trying to set the “From Email” address to something other than the default account address.

I was trying to extend James Bennett’s excellent django-contact-form by adding a few custom fields. All in all a very simple task that should not have taken more than a few minutes.

One of the modifications I wanted to make was to use the email address entered by the user as the from_email address in the email. The client for this project was using Gmail for their corporate email and having all the contact form submissions coming in from the same email address causes them all to be rolled up in the same thread.

I did a straightforward subclass of the AkismetContactForm and overrode the save() method as such:

from django import forms
from django.core.mail import send_mail
from contact_form.forms import AkismetContactForm

class PhoneForm(AkismetContactForm):
    phone = forms.CharField(max_length=12, widget=forms.TextInput(), required=False, label=u'Your phone

    def save(self, fail_silently=False):
        send_mail(self.subject(), self.message(), self.cleaned_data['email'], self.recipient_list, fail_silently=fail_silently)

All well and good. Except the from_email simply wouldn’t change. It would default to the Gmail username used to authenticate with the Gmail SMTP server. I spent quite a bit of time trying to figure out what went wrong and where I screwed up, all to no avail. I’ll admit I’m a beginner when it comes to Django, so I figured I botched something simple somewhere along the way.

I found bits and pieces of discussions online about problems with Gmail, but nothing definitive. On a whim I switched to using a non-Gmail SMTP server to send the email and everything worked perfectly.

Moral of the story: it appears that the Gmail SMTP server forces the from_email address to use the sending accounts email address and won’t allow an alias.

If anyone finds a work around for this or I’m just flat out wrong, let me know!

Other’s Thoughts

There's nothing here yet, why not start something?

Your Thoughts

*(use what your momma gave you)
*(not published)
(linked to your name)
You can use Markdown syntax.

About this Entry

This entry has been tagged with the following: