How to attach two files in unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to attach two files in unix script
# 22  
Old 12-08-2010
Quote:
#! /bin/ksh
export EMAILLIST="abc@xyz.com"
cd /home2/data
File1=`ls -lrt *_Accepted.* | tail -1 | awk '{print ($9)}'`
File2=`ls -lrt *_Rejected.* | tail -1 | awk '{print ($9)}'`
echo $File1
echo $File2
SUBJECT="Two Attachment Email"
BODY="Kindly refer the attachment" > /home2/data/email_body.txt
# send an email using /bin/mail
(cat /home2/data/email_body.txt; uuencode "$File1" "$File1"; uuencode "$File2" "$File2") | mail -s "$SUBJECT" $EMAILLIST

The problem is quite simple. File1 and File2 are not files - they are environment variables.
Try it with real files.
Also if the filename end in ".txt" and we convert the files from unix to M$ format we can read them with Internet Explorer and Notepad.
I'm assuming you have a unix with the "ux2dos" command and a mailx with the "-m" switch to correctly format an email with uuencode attachments.
So far none of your posts mention what Operating System and mail client you are running which is why you get irrelevant answers. It is actually easier to deal with this issue by using "sendmail" rather than "mailx". Depends what you have.

Code:
File1=/tmp/File1.txt
File2=/tmp/File2.txt
ls -lrt *_Accepted.* | tail -1 | awk '{print ($9)}' > ${File1}
ls -lrt *_Rejected.* | tail -1 | awk '{print ($9)}' > ${File2}

(cat /home2/data/email_body.txt; ux2dos "${File1}"|uuencode "$File1"; ux2dos "${File2}"|uuencode "$File2") | mail -m -s "$SUBJECT" $EMAILLIST[/QUOTE]

# 23  
Old 12-09-2010
Hi,

For my requirement i wrote a separate script for sending email
Code:
#! /usr/bin/ksh
function sendEmail
{
MAILTO=$1
SUBJECT=$2
BODY=$3
export ATTACH=$4
export ATTACH1=$5
echo $BODY
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/plain"
 echo "Content-Disposition: inline"
 echo $BODY 
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH "output.txt"
 echo '---q1w2e3r4t5--'
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH1)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH1)'"'
 uuencode -m $ATTACH1 "output.txt"
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO
}

and for sending email i called the below command
Code:
sendEmail $EMAILLIST "$SUBJECT" "Hello" "$File1" "$File2"

But am able to view only one attachment how can i modify the code to get 2 attachments attached?

Kindly help me with your thoughts.

Thanks In Advance.
Meva.

---------- Post updated at 06:32 AM ---------- Previous update was at 06:14 AM ----------

Hey..... My script is working now Smilie i made changes to it and my final script is given below
Code:
#! /usr/bin/ksh
function sendEmail
{
MAILTO=$1
SUBJECT=$2
BODY=$3
export ATTACH=$4
export ATTACH1=$5
echo $BODY
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/plain"
 echo "Content-Disposition: inline"
 echo $BODY 
 echo '---q1w2e3r4t5'
 echo 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH "output.txt"
 echo '---q1w2e3r4t5'
 echo 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name="'$(basename $ATTACH1)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH1)'"'
 uuencode -m $ATTACH1 "output1.txt"
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO
}

But the problem i was facing is that i was not able to view the body of the email and its blank in my mail; its getting echoed in the command prompt....how do i include the body content to my email to my mail.....

---------- Post updated at 06:42 AM ---------- Previous update was at 06:32 AM ----------

Hey my script is now working fine now Smilie Below code is used for attaching the .csv files to email

Code:
#! /usr/bin/ksh
function sendEmail
{
MAILTO=$1
SUBJECT=$2
BODY=$3
export ATTACH=$4
export ATTACH1=$5
echo $BODY
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/plain"
 echo "Content-Disposition: inline"
 echo 
 echo $BODY 
 echo
 echo '---q1w2e3r4t5'
 echo 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH "output.txt"
 echo '---q1w2e3r4t5'
 echo 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name="'$(basename $ATTACH1)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH1)'"'
 uuencode -m $ATTACH1 "output1.txt"
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO
}

Then i called the below command and it worked

Code:
sendEmail $EMAILLIST "$SUBJECT" "Hello" "$File1" "$File2"

Thanks All for your valuable inputs Smilie

Meva.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Attach filename to wc results on massive number of files

Hello, I have massive number of big files that needed to be counted for the total number of lines (> 100x millions) each. I want the file name attached to the count results so that they are aligned nicely matching name and counts. I could do each file at a time, which will take hours to finish,... (8 Replies)
Discussion started by: yifangt
8 Replies

2. Shell Programming and Scripting

Script to attach latest files of directories in a mail.

Hello Folks, I am looking for the script which will go to directory and check for the latest 5 files and send a mail to attaches these files to user. Kindly guide. Regards (7 Replies)
Discussion started by: sadique.manzar
7 Replies

3. Shell Programming and Scripting

UNIX - how to send attach excel in mail

Hi Experts, i need your help here :confused: Need to send a report thru mail using unix shell script(AIX). can you help me to do this? . i tried "uuencode" with CSV format, but while reading report all values are in single column. i need each column values in separate cell. Thanks in... (9 Replies)
Discussion started by: newbieabc
9 Replies

4. Shell Programming and Scripting

How to attach multiple .csv files using mutt command

I need to attach all files starting with 'BusinessReport' using mutt command. It could be any number of files in that directory, say BusinessReport_01, BusinessReport_03, BusinessReport_04 etc. Is there a way to attach all files where filename like BusinessReport_* and sent it using mutt... (2 Replies)
Discussion started by: Jassz
2 Replies

5. Shell Programming and Scripting

Script to attach file to mail

Hello, I have a .dat file containing one line. I need a script to read that line and make it part of the body and send a mail... Let's say the line is $line. I need the script to send a mail with the body "The last disposal feed is $line". Thanks (4 Replies)
Discussion started by: sfetea
4 Replies

6. Shell Programming and Scripting

Attach a binary file to email in a script

Hi, I am trying to get an email sent out by the unix ( aix ) system that has a .gz file attached to it. I can get the attachment, but it's not working when being looked at from outlook. I think there is a problem because of the way I am doing it, and the fact that it's binary. I am trying to... (15 Replies)
Discussion started by: fwellers
15 Replies

7. Shell Programming and Scripting

attach multiple files in email

I am trying to send multiple files as attachment in one email, I tried to search but couldn't find. Please let me know if any solutions. (2 Replies)
Discussion started by: mgirinath
2 Replies

8. Shell Programming and Scripting

attach 2 files using mailx

if test.dat is the file cat test.dat|uuencode test.dat|mailx -s "subject" mailid can be used for attaching test.dat how can i attach more than one file to a mail using mailx (2 Replies)
Discussion started by: anumkoshy
2 Replies

9. UNIX for Advanced & Expert Users

pine does'nt attach files

Hello All, I am maintaining a server and I use pine as MUA and sendmail as MTA. Suddenly many users in the network face the problem of not being able to attach files using pine. I checked the sendmail.cf file and found a variable "MaxMessageSize = 1000000". Eventhough the message size... (2 Replies)
Discussion started by: maybemedic
2 Replies
Login or Register to Ask a Question