Running this script on: “Red Had Enterprise Linux Server release 5.11 (Tikanga)”
Mail is: version 8.1 6/6/93 (appears quite old).
Have tried various things and cannot seem to get this to work..
echo "Email message" | mail -a /home/user/checks/notprocessed.txt -a /home/user/checks/dirlist.txt -s "Subject" <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394b5c5a5049505c574d795a565449585740175a5654">[email protected]</a> /home/user/checks
Resulting message:
mail: invalid option — a
mutt is not installed and I have limited access to this system as it is owned by the company I work for. I could add in other components, but has to go through change management, and related processes.
uuencode is not available either.
Here is what I am using today and I am trying to add a second file to:
if grep 'Not Empty' /home/user/checks/notprocessed.txt then echo "files were found"; SUBJECT="NIGHTLY DIRECTORY LISTING OF FILES SENT TO VENDOR"; FROM="SENDER<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2152444f45445361424e4c51404f580f424e4c">[email protected]</a>>" # EMAIL="JOHN DOE<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9d8a8c869f868a819baf8c80829f8e8196c18c8082">[email protected]</a>>" EMAIL="MARY DOE<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f48691979d849d919a80b4979b9984959a8dda979b99">[email protected]</a>>"; /bin/mail -s "$SUBJECT" "$EMAIL" -- -f "$FROM" < /home/user/checks/notprocessed.txt /home/user/checks/dirlist.txt exit -20 fi
And.. /home/user/checks/dirlist.txt is not attached or included as part of the e-mail. I would like /home/user/checks/dirlist.txt to be actually an attachment
This actually does work – but includes the notprocessed.txt as the body of the email..
if grep 'Not Empty' /home/user/checks/notprocessed.txt then echo "files were found"; SUBJECT="NIGHTLY DIRECTORY LISTING OF FILES SENT TO VENDOR"; FROM="SENDER<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3546505b51504775565a5845545b4c1b565a58">[email protected]</a>>" # EMAIL="JOHN DOE<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b796e68627b626e657f4b6864667b6a657225686466">[email protected]</a>>" EMAIL="MARY DOE<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="374552545e475e5259437754585a4756594e1954585a">[email protected]</a>>"; /bin/mail -s "$SUBJECT" "$EMAIL" -- -f "$FROM" < /home/user/checks/notprocessed.txt exit -20 fi
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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: <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dfee8eeffe8f9a0ece9e0e4ffe8ffcde8f5ece0fde1e8a3eee2e0">[email protected]</a>"
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0