web applications and email

[admin update 2013-10-28: see the wiki section on email/smtp]

Does anyone know how to manage email related activities for a web application? I would like to send out various reminders through email based on various actions that people take as they are interacting with the web application but I'm not sure where to start.

The first thing I guess would be to figure what kind of email server hosting I need but I'm not sure where to start.

Hi David, a free way to get started doing this is to use Google Apps for domains and then use their SMTP server. Eventually, as your application scales, you might hit their daily sending limits. After that their are lots of companies that provide SMTP as a service.

Django has some builtin stuff that makes sending emails quite simple. But using smtplib, in the standard library, is really not that hard. Does that point you in the right direction?

Yes hansel, it does. I had been looking at rackspace and fusemail but I didn't even think of Google Apps.

I've used Google's SMTP in some personal Django apps and it Just Works™. I remember having go and look up IMAP headers for another project when using smtplib directly. But it wasn't that hard.

Actually, I think you don't even need to use Google Apps for domains -- a normal Gmail account will work. I've got a Django app that sends email using these settings:

EMAIL_HOST_PASSWORD = 'some kind of password'

It's worth noting that this will only work on a PythonAnywhere account with unrestricted Internet access, though -- so it's fine for you, davidk01, but won't work for free users. I guess we need to work out something else for them.

Thanks for the info guys. I went ahead registered a domain through Google Apps. I need to learn this stuff anyway and having a dedicated email address for the domain I think will be better in the long run.

I've helped a LOT of people set up Google Apps on their domain so if you trip over something do ask me. If you registered the domain through Google directly though the DNS stuff should all just work anyway.

Thanks for the offer. I got the email part to work from my local machine and I think I got the CNAME stuff figured out as well. I'm using web2py and it is surprisingly straightforward so I don't think I'll run into too many issues when I try to run it on pythonanywhere.

hi, i tried to send email using setting above:


but I have this:

SMTPAuthenticationError: (535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 eb10sm7595930qab.4')

or this:

[Errno 101] Network is unreachable


the password and user are correct


from shell follow code works:

from django.core.mail import send_mail

send_mail('test email', 'hello world', '', ['])

but when i use the same code in i get error


i used use_https=True - it was mistake

The p in p.s. is post, so when you string them it would be p.p.s and p.p.p.s...☺

p.s. the s is script.

i used use_https=True - it was mistake

@xDrgh -- is everything working OK now?

@a2j -- you might like this comic ;-)

@giles -- as a matter of fact I did. Some of the comments were as good as the comic as well...☺


Dinner Party

When: 12 Noon to 2 A.M. in the morning
Where: My house. If you arrive early, youll have to wait outside in the yard.

We have foreign imported wine. Dont bring your own alcoholic drinks.
Dont bring bouquets of flowers either, unless its a fake replica. Some people
have allergies.

*You can bring one fellow colleague. Please RSVP so I can plan in advance.
I need to know the sum total of the people who are arriving so I can prepare the
basic necessities.

Wonderful! One of the best comment threads that I've seen on my LCD display recently... I was delighted to learn it's called RAS syndrome

I saw that too for the first time.

xD, thanks for the correction, yes now it works.

('use_https=False' + unblock on: )

Another incredibly helpful (and unusually amusing) PA forum! I am a noob and this forum helped me get GMAIL set up as my SMTP for rough dev stuff in 5 minutes. Super super helpful.

Hi, I have the same problem described by xDrgh on Aug. 16, 2012, 6:53 p.m. xDrgh's p.p.p.s seems to suggest the problem was caused by a "use_https=True" setting somewhere. Can someone please explain where? I don't have that in my

Just a note to say that @donthireddy and I are working this one out over email, and I'll post the solution here if there's something new and generally useful.

It turned out not to be generally useful, just a bit of Django configuration confusion.

The settings listed above should still work.

I need to be able to send out emails to customers, but using gmail means I can't control the address the users see (I want a 'noreply@mydomain' as the address, and Google forces my normal gmail one in its place.)

I asked my email provider (GoDaddy) for their relay server details so I could pull the same trick using a custom address, but they refused. I'm unsure how best to proceed.

Can anyone suggest anything?

Hmm, tricky. You need an SMTP server that's configured to send stuff for your domain. If GoDaddy don't supply one, then perhaps you could look at Google Apps for your domain? Problem is, of course, that it's not free. But they're the only ones that spring to mind for me.

Since you have a paid account your ability to connect out is unrestricted, hence you could connect directly to SMTP servers. This is potentially a troublesome option, however, as services are starting to distrust SMTP hosts that they don't recognise and which don't match your domain. Also, it means using some pretty low-level libraries to do the SMTP exchange.

If the volume of email you send is pretty low (less than 200 per day) then you can get a free account with SendGrid, who are an outfit set up for people to send automated and bulk email. I had a brief play with their services recently and it seemed to work, but I didn't really push it around. One thing I did notice was that because they include a DKIM signature, some email clients (e.g. Gmail) may show the email as having come "via". The "From" address will still be correct, however.

They offer an SMTP server which will accept email for any destination address (provided you've authenticated via your SendGrid credentials), or they also offer direct APIs instead, including Python wrappers. You can find examples here.

You can use their services to send more than 200 emails a day (indeed, they're aiming at much higher volumes) but then it becomes a paid service.

Just to clarify I haven't used it nearly enough to be recommending it - I don't have a need to send email to anyone but myself, currently. However, it's an option you may like to consider.

Thanks for that Cartroo - I'd hoped some service like that would exist. As long as SendGrid refrain from angrily posting my picture to Twitter then they should be perfect.

Oh yes, I'd forgotten that it was SendGrid who were involved in that whole furore. I remember thinking that there wasn't a single party involved that didn't behave inappropriately in some way, just a massive heap of poor decisions all piled atop one another, a bit like that Paul Chambers debacle. It seemed to me, however, that the biggest crime was against comedy - I mean, innuendo about forking and dongles? Seriously?

Anyway, if you give them a try in a production environment (SendGrid, that is, not poor innuendos) then it would be very helpful to post an update here with your experiences, for anybody else with the same issue.

I will try both SendGrid and poor innuendos in a production environment then.

So far they've been pretty good - got me up and running within a couple of hours. Friendly support team as well. Will let you know how it goes.

Hi, I'm having the same problem than xDrgh, and like donthireddy I don't know where to put this "use_https=False". And my english is not very good, but I think giles said would put the answer but didn't, right?

So... I search on the web and I found some code like this:

context = {
'email': email,
'domain': domain,
'site_name': site_name,
'protocol': use_https and 'https' or 'http',
'key': key

subject = render_to_string(subject_template_name, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())

body = render_to_string(email_template_name, context)

email = EmailMessage(subject, body, to=[email])

But I don't really understand what is going on here, I tried a couple of things and it did'nt work.

I have this code:

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            message = cd['message'] + "\n" + "Comunicate with: " + cd['email']
            email = EmailMessage(cd['subject'], message, to=[''])
            return HttpResponseRedirect('/contact/thanks/')
        form = ContactForm()
    return render_to_response('contact.html', {'form': form}, context_instance=RequestContext(request))

where should I put the "use_https=False"??

Thanks a lot.

What error do you get? I think that use_https is not part of sending the mail, it's used to determine what's in the email that gets sent.

Just going to see again the error and it disapeared.. they were:

SMTPAuthenticationError: (535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 eb10sm7595930qab.4')

or this:

[Errno 101] Network is unreachable

I tried from diferents ways and it works.. thanks :)

Hmm. This might be due to Gmail changing their smtp servers' IP addresses again. Free PythonAnywhere users have filtered Internet access from their web apps, and we need to keep the filter up-to-date whenever the IPs change. It might be that they temporarily moved to a different IP, breaking your code, then back again.

oh.. good to know giles. Thanks :)