Well, what happens when you do an stdin redirect (the <
), is that the first file - and only the first - is opened and its content passed to mail
's stdin which uses it as the body.
Because your mailx
implementation is old, it does not support the current version's -a
flag to create MIME attachments. What you can do instead is to create the MIME format yourself, using things that are available.
Fortunately its not hard to do using a simple bash script (which I'm 100% sure is available, even on RHEL 5 :-) ), something as simple as this would probably do:
#!/bin/bash
msgid="$RANDOM$RANDOM$RANDOM$RANDOM"
to="$1"
shift
subject="$1"
shift
echo "To: $to"
echo "From: secret-admirer@example.com"
echo "Subject: $subject"
echo "Message-ID: $msgid"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=$msgid"
echo "" # header termination
while [ -n "$1" ]; do
echo "--$msgid"
echo "Content-Type: text/plain; charset=UTF-8"
echo "Content-Disposition: attachment; filename=\"$(basename $1)\""
echo "Content-Transfer-Encoding: base64"
echo ""
base64 < "$1"
shift
done
Then you only need to submit the resulting email text to sendmail
for delivery, something like this:
mail.sh "$SUBJECT" "$EMAIL" /home/user/checks/notprocessed.txt \
/home/user/checks/dirlist.txt | sendmail -t
This script uses the base64
command to "ASCII armor" files, so you can send both text and binary files, though if you are not sending text files you probably want to figure out how to set the attachment's Content-type
correctly (or just change it all to application/octet-stream
, i.e. "binary"). That command is available as part of coreutils on RHEL 5, so I don't expect there to be and problem with using it.
Also note the use of the -t
flag to sendmail
to have the MTA just read the email envelope from the mail itself, so there's no need to put anything else in the sendmail
command arguments. Just make sure that the script includes all the needed fields in the output - I've made it read the recipient and subject from the command line like mailx
does, but its not necessary and the sender address is hard-coded anyway.
You may use sendmail
or "sendmail look alike" provided by postfix/exim/... .
/usr/sbin/sendmail -i -- $recipients < message_file
-i
- do not treat lines with leading dot specially
You may use more exotic "sendmail look alike" (e.g. provided by msmtp
) to send directly via another smtp host without "system wide" configuration.
msmtp
is distributed in debian so it is likely to be included in other linux distributions.
https://packages.debian.org/stretch/msmtp
Package: msmtp (1.6.6-1)
light SMTP client with support for server profiles
msmtp is an SMTP client that can be used to send mails from Mutt and probably other MUAs (mail user agents). It forwards mails to an SMTP server (for example at a free mail provider), which takes care of the final delivery. Using profiles, it can be easily configured to use different SMTP servers with different configurations, which makes it ideal for mobile clients.
Best Answer
mailx can not send attachments and you don't have uuencode. If you have mime-construct, then it is relatively easy:
the output:
together with mailx: