I am trying to send .csv file generated from dictionary using smtplib and MIME. I am able to send correct data to mail, but for some reason the data is always extended as .htm when downloaded from the mail itself. So my question is - how to set the MIME correctly, so the data is always in .csv data type. Here is my code as of now:
def new_email(df):
message = MIMEMultipart()
message['Subject'] = "New data"
message['From'] = "someGenericMail@outlook.com"
message['To'] = "someOthergGenericMail@gmail.com"
bio = io.BytesIO()
df.to_csv(bio,mode="wb")
bio.seek(0)
attachement = MIMEApplication(bio.read(),"csv")
bio.close()
attachement.add_header("Content-Disposition", "attachement", filename="Results.csv")
body = MIMEText("Here is your data", 'plain')
message.attach(body)
message.attach(attachement)
with smtplib.SMTP("smtp.office365.com", 587) as server:
server.starttls()
server.login("someGenericMail@outlook.com","password")
server.sendmail("someGenericMail@outlook.com","someOthergGenericMail@gmail.com", message.as_string())
This is what I get when I download attachement from the recieved mail: