I have the following script that i put togheter from the web, to send emails from terminal trough gmail.
#!/bin/bash
#sendGmail "FROM" "TO" "SUBJECT" "BODY" "ATTACHMENTS (optional)"
FROM=$1
TO=$2
SUBJECT=$3
BODY=$4
# Function to check if entered file names are really files
function check_files()
{
output_files=""
for file in $1; do
if [ -s $file ]; then
output_files="${output_files}${file} "
fi
done
echo $output_files
}
if [ "$FROM" == "" ]; then
FROM="default@gmail.com"
else
if [[ "$FROM" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]]; then
echo error in FROM
exit
fi
fi
if [[ "$TO" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]]; then
echo error in TO
exit
fi
if [ -z "$5" ]; then
echo $BODY | mail -r $FROM -s $SUBJECT $TO
else
ATT=$5
ATTACHMENTS=""
attachments=$(check_files "$ATT")
for attachment in $attachments; do
ATTACHMENTS="$ATTACHMENTS $attachment"
done
echo $ATTACHMENTS
echo $BODY | mail -r $FROM -s $SUBJECT -A $ATTACHMENTS $TO
fi
echo email sent!
But when i send emails, i have the following behaviours:
- with/without attachments: If
$subject
is "some random theme", then the email is sent to$TO
andsome@mipc.localdomain
,random@mipc.localdomain
andtheme@ mipc.localdomain
. - without attachments:
$BODY
is in the email body - with attachments: email body is empty
EDIT: thanks to @ilkkachu, first issue was fixed, new code:
#!/bin/bash
#sendGmail "FROM" "TO" "SUBJECT" "BODY" "ATTACHMENTS (optional)"
FROM=$1
TO=$2
SUBJECT=$3
BODY=$4
if [ "$FROM" == "" ]; then
FROM="default@gmail.com"
else
if [[ "$FROM" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]]; then
echo error in FROM
exit
fi
fi
if [[ "$TO" =~ "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$" ]]; then
echo error in TO
exit
fi
if [ -z "$5" ]; then
echo "$BODY" | mail -r "$FROM" -s "$SUBJECT" "$TO"
else
ATT=$5
ATTACHMENTS=""
for attachment in $ATT; do
if [ -f $attachment ]; then
ATTACHMENTS="$ATTACHMENTS-A $attachment "
else
echo something wrong with $attachment, therefore not attached
fi
done
echo "$BODY" | mail -r "$FROM" -s "$SUBJECT" $ATTACHMENTS "$TO"
fi
echo email sent!
-A
– kurokirasama Jul 03 '18 at 20:16$SUBJECT
or$ATTACHMENTS
should just appear as new recipients for message, just as you saw. You may need to check that theecho $BODY
in the attachment branch doesn't have any weird typos. The shell won't warn if you've writtenecho $BOD
, or some other less visible mistake. – ilkkachu Jul 03 '18 at 20:38-A
to the for loop, i still get problems with sending attachments. However, i realice that it was the functioncheck_files
(i don't know why). I added its functionality to the for loop and now it works great, – kurokirasama Jul 03 '18 at 23:41[ -f "$file" ]
instead of[ -s "$file" ]
,-f
tests for regular files,-s
just for non-zero size, which directories usually also have... Or maybe even[ -f "$file" ] && [ -s "$file" ]
to test for both. – ilkkachu Jul 04 '18 at 09:04for attachment in $ATT; do
. See: Why does my shell script choke on whitespace or other special characters? and When is double-quoting necessary? – ilkkachu Jul 04 '18 at 09:05echo "hello there" | mail.mailutils -A foo.txt -s 'test test' $emailaddress
seems to work and I can't see anything obviously wrong in your script. Try if it works when running manually, and double-check the script for typos or any weird/invisible characters – ilkkachu Jul 07 '18 at 10:37