0

I am trying to send an email with CSV file as attachment and also print the data available in the text file as Email body using the below shell scripts. But im getting only attachment and there is no Email body available in the email which I have got after the execution of the below scripts , can someone help to resolve the issue.

Script Execution:

sh <script_name> CSV File 2012 

Code - 1 :

#!/bin/bash
export MAILTO="abc@hello.com"
export SUBJECT="Test - \"$1\""
export BODY="$2.txt"
export ATTACH_CSV=""$2"_"$3".csv"
csv_file=$( basename "$ATTACH_CSV" )
{
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "Content-Type: multipart/mixed; boundary=\"FILEBOUNDARY\""
echo "--FILEBOUNDARY"
echo "Content-Type: multipart/mixed; boundary=\"MSGBOUNDARY\""
echo "--MSGBOUNDARY"
echo "Content-Type: text/html; charset=iso-8859-1"
echo "Content-Transfer-Encoding: 8bit"
#echo "Content-Disposition: inline"
cat "$BODY"
echo "--MSGBOUNDARY--"
echo "--FILEBOUNDARY"
echo "Content-Type: text/csv"
echo "Content-Disposition: attachment; filename=\"${csv_file}\""
echo "Content-Transfer-Encoding: uuencode"
uuencode "$ATTACH_CSV" "$csv_file"
echo "--FILEBOUNDARY--"
} | sendmail -t

Code - 2 :

#!/bin/bash
export MAILTO="abc@hello.com"
export SUBJECT="Test - \"$1\""
export BODY="$2.txt"
export ATTACH_CSV=""$2"_"$3".csv"
csv_file=$( basename "$ATTACH_CSV" )
{
        echo "To: $MAILTO"
        echo "Subject: $SUBJECT"
        echo "Content-Type: multipart/mixed; boundary=\"FILEBOUNDARY\""

        echo "--FILEBOUNDARY"
        echo "Content-Type: multipart/alternative; boundary=\"MSGBOUNDARY\""

        echo "--MSGBOUNDARY"
        echo "Content-Type: text/plain; charset=iso-8859-1"
        echo "Content-Transfer-Encoding: 8bit"
        echo "Content-Disposition: inline"
        cat "$BODY"
        echo "--MSGBOUNDARY--"

        echo "--FILEBOUNDARY"
        echo "Content-Type: text/csv"
        echo "Content-Disposition: attachment; filename=\"${csv_file}\""
        echo "Content-Transfer-Encoding: uuencode"
        uuencode "$ATTACH_CSV" "$csv_file"
        echo "--FILEBOUNDARY--"
} | /usr/sbin/sendmail -t
2
  • So the content of the mail is in File.txt?
    – Panki
    Apr 29 '20 at 11:29
  • Yes you are right
    – user78873
    Apr 29 '20 at 11:35
0
  • You're constructing an invalid message. You always need an empty line between message header and body. In your header, you have two occurrences of Content-Type: with different boundary definitions, and the boundary strings between them which only belong in the body to separate MIME parts. I suggest you look up some in-depth information or tutorial on how MIME messages must be structured.
  • When you represent essentially the same information once as text and once as a CSV file, it's probably better to use multipart/alternative to denote that these two MIME parts present the same information in different formats, but this may depend on the recipient's user agent, so your mileage may vary.
6
  • I have updated the another version of the script which i have used. As you specified above i have tried using the multipart/alternative also but the issue is still the same.
    – user78873
    Apr 29 '20 at 13:04
  • You have placed empty lines between the echo commands, but you did not add echo commands to emit empty lines. I suggest instead of piping to sendmail you first store into a file and manually check whether it looks like a valid mail message. Apr 29 '20 at 13:07
  • I have tried it and it looks like valid mail message only, but still not working. Can you correct the script and let me know
    – user78873
    Apr 29 '20 at 13:22
  • I'm sorry I don't have the time and inclination to do that now. You might add the output of your script to your question so people can give recommendations without needing to run your script. Apr 29 '20 at 14:17
  • Output is the same as i told earlier, im getting only CSV file attachment and there is no Email body appearing in the mail
    – user78873
    Apr 29 '20 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.