message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
This message has spaces before the keywords From
, To
, Subject
, etc. So these keywords are not recognized as header field names. So it all becomes part of the message body.
To fix this, dedent the text:
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
Or, to preserve the readability of the indentation,
import textwrap
if X == 1:
message = textwrap.dedent("""\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT))