sending django email via gmail

3,713 views
Skip to first unread message

Scott Macri

unread,
Mar 6, 2012, 12:28:56 PM3/6/12
to django...@googlegroups.com
I'm attempting to send a message from my django app via gmail and keep
getting a connection refused error even though I know the parameters
are correct.

settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'myu...@gmail.com'
EMAIL_HOST_PASSWORD = 'password'

idle command:
send_mail('My First Subject','My First Email Message in
DJango','myu...@gmail.com',['some...@gmail.com'],fail_silently=False)

I am able to send email through my mail client (Thunderbird).

Any insight on this matter would be greatly appreciated. Thanks.

********* The error message is below ************

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.6/site-packages/django/core/mail/__init__.py",
line 61, in send_mail
connection=connection).send()
File "/Library/Python/2.6/site-packages/django/core/mail/message.py",
line 251, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Library/Python/2.6/site-packages/django/core/mail/backends/smtp.py",
line 79, in send_messages
new_conn_created = self.open()
File "/Library/Python/2.6/site-packages/django/core/mail/backends/smtp.py",
line 42, in open
local_hostname=DNS_NAME.get_fqdn())
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py",
line 239, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py",
line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py",
line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py",
line 512, in create_connection
raise error, msg
error: [Errno 61] Connection refused


--
Scott

nate

unread,
Mar 6, 2012, 1:01:44 PM3/6/12
to django...@googlegroups.com
everything looks correct.  I also use gmail and I have exactly what you have and it is working for me.  The only difference I see is the order and I'm not sure if that makes a difference.  My order is:

EMAIL_USE_TLS
EMAIL_HOST
EMAIL_PORT
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD

Also are you using regular gmail or google apps???

Tom Evans

unread,
Mar 6, 2012, 1:09:35 PM3/6/12
to django...@googlegroups.com

Connection refused is pretty clear. Something is stopping outgoing
connections from reaching smtp.gmail.com.

You didn't specify whether thunderbird and django running on the same
machine - are they?

If you go to a console on the machine where django is running, and
type in "telnet smtp.gmail.com 587", you should get an output like
this:

> $ telnet smtp.gmail.com 587
Trying 173.194.67.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP 9sm82106435wid.2

If not, you must have a firewall somewhere between that box and google
that is refusing the connection.

Cheers

Tom

Scott Macri

unread,
Mar 6, 2012, 4:05:15 PM3/6/12
to django...@googlegroups.com
Here is my telnet response:

Trying 74.125.45.109...


Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.

220 mx.google.com ESMTP v26sm52104286yhk.1

I can run thunderbird on the same machine and don't have any issues.
No, I am not running tbird at the same time as the django app. Thus,
I know it's not a port conflict.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
Scott A. Macri
www.ScottMacri.com
(571) 234-1581

Scott Macri

unread,
Mar 6, 2012, 4:45:07 PM3/6/12
to django...@googlegroups.com
Got this resolved. The issue was that I needed to close my idle
session and reopen it after changing my settings.

CLIFFORD ILKAY

unread,
Mar 6, 2012, 4:52:47 PM3/6/12
to django...@googlegroups.com
On 03/06/2012 04:05 PM, Scott Macri wrote:
> I can run thunderbird on the same machine and don't have any issues.
> No, I am not running tbird at the same time as the django app. Thus,
> I know it's not a port conflict.

It wouldn't make any difference if you were running your Django app and
T'bird at the same time. The port that the Django dev server uses has
nothing to do with the port the Gmail SMTP server uses. Even if your
Django app were to send email at precisely the same time as T'bird, it
wouldn't make any difference. SMTP servers are designed to handle
multiple simultaneous connections.
--
Regards,

Clifford Ilkay
Dinamis
1419-3266 Yonge St.
Toronto, ON
Canada M4N 3P6

<http://dinamis.com>
+1 416-410-3326

infinitylX

unread,
Mar 13, 2012, 6:09:36 PM3/13/12
to Django users
I have same problem.

Telnet works, if i turn off tls and set 25 as port and every thing
works fine. But i want secure connection with tls...
By the way i have same situation with other smtp server (yandex for
example.)

Here is my settings:
EMAIL_USE_TLS = True
EMAIL_HOST = "smtp.googlemail.com"
EMAIL_PORT = 465
EMAIL_HOST_USER = "infin...@gmail.com"
EMAIL_HOST_PASSWORD = "password"

infinitylX

unread,
Mar 14, 2012, 5:57:54 AM3/14/12
to django...@googlegroups.com
Guys when i try to use smtplib directly with next code every things works.

#!/usr/bin/env python
import smtplib

def main():
   print "sending throut gmail"
   username = "infin...@gmail.com"
   password = "password"
   fromaddr = "infin...@gmail.com"
   toaddr = "so...@other.email.com"
   msg = "join the quark"

   server = smtplib.SMTP_SSL('smtp.googlemail.com:465')
   server.login(username, password)
   server.sendmail(fromaddr, [toaddr], msg)
   server.quit()
   print "sended"

if __name__ == '__main__':
    main()


Середа, 14 березня 2012 р., 00:09:36 UTC+2 користувач infinitylX написав:

JJO

unread,
Mar 14, 2012, 10:51:47 AM3/14/12
to Django users
I think you want to use port 587 if you are using TLS. We are using
port 587 successfully with gmail. In any case, Django 1.3.1 does this,
not the code you showed.

self.connection = smtplib.SMTP(self.host, self.port,

local_hostname=DNS_NAME.get_fqdn())
if self.use_tls:
self.connection.ehlo()
self.connection.starttls()
self.connection.ehlo()

So this method will probably not work over port 465.


On Mar 14, 2:57 am, infinitylX <infinit...@gmail.com> wrote:
> Guys when i try to use smtplib directly with next code every things works.
>
> #!/usr/bin/env python
> import smtplib
>
> def main():
>    print "sending throut gmail"
>    username = "infinit...@gmail.com"
>    password = "password"
>    fromaddr = "infinit...@gmail.com"
>    toaddr = "s...@other.email.com"
>    msg = "join the quark"
>
>    server = smtplib.SMTP_SSL('smtp.googlemail.com:465')
>    server.login(username, password)
>    server.sendmail(fromaddr, [toaddr], msg)
>    server.quit()
>    print "sended"
>
> if __name__ == '__main__':
>     main()
>
> Середа, 14 березня 2012 р., 00:09:36 UTC+2 користувач infinitylX написав:
>
>
>
>
>
>
>
>
>
> > I have same problem.
>
> > Telnet works, if i turn off tls and set 25 as port and every thing
> > works fine. But i want secure connection with tls...
> > By the way i have same situation with other smtp server (yandex for
> > example.)
>
> > Here is my settings:
> > EMAIL_USE_TLS = True
> > EMAIL_HOST = "smtp.googlemail.com"
> > EMAIL_PORT = 465
> > EMAIL_HOST_USER = "infinit...@gmail.com"

infinitylX

unread,
Mar 14, 2012, 2:16:38 PM3/14/12
to django...@googlegroups.com
465 is a port for gmail and code i provide is completely working...
but question is why django is not working with same settings...

Karen Tracey

unread,
Mar 14, 2012, 11:22:17 PM3/14/12
to django...@googlegroups.com
2012/3/14 infinitylX <infin...@gmail.com>:

> 465 is a port for gmail and code i provide is completely working...
> but question is why django is not working with same settings...

Because, as JJO stated, the code you show is NOT what Django does when
establishing a secure email connection. The code you showed
establishes an SSL connection to the email server. The
smtplib.SMTP_SSL method you show is a feature new in Python 2.6.
Django still supports Python 2.5, so cannot use this method. The older
way of establishing a secure connection to an email server is to begin
with a non-encrypted connection and upgrade it to secure via the
STARTTLS command. This is what Django does when you specify
EMAIL_USE_TLS=True. Gmail doc notes that for the TLS/STARTTLS case,
you need to use port 587:
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287

Karen
--
http://tracey.org/kmt/

infinitylX

unread,
Mar 15, 2012, 4:27:37 PM3/15/12
to django...@googlegroups.com
Thank guys.

Finally i get why this does not work.
Reply all
Reply to author
Forward
0 new messages