Forums

Sending emails with threads

I just started implementing asynchronous emails with Flask and have run into some inconsistency. Only a small number of emails are sent and all of the emails with a file attached are not. I searched the forums, but couldn't determine if threads are still disabled or if there is a way to use them? My code works find on my local machine, but below are the common errors that keep popping up in my error log

2015-02-28 03:12:47,551 :Exception in thread Thread-2:
2015-02-28 03:12:47,553 :Exception in thread Thread-1:
2015-02-28 03:12:47,557 :Traceback (most recent call last):
2015-02-28 03:12:47,557 :Traceback (most recent call last):
2015-02-28 03:12:47,557 :  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
2015-02-28 03:12:47,557 :  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
2015-02-28 03:12:47,557 :    self.run()
2015-02-28 03:12:47,557 :    self.run()
2015-02-28 03:12:47,558 :  File "/usr/lib/python2.7/threading.py", line 763, in run
2015-02-28 03:12:47,558 :  File "/usr/lib/python2.7/threading.py", line 763, in run
2015-02-28 03:12:47,558 :    self.__target(*self.__args, **self.__kwargs)
2015-02-28 03:12:47,558 :    self.__target(*self.__args, **self.__kwargs)
2015-02-28 03:12:47,558 :  File "/home/leapfrog/proj/controllers.py", line 41, in send_async_email
2015-02-28 03:12:47,558 :  File "/home/leapfrog/proj/controllers.py", line 41, in send_async_email
2015-02-28 03:12:47,558 :    mail.send(msg)
2015-02-28 03:12:47,558 :    mail.send(msg)
2015-02-28 03:12:47,559 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 401, in send
2015-02-28 03:12:47,559 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 400, in send
2015-02-28 03:12:47,560 :    message.send(connection)
2015-02-28 03:12:47,560 :    with self.connect() as connection:
2015-02-28 03:12:47,560 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 110, in __exit__
2015-02-28 03:12:47,560 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 102, in __enter__
2015-02-28 03:12:47,560 :    self.host.quit()
2015-02-28 03:12:47,560 :    self.host = self.configure_host()
2015-02-28 03:12:47,561 :  File "/usr/lib/python2.7/smtplib.py", line 756, in quit
2015-02-28 03:12:47,561 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 114, in configure_host
2015-02-28 03:12:47,561 :    res = self.docmd("quit")
2015-02-28 03:12:47,561 :    host = smtplib.SMTP_SSL(self.mail.server, self.mail.port)
2015-02-28 03:12:47,561 :  File "/usr/lib/python2.7/smtplib.py", line 386, in docmd
2015-02-28 03:12:47,561 :  File "/usr/lib/python2.7/smtplib.py", line 781, in __init__
2015-02-28 03:12:47,561 :    self.putcmd(cmd, args)
2015-02-28 03:12:47,562 :    SMTP.__init__(self, host, port, local_hostname, timeout)
2015-02-28 03:12:47,562 :  File "/usr/lib/python2.7/smtplib.py", line 336, in putcmd
2015-02-28 03:12:47,562 :  File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
2015-02-28 03:12:47,562 :    self.send(str)
2015-02-28 03:12:47,562 :    (code, msg) = self.connect(host, port)
2015-02-28 03:12:47,562 :  File "/usr/lib/python2.7/smtplib.py", line 328, in send
2015-02-28 03:12:47,562 :  File "/usr/lib/python2.7/smtplib.py", line 311, in connect
2015-02-28 03:12:47,563 :    self.sock = self._get_socket(host, port, self.timeout)
2015-02-28 03:12:47,563 :  File "/usr/lib/python2.7/smtplib.py", line 787, in _get_socket
2015-02-28 03:12:47,563 :    new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
2015-02-28 03:12:47,563 :    raise SMTPServerDisconnected('please run connect() first')
2015-02-28 03:12:47,563 :  File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket
2015-02-28 03:12:47,563 :SMTPServerDisconnected: please run connect() first
2015-02-28 03:12:47,563 :    ciphers=ciphers)
2015-02-28 03:12:47,564 :
2015-02-28 03:12:47,564 :  File "/usr/lib/python2.7/ssl.py", line 243, in __init__
2015-02-28 03:12:47,565 :    self.do_handshake()
2015-02-28 03:12:47,565 :  File "/usr/lib/python2.7/ssl.py", line 405, in do_handshake
2015-02-28 03:12:47,565 :    self._sslobj.do_handshake()
2015-02-28 03:12:47,565 :SSLError: [Errno 8] _ssl.c:510: EOF occurred in violation of protocol
2015-02-28 03:12:47,565 :
2015-02-28 03:12:56,407 :Exception in thread Thread-1:
2015-02-28 03:12:56,453 :Traceback (most recent call last):
2015-02-28 03:12:56,453 :  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
2015-02-28 03:12:56,453 :    self.run()
2015-02-28 03:12:56,453 :  File "/usr/lib/python2.7/threading.py", line 763, in run
2015-02-28 03:12:56,453 :    self.__target(*self.__args, **self.__kwargs)
2015-02-28 03:12:56,453 :  File "/home/leapfrog/proj/controllers.py", line 41, in send_async_email
2015-02-28 03:12:56,453 :    mail.send(msg)
2015-02-28 03:12:56,453 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 401, in send
2015-02-28 03:12:56,453 :    message.send(connection)
2015-02-28 03:12:56,453 :  File "/usr/local/lib/python2.7/dist-packages/flask_mail.py", line 110, in __exit__
2015-02-28 03:12:56,453 :    self.host.quit()
2015-02-28 03:12:56,453 :  File "/usr/lib/python2.7/smtplib.py", line 756, in quit
2015-02-28 03:12:56,453 :    res = self.docmd("quit")
2015-02-28 03:12:56,453 :  File "/usr/lib/python2.7/smtplib.py", line 387, in docmd
2015-02-28 03:12:56,453 :    return self.getreply()
2015-02-28 03:12:56,454 :  File "/usr/lib/python2.7/smtplib.py", line 363, in getreply
2015-02-28 03:12:56,454 :    raise SMTPServerDisconnected("Connection unexpectedly closed")
2015-02-28 03:12:56,454 :SMTPServerDisconnected: Connection unexpectedly closed
2015-02-28 03:12:56,454 :

Not using this method of asynchronous emails posses a problem because my application is locked while the email is sent. Also, attaching pictures to the emails but threw this "Error code: 504-loadbalancer". Any ideas? Thanks =)

Hmm. Threads are disabled from web apps, so that could be the cause. But on the other hand, from that stack trace it looks like the server you're using to send emails is disconnecting you. What SMTP server are you using?

OK, thanks for the confirmation on the threads. I am using Gmail's SMTP server. Sending emails without attachments doesn't seem to cause any problems, so that may be the issue...

Hmm, maybe. Are they particularly large? I know Gmail has a limit on attachment size, something like 5MB if I remember correctly.

I had that in mind so all emails were around 1MB in size. Now that I know threads are disabled, I'm going to make some adjustments and see if the error repeats. Your time and consideration is appreciated, thanks.

OK, good luck! Let us know how you get on.

Just a follow up... I disabled asynchronous emails, used the standard/general configuration for Flask-Mail and opted not to attach documents to the email. I was concerned the app would lock up for a bit while the email sends, but with a text-only email there was no noticeable effect.

yep! sending emails really doesn't take that long...