4

By using find command I got multiple files. Now I want to add all these files as mail attachment. How do I add these files as attachment in single mail?

I want to implement this in script. Do I need to use for loop and store the files in array variables?

EX: I got 3 files results by the following

find . -type f -name "sum*"

result:

sum123.pdf
sum234.pdf
sum453.pdf
4

You can do it with mutt like this:

mutt -a $(find . -type f -name "sum*")

If you want to do it non-interactive, try

mutt -s "Subject" -a $(find . -type f -name "sum*") -- user@cats.com < /dev/null

If mutt is not installed, here is an example with mail and more tools (e.g. mpack)!

So it should be something like

#!/bin/bash
# This needs heirloom-mailx
from="sender@example.com"
to="recipient@example.org"
subject="Some fancy title"
body="This is the body of our email"

declare -a attargs
for att in $(find . -type f -name "sum*"); do
  attargs+=( "-a"  "$att" )  
done

mail -s "$subject" -r "$from" "${attargs[@]}" "$to" <<< "$body"

For a sh environment without declare:

#!/bin/sh
# This needs heirloom-mailx
from="sender@example.com"
to="recipient@example.org"
subject="Some fancy title"
body="This is the body of our email"

attargs=""
for att in $(find . -type f -name "sum*"); do
  attargs="${attargs}-a $att "  
done
attargs=${attargs::-1}

mail -s "$subject" -r "$from" ${attargs[@]} "$to" <<< "$body"
  • can you install it? It's very useful in this situation. Otherwise i'll try to find a mail equivalent. What mail programs are installed on your system? – polym Jun 19 '14 at 14:56
  • Have you tried the code above? – polym Jun 19 '14 at 15:06
  • ah ok then use attargs=() instead of declare -a attargs. Do you use bash or sh? You can find it out via echo $BASH. I think declare is a bash builtin only. If you run this step by step, better save this script as mymail.sh e.g., run chmod +x ./mymail.sh and finally ./mymail.sh. – polym Jun 19 '14 at 15:18
  • Did it work? If so, please accept the suggestion as solution, otherwise I am trying to refine the code to solve your issue. – polym Jun 19 '14 at 15:38
  • It is showing like Syntax error at line 5 : `(' is not expected line is: attargs=() – Aravind Jun 19 '14 at 15:39
0
ATTACH_FILE=`ls $HOME/data/*log.txt`
rmdat $HOME/file.dat
rmdat $HOME/sendemail.dat
ATTACH_FILE="$(echo $ATTACH_FILE | sed 's/ /\\n/g')"
export FILE=$HOME/file.dat
export FILE1=$HOME/sendemail.dat
echo $ATTACH_FILE >> $FILE
ATT_FILES=""
while read BP_fl
do

ATT_FILES=$ATT_FILES" uuencode  $BP_fl $(basename $BP_fl) ;"

done < $HOM/file.dat

echo '( echo "Hi" ; ' >> $FILE1
echo   $ATT_FILES >> $FILE1 
echo ') | mailx -m -s " automation test email" mxxxxx@abcdefg.com ' >> $FILE1

chmod 777 $FILE1
. $FILE1
  • 1
    would you please add some explanation into your answer which can help to better understand your script, thank you – αғsнιη Jul 19 '17 at 15:08

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.