Send a mail via Python with a csv files

Question:

I have a probleme, this code works couple weeks ago but doesn’t works now and i don’t know why:

from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

def send_email(send_to, subject, df):
    send_from = "***"
    password = "***"
    message = """
    <p><strong>This is a test email&nbsp;</strong></p>
    <p><br></p>
    <p><strong>Greetings&nbsp;</strong><br><strong>Alexandre&nbsp;</strong></p>
    """

    multipart = MIMEMultipart()
    multipart["From"] = send_from
    multipart["To"] = send_to
    multipart["Subject"] = subject  
    attachment = MIMEApplication(df.to_csv())
    attachment["Content-Disposition"] = 'attachment; filename="{}"'.format(f"{subject}.csv")
    multipart.attach(attachment)
    multipart.attach(MIMEText(message, "html"))
    server = smtplib.SMTP("smtp-mail.outlook.com", 587)
    server.starttls()
    server.login(multipart["From"], password)
    server.sendmail(multipart["From"], multipart["To"], multipart.as_string())
    server.quit()
        
send_email("***", "Résultats extraction de l'analyse du fichier MAP", ListePourCorrection)

Now i recieved an ATT00001.bin and not a ListePourCorrection.csv.

Thanks for your help

Asked By: Rocheteau

||

Answers:

Duplicated with Use crontab job send mail, The email text turns to an attached file which named ATT00001.bin

Answer from sastorsl

Sollution

Explicitly set the locale in your script:

LANG="en_US.UTF8" ; export LANG
Run export in your shell – or setenv if you run csh or tcsh to determine what your > locale is set to.

Answered By: zlasd

I suppose you are using a Pandas dataframe and give it to the function send_email.

I modified something in your code that works for me:

  1. Created a fake dataframe from a dictionary because ListPourCorrection doesn’t exist
  2. Removed the double quotes from filename=
    (don’t use quotes on { }, it’s already a string)
  3. Changed the filename because the subject contains blank spaces.
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import pandas as pd
import smtplib

def send_email(send_to, subject, df):
    send_from = "***"
    password = "***"
    message = """
    <p><strong>This is a test email</strong></p>
    <p><br/></p>
    <p><strong>Greetings</strong><br/><strong>Alexandre</strong></p>
    """

    multipart = MIMEMultipart()
    multipart["From"] = send_from
    multipart["To"] = send_to
    multipart["Subject"] = subject  
    attachment = MIMEApplication(df.to_csv())
    attachment["Content-Disposition"] = "attachment; filename={}".format(f"namewithoutspaces.csv")
    multipart.attach(attachment)
    multipart.attach(MIMEText(message, "html"))
    server = smtplib.SMTP("smtp-mail.outlook.com", 587)
    server.starttls()
    server.login(multipart["From"], password)
    server.sendmail(multipart["From"], multipart["To"], multipart.as_string())
    server.quit()

name_dict = {
            'Name': ['a','b','c','d'],
            'Score': [90,80,95,20]
          }

df = pd.DataFrame(name_dict)
send_email("***", "Résultats extraction de l'analyse du fichier MAP", df)
Answered By: jontec
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.