0

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

Rocheteau
  • 43
  • 7
  • As an aside, it looks like your `email` code was written for an older Python version. The `email` module in the standard library was overhauled in Python 3.6 to be more logical, versatile, and succinct; new code should target the (no longer very) new `EmailMessage` API. Probably throw away this code and start over with modern code from [the Python `email` examples documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Jul 26 '22 at 15:35
  • As an email recipient, I would hate to receive a message where **everything** **is** **emphasized.** – tripleee Jul 26 '22 at 17:22

2 Answers2

2

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)
tripleee
  • 175,061
  • 34
  • 275
  • 318
jontec
  • 113
  • 5
  • This should probably be updated to use the modern `EmailMessage` API from Python 3.6+. Then you can also use an attachment name with spaces, and Python will take care of properly RFC2231-encoding it. – tripleee Jul 26 '22 at 17:19
0

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.

zlasd
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '22 at 10:47