Home > Mobile >  Send a mail via Python with a csv files
Send a mail via Python with a csv files

Time:07-26

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

CodePudding user response:

I supposed 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 name because the subject contain 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&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"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)

Hope this help,
Regards

CodePudding user response:

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.

  • Related