MailShake icon indicating copy to clipboard operation
MailShake copied to clipboard

Long subject & HTML body bring error

Open kibi opened this issue 8 years ago • 1 comments

If you create message with HTML body and long subject:

    email_msg = EmailMessage(
        subject='Rather long subject ............................................................................',
        html='<h1>html message body</h1>'
        from_email='[email protected]',
        to='[email protected]'
    )

It causes to error while sending the message: Traceback (most recent call last): File "C:\Users\ ___\lib\site-packages\mailshake\mailers\smtp.py", line 111, in send_messages sent = self._send(message) File "C:\Users\ ___\lib\site-packages\mailshake\mailers\smtp.py", line 136, in _send self.connection.sendmail(from_email, group, message.as_bytes()) File "C:\Users\ ___\lib\site-packages\mailshake\message.py", line 199, in as_bytes return self.render().as_bytes(unixfrom) File "C:\Users\ ___\lib\site-packages\mailshake\message.py", line 167, in render msg['Subject'] = self.subject File "C:\Users\ ___\lib\site-packages\mailshake\message.py", line 94, in setitem MIMEMultipart.setitem(self, name, val) File "C:\Program Files (x86)\Python35-32\lib\email\message.py", line 410, in setitem self._headers.append(self.policy.header_store_parse(name, val)) File "C:\Program Files (x86)\Python35-32\lib\email\policy.py", line 138, in header_store_parse raise ValueError("Header values may not contain linefeed " ValueError: Header values may not contain linefeed or carriage return characters

kibi avatar Feb 20 '17 11:02 kibi

Note that the subject doesn't need to be that long to hit the issue. In fact, the encoded length matters (https://github.com/python/cpython/blob/407c7343266eb3e5a2f5c1f4913082c84f8dd8a0/Lib/email/policy.py#L145) thus using non-latin characters makes you hit the problem way sooner.

For instance using characters like ř would limit you to around 40 characters :-)

sochi avatar Feb 22 '19 11:02 sochi