Use Crontab Job Send Mail, the Email Text Turns to an Attached File Which Named Att00001.Bin

Send a mail via Python with a csv files

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)

Send email with body and attachment with mailx

From mailx's man page :

-a file
Attach the given file to the message.

-f makes mailx process a file as if it was provided on stdin, so you encountered an error because you were providing data to mailx both through stdin and a file.

multiple crontab jobs but only one of them should not send email

See http://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/

You could use something like

0 * * * * test3.sh >/dev/null 2>&1

There will be no output --> no mail sent.

linux mail file.log has Content-Type: application/octet-stream (a noname attachment in Gmail)

The man page is a good place to start! Keep reading until you get to the MIME TYPES section, and pay close attention the following:

Otherwise, or if the filename has no extension, the content types
text/plain or application/octet-stream are
used, the first for text or international text files, the second for any file that contains formatting char‐
acters other than newlines and horizontal tabulators.

So, if your message contains "formatting characters" (which in general means control characters) other than newlines and tabs, it will automatically be classified as application/octet-stream. I bet that if you look closely at the data you'll find some control characters floating around.

You can work around this by...

  • Including the log file as an attachment (using -a) instead of the main message body, and set up your ~/.mime.types file to identify *.log files as text/plain.
  • Filter out control characters using something like tr.
  • Use another MUA such as mutt to send the mail. In fact, you could just craft a message yourself and send it directly to sendmail:

    (
    echo To: person@example.com
    echo From: you@example.com
    echo Subject: a logfile
    echo
    cat logfile.log
    ) | sendmail -t

How to set the From email address for mailx command?

You can use the "-r" option to set the sender address:

mailx -r me@example.com -s ...

The script runs from command line but crontab fails

Your script assumes that it is being run from a particular directory (note that almost every path is a relative path, not an absolute path). cron happens to be running it from another directory.

The Fix

If the script works when you run it from the directory it lives in, add the following to the top of your script:

mydir=$(dirname "$0") && cd "${mydir}" || exit 1

Explanation

$0 is the (possibly relative) filename of the shell script being executed. Given a filename, the dirname command returns the directory containing the filename.

So, that line changes directories to the directory containing the script or exits with an error code if either dirname or cd fails.



Related Topics



Leave a reply



Submit