sending multiple files as attachment in e-mail using mailx - bash

I have a requiremnet to send multiple files as e-mail attachmnet in shell script. I have used below command.
(printf "%s\n" "BODY"; uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt ) | mailx -m -s "TEST" emailid#domain.com
However the number of files i want to send as an attachmnet are dynamic. So I want to assign the uuencode ... comand to a variable and then use it. I have tried below way,
$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
$ (printf "%s\n" "BODY"; $ATTACH_CMD ) | mailx -m -s "TEST" emailid#domain.com
And i am getting below error.
sh: uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt: not found.
Can any one please help me with this? Thanks in advance.

Have you tried using the below code? Not sure why it works, but maybe the below code could be used as a workaround
(printf "%s\n" "BODY"; `echo $ATTACH_CMD` ) | mailx -m -s "TEST" emailid#domain.com`?
For $ATTACH_CMD I have used echo command.

I finally found the way. eval makes the trick
eval $STR
$ ATTACH_CMD=$(echo `cat $OUTPUT_FILE`)
$ echo $ATTACH_CMD
uuencode out.txt out.txt ; uuencode asgda.txt asgda.txt
$ (printf "%s\n" "BODY"; eval $ATTACH_CMD ) | mailx -m -s "TEST" emailid#domain.com

Related

How to insert name field in mailx

My sample data File is
$ cat /fullpath/myfile.csv
a#gmail.com, A Singh
k#gmail.com, K Singh
I am using script.sh
#!/bin/bash
while IFS= read -r line
do
email=$(echo $line | awk -F, '{print $1 }')
name=$(echo $line | awk -F, '{print $2 }')
echo | mailx -v -s "Helo $name" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.gmail.com:587 -S from="xxxx#gmail.com(John Smith)" -S smtp-auth-user=xxxx#gmail.com -S smtp-auth-password=xxxxpassword -S ssl-verify=ignore -S nss-config-dir=~/.certs "$name<$email>"
done < /fullpath/myfile.csv
what is the correct syntax of adding receiver name
I am looking for syntax which I am not able to find
I tried below
"$name<$email>"
$name<$email>
-S to:"$name<$email>"
-S To:"$name<$email>"
-S To: "$name <$email>"
-S To: $name <$email>
its picking names (A Singh) as email and say invalid email. if i use To, it pick TO as email. i.e. whatever come 1st after certs code pic that as email.
According to the standard documentation, mailx does not seem to support the -S option, but some systems may add this option.
I recommend you use GNU Mailutils.
To specify a "FROM" name and address, you can use the "-a" option.
-a header:value
--append=header:value
Append the given header to the composed message.
To specify the receiver name and address, just like you did, add name and email to the end of the command will do the work.
#!/bin/bash
while IFS= read -r line
do
email=$(echo $line | awk -F, '{print $1 }')
name=$(echo $line | awk -F, '{print $2 }')
mail -s "Hello $name" -a "From: John Smith<xxxx#gmail.com>" "$name<$email>"
#echo | mailx -v -s "Helo $name" -S smtp-use-starttls -S ssl-verify=ignore -S smtp-auth=login -S smtp=smtp://smtp.gmail.com:587 -S from="xxxx#gmail.com(John Smith)" -S smtp-auth-user=xxxx#gmail.com -S smtp-auth-password=xxxxpassword## -S ssl-verify=ignore -S nss-config-dir=~/.certs "$name<$email>"
done < ./myfile.csv

echo to stdout and append to file

I have this:
echo "all done creating tables" >> ${SUMAN_DEBUG_LOG_PATH}
but that should only append to the file, not write to stdout.
How can I write to stdout and append to a file in the same bash line?
Something like this?
echo "all done creating tables" | tee -a "${SUMAN_DEBUG_LOG_PATH}"
Use the tee command
$ echo hi | tee -a foo.txt
hi
$ cat foo.txt
hi
Normally tee is used, however a version using just bash:
#!/bin/bash
function mytee (){
fn=$1
shift
IFS= read -r LINE
printf '%s\n' "$LINE"
printf '%s\n' "$LINE" >> "$fn"
}
SUMAN_DEBUG_LOG_PATH=/tmp/abc
echo "all done creating tables" | mytee "${SUMAN_DEBUG_LOG_PATH}"

Send multiple files by email and also add a body message to the email (Unix Korn Shell)

I'm trying to send multiple files by email but also include a body message in the email, I've tried couple of ways with no luck, the following code is for send multiple files:
(uuencode file1.txt file1.txt ; uuencode file2.txt file2.txt) | mailx -s "test" email#test.com
I've tried this option with no luck:
echo "This is the body message" | (uuencode file1.txt file1.txt ; uuencode file2.txt file2.txt) | mailx -s "test" email#test.com
any idea how could be the code?
Try this:
(echo "This is the body message"; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt) | mailx -s "test" email#test.com
The issue with your command is that you are piping the output of echo into the subshell and it is getting ignored as uuencode isn't reading from stdin.
You can use { ... } to avoid the subshell:
{ echo "This is the body message"; uuencode file1.txt file1.txt; uuencode file2.txt file2.txt; } | mailx -s "test" email#test.com
If you are doing this in a script and you want it to look more readable, then:
{
echo "This is the body message"
uuencode file1.txt file1.txt
uuencode file2.txt file2.txt
} | mailx -s "test" email#test.com

wget bash function without messy output

I am learning to customize wget in a bash function and having trouble. I would like to display Downloading (file):% instead of the messy output of wget. The function below seems close I am having trouble calling it for my specific needs.
For example, my standard wget is:
cd 'C:\Users\cmccabe\Desktop\wget'
wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
and that downloads the .csv as a .txt in the directory specified with all the messy wget output.
This function seems like it will do more-or-less what I need, but I can not seem to get it to function correctly using my data. Below is what I have tried. Thank you :).
#!/bin/bash
download() {
local url=$1 wget -O getCSV.txt http://xxx.xx.xxx.xxx/data/getCSV.csv
local destin=$2 'C:\Users\cmccabe\Desktop\wget'
echo -n " "
if [ "$destin" ]; then
wget --progress=dot "$url" -O "$destin" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
else
wget --progress=dot "$url" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
fi
echo -ne "\b\b\b\b"
echo " DONE"
}
EDITED CODE
#!/bin/bash
download () {
url=http://xxx.xx.xxx.xxx/data/getCSV.csv
destin='C:\Users\cmccabe\Desktop\wget'
echo -n " "
if [ "$destin" ]; then
wget -O getCSV.txt --progress=dot "$url" -O "$destin" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
else
wget -O getCSV.txt --progress=dot $url 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
fi
echo -ne "\b\b\b\b"
echo " DONE"
menu
}
menu() {
while true
do
printf "\n Welcome to NGS menu (v1), please make a selection from the MENU \n
==================================\n\n
\t 1 Patient QC\n
==================================\n\n"
printf "\t Your choice: "; read menu_choice
case "$menu_choice" in
1) patient ;;
*) printf "\n Invalid choice."; sleep 2 ;;
esac
done
}

Can't figure out how to send ^D (EOT) signal to mailx in bash script

I'm writing a bash script to send me an email automatically.
Mailx requires an EOT or ^D signal to know the message body is over and it can send.
I don't want to hit ^D on the keyboard when I run script which is what it does now.
Here is my code:
#! /bin/bash
SUBJ="Testing"
TO="test#test.com"
MSG="message.txt"
echo "I am emailing you" >> $MSG
echo "Time: `date` " >> $MSG
mail -s "$SUBJ" -q "$MSG" "$TO"
rm -f message.txt
If you do not need to add more text and just need to send the content of $MSG, you can replace
mail -s "$SUBJ" -q "$MSG" "$TO"
with
mail -s "$SUBJ" "$TO" < "$MSG"
The EOT will be implicit in the < construct. -q is indeed only used to start a message. The rest is supposed to come through stdin.
Pipe the output of a command group to mail.
#! /bin/bash
SUBJ="Testing"
TO="test#test.com"
MSG="message.txt"
{
echo "I am emailing you"
echo "Time: `date` "
} | mail -s "$SUBJ" -q "$MGS" "$TO"
rm -f message.txt

Resources