Flask Mail, Maximum Message Length

Maybe I'm missing it somewhere via documentation...

I'm using FLASK_MAIL to send emails.

If I send emails that exceed some sort of message size threshold (which I haven't been able to figure out the number), the system throws an error stating the message is too long.


2019-06-06 16:25:55,996: reply: b'250 2.1.5 OK 50sm1402806qtt.77 - gsmtp\r\n'

2019-06-06 16:25:55,996: reply: retcode (250); Msg: b'2.1.5 OK 50sm1402806qtt.77 - gsmtp'

2019-06-06 16:25:55,997: send: 'data\r\n'

2019-06-06 16:32:32,168: Traceback (most recent call last):

2019-06-06 16:32:32,169: File "/usr/lib/python3.7/logging/", line 939, in emit

2019-06-06 16:32:32,169: self.socket.sendto(msg, self.address)

2019-06-06 16:32:32,169: OSError: [Errno 90] Message too long


This works locally with large files uploaded, but now, only small file sizes + body contents pass through.

Can anyone tell me why?

I feel like this isn't a Flask Mail issue but a logging issue on output, it's to large to output to the log file and throws an error.


Perhaps Google has different size limitations for emails coming from different places or going to different places.

I figured out the problem.

Pythonanywhere seems to crash while sending large emails + attachments when the FLASK Application is in debug mode.

os.environ['FLASK_CONFIG'] = 'production'

Setting the FLASK_CONFIG to production stops the crashing inside the file.

It looks like in debug mode flask_mail tries to log the entire message, so the "message is too long" messge was coming from the logging system. In production mode that logging would not happen. You can probably also turn down the logging level in debug mode so that the entire message is not logged.

I actually tried something like that, even turning logging off outright doing something like below when I tested locally:

import logging
log = logging.getLogger('werkzeug')
log.disabled = True

But the flask_mail output still came through. Ideally whats hosted here shouldn't be in debug mode anyways so Ill just need to keep a mental note moving forward.