That is the normal behaviour for both mail and mailx.
To send an attachment you need to format the mail to be using attachments, and that requires other tools.
Unfortunately every time I settle on using some tool to send files as attachments, a couple of years later I find that tool no longer readially available! I got so sick of it I wrote a script to attach files (text or binaries) to raw mails using plain test tools, and sent them using sendmail (or whatever is pretending to be sendmail). I would not however NOT like to inflict the resulting script hell on to anyone else!
Tools I have used in the past (found in obsoleted code blocks on my mailfile script)...
mhbuild
metasend
and others I no longer even try to use.
More manual tools used to create mime attachments
file (for mimetyping, but parsing normal output and directly using file -bi )
b64encode (to encode binary files)
uuencode (yes I have been mailing files around long before mime types or even the web existed)
Example from my script to send mail generated using mhbuild
Code:
SENDMAIL="/usr/lib/sendmail -t" # path can vary
#users= who to mail to
#file= the file to attach
#type= the mimetype of the file
#name= the name of the file (without path components)
( echo "To: $users"
echo "Subject: MailFile "$name""
echo "Precedence: bulk"
echo "" # end of header
echo "#$type; name="$name" $file"
) | mhbuild - | $SENDMAIL
Example using metasend (which calls sendmail itself), is much easer, if you can find a copy.
Code:
metasend -b -t "$users" -s "MailFile "$name"" \
-m "$type" -f "$name" -D "$name"
My plain text sender is much more complicated (though still very linear in processing) as it also has to generated content_id's and boundary lines, and b64encode binary files, if needed.
Code:
BOUNDARY="----- =_aaaaaaaaaa0"
CONTENT_ID="Content-ID: <$$.`date +s`.%d@$HOST>\n" # printf format
( echo "To: $users"
echo "Subject: MailFile \"$name\""
echo "Precedence: bulk"
# MIME Encoded Mail
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=\"$boundary\""
printf "$CONTENT_ID" 0
echo "" # end of header
echo "--$BOUNDARY" # -----------------
echo "Content-Type: $type; name=\"$name\""
printf "$CONTENT_ID" 1
case "$type" in
text/*) # plain or html text file
echo "" # end of sub-heading
cat "$file" # raw text file
;;
*) # assume binary format file
echo "Content-Transfer-Encoding: base64"
echo "" # end of sub-heading
b64encode < "$file" # mime (base64) file encoding
;;
esac
echo "" # end of sub-section
echo "--$BOUNDARY" # -----------------
echo "Content-Type: text/plain; charset=\"us-ascii\""
printf "$CONTENT_ID" 2
echo "" # end of signature sub-heading
signed # add output of a signature script
) $SENDMAIL
That should give you a start for your own mailfile script. And yes the scripted form does work!