How to send email with multiple attachments ?

Hello ,

I am trying to send an email with two attachments . I have tried all previous suggestion in this forum but none worked. I could send one attachment in an email by

uuencode $file "$file" | mailx -m -s "File"  xxx@xx.com 

but unable to send multiple attachments .
I have tried

1 .  `(uuencode $file1 $file1 ; uuencode $file2 $file2)` | mailx -m -s "All Files"  xxx@xxx.com 

2.

files= ( "$file1" "$file2" )  
for fileName in ${files[@]} 
	do
	uuencode $fileName $fileName 
	done)` | mailx -m -s "files in attachment" xxx@xx.com

By using 1 , I am getting message that begin : not found .

By 2 , I am getting message that the file could not be found , but I see that the file exists in the path.
I am very confused after trying a lot. Please help

You are putting backticks where they have no purpose. You don't need backticks if you're putting the output through a pipe.

Try (uuencode $file1 $file1 ; uuencode $file2 $file2) | mailx -m -s "File" xxx@xx.com

2 Likes

Thanks a lot it worked : )
Actually I put the backticks here because previously I got messages that '(' was not expected which was fixed if I put a backTick. So i developed habit of backtick before bracket thinking that my version of linux does not recognize brackets.

It probably meant something else by that error. Perhaps you were trying to use arrays in a shell that didn't support them. It depends more on the shell than the OS.

Please post:

uname -a    # Blotting anything confidential with Xs
echo $SHELL

HP-UX ,
/usr/bin/sh

1 Like

It is painful when someone cannot post the version of HP-UX (which appears clearly in uname -a ).

Anyway, assuming that this is a modern version, /sbin/sh will be the Posix Shell.

Corona688 has hit the nail on the head. The solution posted largely fits the bill.
There are differences between unix and MSDOS text file formats. I would convert the files if the mail reader is likely to be a Microsoft mail client.

(cat "${file1}"|ux2dos|uuencode "${file1}"; cat "${file2}"|uuencode "${file2}") | mailx -m -s "File" xxx@xx.com 

The only change I might make is to ensure the the names of the files in $file1 and $file2 are formatted to MSDOS standards. e.g. If the file is a Text file it should have the extension ".txt" such that the recipient can open the file with Windows Notepad.

Normally MSDOS File Extensions are totally irrelevant in unix, but in email attachments they are important.

Ps. Your Option 2 is a non-starter. It contains too many syntax errors.

1 Like

this is what i got with uname -a ....

HP-UX serverName B.11.31 U ia64 3073994272 unlimited-user license

.. Can you please tell how I can get text body in same email along with attachments ?

I have tried

(uuencode $HOME_DIR/first.sh first.sh ; uuencode $HOME_DIR/cd_test.sh cd_test.sh ; echo  $HOME_DIR) | mailx -m -s "testing email" ravi@xx.com

... it is not working ..

If i had the text body in a file , cat command might have worked , but i do not have as such . also tried

(uuencode $HOME_DIR/first.sh first.sh ; uuencode $HOME_DIR/cd_test.sh cd_test.sh) | mailx -m -s "testing email" ravi@xx.com <<EOF
$HOME_DIR
EOF

Have you tried this variation (some licence taken on the output format):
The only real difference is that the email body should appear before the attachments.

(echo "Testing email body content ${HOME_DIR}" ; uuencode ${HOME_DIR}/first.sh first.sh ; uuencode ${HOME_DIR}/cd_test.sh cd_test.sh) | mailx -m s "testing email" ravi@xx.com

Ps. It always helps to know what the final output should look like.