Solved

send mail with attachments from UNIX server to Windows

Posted on 2009-07-12
44
797 Views
Last Modified: 2013-12-26
can someone help with the script/command required to send a mail with attachment from a UNIX server to Windows mailbox.Is there any additional configuration that needs be done on the UNIX box for this ?
0
Comment
Question by:rvsBhanu
  • 21
  • 18
  • 5
44 Comments
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Hi,

If all you would like to send as an attachement is a file and you don't need MIME encoded attachement you can do it like that:

uuencode reports.tar.gz reports.tar.gz | mailx -s "My Report" user@yourcorp.com

Or

$ (cat mymessage.txt; uuencode file1.jpeg file2.jpeg) | mail -s "Subject" user@example.com

Bif you need MIME than you need to use some program like mutt.

Cheers,
K.
0
 

Author Comment

by:rvsBhanu
Comment Utility
thanks...

I would normally like to send an excel sheet as an attachment.
would this command work out for that....or do I need mutt..?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Yup. you can simply do this:


uuencode sheet1.xls | mailx -s "My report sheet" user@example.com

This will send your report. If you don't have uuencode installed on your system it comes with sharutils package so try to install it.

Cheers,
K.
0
 

Author Comment

by:rvsBhanu
Comment Utility
can u also tell me from where I can install the uuencode or where I can get the sharutils package ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
ooops sorry you need to rpeat the name twice:

uuencode sheet1.xls sheet1.xls | mailx -s "My report sheet" user@example.com
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
What distro are you using ??
0
 

Author Comment

by:rvsBhanu
Comment Utility
sorry...distro...could not get that..?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
What Brand UNIX are you using ? If it is LINUX what distirbution of LINUX are you using?

Different unicies have different installation instructions or resources to install
0
 

Author Comment

by:rvsBhanu
Comment Utility
not really sure of that...where can I check that ?
0
 

Author Comment

by:rvsBhanu
Comment Utility
I mean where can I check the type of distribution ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
just issue

uname -a

and post the result here.
0
 

Author Comment

by:rvsBhanu
Comment Utility
I need to go to my workplace and check for it.
Will post the results for it by tomorrow same time...

thanks a lot for ur time on this..
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Yr welcome take your time and let me know.
0
 

Author Comment

by:rvsBhanu
Comment Utility
% uname -a
SunOS dvc7600 5.9 Generic_118558-38 sun4u sparc SUNW,Netra-T12
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
uuencode is available on solaris, did you try to run it?

you could try to find it with

which uuencode

or

find / -name uuencode

it should be under /usr/bin dir
0
 

Author Comment

by:rvsBhanu
Comment Utility
I am able to send/receive the mail,but without any attachments...

Can you let me know wat needs to be done for getting the attachments as well..??
0
 

Author Comment

by:rvsBhanu
Comment Utility
Thanks all...I am able to send mail with attachement.

Is there any way that it can be automated ?as in....at a particular time of the day a mail is fired to a mail address with the daily excel as an attachement ?This daily excel has a particular format ie.rptjul1309...a simialar file is created for each day with the suitable date...
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
- you can create a shell script e.g. myscript that contains

/usr/bin/uuencode /path/to/sheet.xls sheet.xls | /usr/bin/mailx -s "put subject here" username@domain.com

- make the script executable

chmod +x /path/to/myscript

- create a crontab job

crontab -l > mycrom
echo '0 21 * * * /path/to/myscript' > mycron
crontab mycron

here the script will run daily at 9:00 PM

for more help run

man crontab
0
 

Author Comment

by:rvsBhanu
Comment Utility
the excel is not static data here. everyday a new excel file with a different name(as in the name would have a different date appended for each day)would be uploaded at the particular location and I need that to be sent by this job.

Would the above solution work for that ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Yeah you can just create the script I'll gve an example below. Then put it in cron as OmerFarid indicated and you're done.



#!/bin/sh
 

export RPTNAME="rpt$(date +%b%d%g).xls"
 

uuencode $RPTNAME $RPTNAME | mailx -s "Contains report file $RPTNAME" user@example.com

Open in new window

0
 

Author Comment

by:rvsBhanu
Comment Utility
here,where do I specify the location of the excel ,from where it is to be pulled ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
export RPTNAME="rpt$(date +%b%d%g).xls"

This line is creating the name of the file form the current date. For today the name will be:

rptJul1309.xls

%b means short month name such as Jan, Jul, Oct etc.
%d means the day part of the date in the format 00..31
%g means two digit year 00.99

So the name is created. then it will uuencodeand send the named file with the next line:
uuencode $RPTNAME $RPTNAME | mailx -s "Contains report file $RPTNAME" user@example.com
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:rvsBhanu
Comment Utility
thx...understood this...
wat I wanted to ask was where do I specify the path of the excel file as in ,can it be :

uuencode /path/to/$RPTNAME $RPTNAME | mailx -s "Contains report file $RPTNAME" user@example.com

Is this correct ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Yeah this is correct you can specifty the path like that

uuencode /path/to/$RPTNAME $RPTNAME | mailx -s "Contains report file $RPTNAME" user@example.com

you don't need to modify the second one since it is to put in the encoded text as filename.
0
 

Author Comment

by:rvsBhanu
Comment Utility
is there any way where I can send two files from two different locations as attachments in the same mail ?
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
tar the two files into one tar file, then compress the tar file then send it:

- tar cf myfiles.tar /path/to/file1 /path/to/file2
- gzip myfiles.tar
- cat myfiles.tar.gz | uuencode myfiles.tar.gz | mailx -s "two files attachment" username@domain.com
0
 

Author Comment

by:rvsBhanu
Comment Utility
Thanks..
Now the thing is I need this file : myfiles.tar to be variable as in file1 and file2 are daily generated .

So is there some way by which it creates the tar file as variable or deletes the file everyday once the mail is sent ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Hi,

This will do.
#!/bin/sh

 

export RPTNAME="rpt$(date +%b%d%g).tar"
 

tar cf $RPTNAME.tar /path/to/files

 

uuencode $RPTNAME $RPTNAME | mailx -s "Contains report file $RPTNAME" user@example.com

Open in new window

0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Sorry my bad try this instead:
#!/bin/sh

 

export RPTNAME="rpt$(date +%b%d%g).tar"

 

tar cf $RPTNAME /path/to/files

 

uuencode $RPTNAME $RPTNAME | mailx -s "Contains report file $RPTNAME" user@example.com

Open in new window

0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
You might also like to use the code below. Will you tell what will be the namses for the files? Are there rules to know what will be the file names to go in the attachment ?


#!/bin/sh
 

export RPTNAME="rpt$(date +%b%d%g)"
 
 

mailx -s "Contains report file $RPTNAME" kerem@sibernet.com.tr << END

$(uuencode file1.xls  file1.xls)

$(uuencode file2.xls file2.xls)

END

Open in new window

0
 

Author Comment

by:rvsBhanu
Comment Utility
yes,the two excel files that I need to be attached will have a particular format ie. devidrpt<mmmddyy> and nondevidrpt<mmmddyy>.

Also let me know where do I specify the path to these files as both these files would be at different location ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Here is the info.

You need to add the path before the first occurrence of file name after the uuencode command:
#!/bin/sh

 

export RPTNAME="$(date +%b%d%g)"

 

 

mailx -s "Contains report file $RPTNAME" user@example.com << END

$(uuencode /path/to/devidrpt${RPTNAME}.xls  devidrpt${RPTNAME}.xls)

$(uuencode /ot/path/to/nondevidrpt${RPTNAME}.xls  nondevidrpt${RPTNAME}.xls)

END

Open in new window

0
 

Author Comment

by:rvsBhanu
Comment Utility
the above code gives the below error:

RPTNAME=$(date +%b%d%g): is not an identifier

any idea wat is causing the issue ?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Hi,

I guess your shell is different then mine try to modify this line:

export RPTNAME="$(date +%b%d%g)"

as

RPTNAME="$(date +%b%d%g)"
export RPTNAME

Cheers,
K.
0
 

Author Comment

by:rvsBhanu
Comment Utility
thanks...
I already have a script for modifying the attachement name as per the date.

I would like to know how can I send the two attachments(excel) from two different locations in one mail.And since these are excel files in binary format, any mechanism to convert the binary to excel format for easy viewing.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
@rvsBhanu
> I would like to know how can I send the two attachments(excel) from two different locations in one
> mail

Did you notice the encoding part in my script:

the line 2 sends the file in /path/to/file/devidrpt${rptname}.xls
while the line 3 sends /ot/path/to/nondevidrpt${RPTNAME]}.xls

As you see you can modify the paths as you wish.

> And since these are excel files in binary format, any mechanism to convert the binary to excel format for easy viewing.

You've lost me completely. Yes excel files are in binary format. There is no such thing as non-binary excel format. Do you mean CSV (Acronym for Comma Separated Values)  when you say binary to excel format ? Or do you mean you need some your binary to .xls format? Then what is the format that your binaries are kept? What application generates it? May be this application has a facility to export in CSV instead of .xls so that you can view the daa in nan ordinary text editor. Furthermore excel can import CSV files into spreadsheets.
mailx -s "Contains report file $RPTNAME" user@example.com << END

$(uuencode /path/to/devidrpt${RPTNAME}.xls  devidrpt${RPTNAME}.xls)

$(uuencode /ot/path/to/nondevidrpt${RPTNAME}.xls  nondevidrpt${RPTNAME}.xls)

END

Open in new window

0
 

Author Comment

by:rvsBhanu
Comment Utility
SCRIPT:

#!/bin/csh
RPTNAME="$(date +%b%d%g)"
export RPTNAME
mailx -s "Contains report file $RPTNAME" me@mymail.com << END
$(uuencode /path/to/file/devidrptJul1509${RPTNAME}.xls devidrptJul1509${RPTNAME}.xls)
$(uuencode /path/to /file/nondevidrptJul1509${RPTNAME}.xls nondevidrptJul1509${RPTNAME}.xls)
#
-----------------------------------------------------------------------------------------------------------------------------
Error:
Variable syntax
--------------------------------------------------------------------
Can you pls check this ?Also I would like to know that if the filenames remains fixed,would this work in that scenario also..?If not,can you pls tell me what needs to be done for that ?
0
 

Author Comment

by:rvsBhanu
Comment Utility
thx...my script runs fine now after a few modifications.

Though I am still able to open my file, if you could help me with getting the file in excel format with delimiters like space on in place of tab ?Is there any way of acheiving this ?
0
 

Author Comment

by:rvsBhanu
Comment Utility
mailx -s "Subject" a@mailaddress.com
******************************************************************************************
Can someone tell me how can I get the subject of the email to be "Report - <todays date>" format ?
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
try

mailx -s "Subject: `date`" a@mailaddress.com
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
please note the ` before and after date command
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
Hi

Try this instead. Sorry I've forgot it was CSH for a moment aand used $(..) construct this belongs to bash.

Your Subject has already date in it. Please don't omit the final "END" it is critical.

Cheers,
K.

#!/bin/csh

set RPTNAME="`date +%b%d%g`"

mailx -s "Contains report file $RPTNAME" me@mymail.com << END

`uuencode /path/to/file/devidrptJul1509${RPTNAME}.xls devidrptJul1509${RPTNAME}.xls`

`uuencode /path/to /file/nondevidrptJul1509${RPTNAME}.xls nondevidrptJul1509${RPTNAME}.xls`

END

Open in new window

0
 
LVL 30

Accepted Solution

by:
Kerem ERSOY earned 125 total points
Comment Utility
RPTMAIL contains Aug1909 as you have requested earlier in the tread...
0
 

Author Closing Comment

by:rvsBhanu
Comment Utility
Thank you :)
0

Featured Post

Why spend so long doing email signature updates?

Do you spend loads of your time carrying out email signature updates? Not very interesting are they? Don’t let signature updates get you down. Let Exclaimer Cloud - Signatures for Office 365 make managing email signatures a breeze.

Join & Write a Comment

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now