Content of attachment is displaying along with subject in mailx

Hi All,
I want to send the csv to an email address.
I have tried the below two approaches.

Approach1: Got error -ksh: uuencode: not found

$ uuencode test_file.csv test_file.csv | mailx -s "Attaching test" msdc.kiran@gmail.com </usr/home/test_file.csv
-ksh: uuencode: not found

Approach2:

$ mailx -s "Attaching test" msdc.kiran@gmail.com < /usr/home/test_file.csv 

In Approach2 I have received mail successfully but content of the csv file is displaying along with subject of the email.
But I want it as attachment.The content of the file should not be displayed until unless if we open the file.

Please help me.

Thanks in advance.

Hello ROCK_PLSQL,

Could you please try following and let me know if this helps you.

####Define function here to send email.
sendmail_touser() {
export MAILPART=$(uuidgen)
export MAILPART_BODY=$(uuidgen)
{
        echo "From: $MAILFROM"
        echo "TO: $MAILTO"
        echo "Subject: $SUBJECT"
        echo "MIME-Version: 1.0"
        echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
        echo "--$MAILPART"
        echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
        echo ""
        echo "--$MAILPART_BODY"
        echo "Content-Type: text/html"
        echo "Content-Disposition: inline"
        cat $Input_file_to_be_printed_in_BODY
        echo "--$MAILPART_BODY--"
        echo ""
        echo "--$MAILPART"
        echo "Content-Type: text/plain; name=testCPUID3.txt"
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: attachment; filename=$Input_file_needed_to_be_attached"
        echo ""
        base64 $Input_file_needed_to_be_attached
        echo "--$MAILPART--"
} | /usr/sbin/sendmail -t
}
####Call function here to send email.
sendmail_touser

You could define values of variables above like MAILFROM , MAILTO , SUBJECT , Input_file_to_be_printed_in_BODY and Input_file_needed_to_be_attached as per your need.

Thanks,
R. Singh

Hi Singh,

Thanks for your script.
I have replaced some of the variables.

MAILFROM msdc.kiran@gmail.com
MAILTO msdc.kiran@gmail.com
SUBJECT test_mail
Input_file_needed_to_be_attached test_file.csv

However I got below error.

test_mail.ksh[26]: /usr/bin/sendmail: not found [No such file or directory]

Pease help me.

cat > test_mail.ksh
sendmail_touser() {
export MAILPART=$(uuidgen)
export MAILPART_BODY=$(uuidgen)
{
        echo "From: msdc.kiran@gmail.com"
        echo "TO: msdc.kiran@gmail.com"
        echo "Subject: test_mail"
        echo "MIME-Version: 1.0"
        echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
        echo "--$MAILPART"
        echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
        echo ""
        echo "--$MAILPART_BODY"
        echo "Content-Type: text/html"
        echo "Content-Disposition: inline"
        #cat $Input_file_to_be_printed_in_BODY
        echo "--$MAILPART_BODY--"
        echo ""
        echo "--$MAILPART"
        echo "Content-Type: text/plain; name=testCPUID3.txt"
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: attachment; filename=test_file.csv"
        echo ""
        base64 test_file.csv
        echo "--$MAILPART--"
} | /usr/bin/sendmail -t
}
sendmail_touser

ksh  test_mail.ksh

Thanks.

can you run this command and see it matches with above call ?

which sendmail

For ex, i have the utility in /usr/sbin/sendmail

Also, check PATH vairable as well?

echo $PATH

Hi,

Thanks for your suggestion.

I have executed

which sendmail

According to that I have changed this part

/usr/bin/sendmail -t

to

 /usr/sbin/sendmail -t 

The script is executed successfully but not received mail.
Any thing else is wrong.
Please help me.

Thanks.

Hello ROCK_PLSQL,

Without showing us what changes you have made and what is running and what is not or any error messages you are getting, it is impossible for anyone to tell what's happening. Please let us know complete details with code tags.

Thanks,
R. Singh

Hi,

Please write with more details as RavinderSingh13 said.

However, my apologizes to you to divert to your first post/command .

Seems mailx itself has option to attach a file.

man mailx
-a file
              Attach the given file to the message.

Here is what i tried and it worked successfully.

mailx -s "hello" -a 123.csv myaddress@domainxyz.com <<< hi

I got a mail as follows:
"hi" in body message with subject "hello" and file attachment "123.csv".
i could read 3 lines of its contents.

Please give a try.