4

when i run the following i would expect to send an email from my gmail account to my gmail account. Instead i get a timeout error. I have tried tried all possible combinations i could think of including those here

import os

from flask import Flask
from flask.ext.mail import Mail, Message

app =Flask(__name__)
mail=Mail(app)

USERNAME = 'my_gmail_username@gmail.com'
PASSWORD = 'my_gmail_password@gmail.com'

app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_SSL = True,
# MAIL_USE_TSL = True,
MAIL_USERNAME = USERNAME,
MAIL_PASSWORD = PASSWORD,
MAIL_FAIL_SILENTLY=False,
DEBUG = True)


mail=Mail(app)

@app.route("/")
def index():

    msg = Message("Hello",
                  sender=USERNAME,
                  recipients=[USERNAME])
    msg.body = "This is the email body"
    mail.send(msg)
    return "Sent"

if __name__ == "__main__":
    app.run()

I get:

* Running on http://127.0.0.1:5000/
 * Restarting with reloader
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/drew/mail/postoffice.py", line 35, in index
    mail.send(msg)
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask_mail.py", line 415, in send
    with self.connect() as connection:
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask_mail.py", line 123, in __enter__
    self.host = self.configure_host()
  File "/home/drew/.virtualenvs/mail2/lib/python2.7/site-packages/flask_mail.py", line 135, in configure_host
    host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
  File "/usr/lib/python2.7/smtplib.py", line 776, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout)
  File "/usr/lib/python2.7/smtplib.py", line 265, in __init__
    addr = socket.gethostbyname(socket.gethostname())
error: [Errno 110] Connection timed out
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [03/Aug/2013 04:03:49] "GET /?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200   
Drew Verlee
  • 1,880
  • 5
  • 21
  • 30
  • Your code works perfectly for me, it would seem the problem must be with how your server network settings are set up, if you connect to your server can you ping and communicate with the smtp server manually? – Doobeh Aug 03 '13 at 16:40
  • I can also confirm that the code works for me. – AlexLordThorsen Aug 03 '13 at 19:02

1 Answers1

4

The stack trace indicates that the timeout occurs in the socket.gethostbyname call. This function takes a hostname (presumably the "smtp.gmail.com" from your settings) and converts it to an IP address, after doing a DNS lookup.

The fact that the function times out suggests the system on which you are running this is unable to resolve the name smtp.gmail.com.

The most obvious reason for this failure is that the system is not connected to the net, or that the network settings prevent it from connecting.

You can try pinging the hostname from the command line to see what happens:

$ ping smtp.gmail.com

Pinging gmail-smtp-msa.l.google.com [74.125.25.108] with 32 bytes of data:
Reply from 74.125.25.108: bytes=32 time=17ms TTL=47
Reply from 74.125.25.108: bytes=32 time=16ms TTL=47
Reply from 74.125.25.108: bytes=32 time=18ms TTL=47
Reply from 74.125.25.108: bytes=32 time=17ms TTL=47

Ping statistics for 74.125.25.108:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 16ms, Maximum = 18ms, Average = 17ms
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152