2

I'm trying to use Mutt to send specific files in a folder. I have a folder titled "Image Cache" that, with a picture being taken every second, has 30 seconds of pictures in it. I would like to use Mutt to put 15 seconds worth of pictures into an email as attachments and send them to me. Each picture is around 30 KiB, putting 15 in an email shouldn't be an issue.

So far I have been using most of the code from this post here with a few changes to match my file system.

Here's the code I'm using:

#!/bin/bash
DATE=$(date +%F)
MESSAGE="IMAGES" 
FILES=$(find /path/to/directory -not -newermt '-15 seconds' | sed 's/ /\\  /g' | grep ".jpg")


if [ -z "$FILES" ]; then
MBODY="No images were found that match search criteria."
echo "$MBODY" | mutt -s "Data files for $TODAY" $EMAILS
fi

echo $MESSAGE | mutt -s "Images taken on $DATE" -a "$FILES" -- myemail@domain.com

Spits out this error:

Can't stat /My/Directory/Image1.jpg
/My/Directory/Image2.jpg: No such file or directory
/My/Directory/Image3.jpg: unable to attach file.

Any help is greatly appreciated :). Thanks in advance

0

1 Answer 1

3

Since you're quoting "$FILES" you are passing a single string to the -a option. You have to pass individual files there.

What you should do is to store the files in an array: it's the best way to handle multiple strings that contain whitespace. I assume your filenames don't contain newlines

# read the output of `find` into an array, one element per line
mapfile -t files < <(find /path/to/directory -name '*.jpg' -not -newermt '-15 seconds')
# ...
echo "$message" | mutt -s "Images taken on $date" -a "${files[@]}" -- myemail@domain.com
# ...................................................^^^^^^^^^^^^^

That exact form of parameter expansion will result in each array element as a separate word in the command.

Get out of the habit of using ALLCAPSVARNAMES: one day you'll use PATH=... and then wonder why your script is broken

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .