I had a script made on Python 2.7 that would send an email. The following code worked perfectly on it:
msg = MIMEMultipart(
From = "Name <mail@from.com>",
To = "Name <mail@to.com>",
Date=formatdate(localtime=True),
Subject="SUBJECT")
msg.attach(MIMEText("""HTML Stuff""", 'html'))
with open(analysis_file, "rb") as fil:
msg.attach(MIMEApplication(
fil.read(),
Content_Disposition='attachment; filename="%s"' % os.path.basename(analysis_file),
Name=os.path.basename(analysis_file)
))
msg['Subject'] = "SUBJECT"
msg['From'] = "Name <mail@from.com>"
msg['To'] = "Name <mail@to.com>"
server.sendmail(FROMADDR, ["mail@to.com"], msg.as_string())
I converted the script to Python 3.5, and all the other parts of the script work exactly as expected. The above, however, does not: instead of sending my email with the HTML Stuff and the attachment, it sends a blank email with an extensionless file called "noname" which I presume has the stuff I wanted to send.
I tried removing the attachment part and keeping only the HTML stuff, but still no deal.
Why does this happen, and how can I fix it?
(The reason I put Subject, From, and To twice is that when I used it only the first time it wouldn't work for some reason, and I never removed it from there after it started working.)
I figured it out by rerereading the email documentation and replacing
msg = MIMEMultipart(
From = "Name <mail@from.com>",
To = "Name <mail@to.com>",
Date=formatdate(localtime=True),
Subject="SUBJECT")
with
msg = MIMEMultipart('alternative')