Sending attachments using email through shell script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sending attachments using email through shell script
# 1  
Old 01-25-2006
Sending attachments using email through shell script

Hi all,

I have written a shell script which sends emails with attachments to our clients. All our attachments are simple flat files (.txt format).

The script is working fine and sending the attachments to the mail-ids except that, when i am sending the attachments to non-outlook users (Like yahoo, gmail etc), i am seeing a junk character at the end of each line and the next line of file is appended to the previous line. In short all the data in the file is coming in one long line.

Here also there is a small twist. In gmail, there will be 2 options 2 download the file, View and Download. I am getting this problem only when i am downloading to the file to my local folder and viewing it.

Also when i am opening the same attachments in outlook, i can see that the data is appearing line by line as we entered it.

Kindly advise me, why this problem is occuring only when i am sending the mail to yahoo and gmail accounts. I am pasting the relavent code from my script.
cat <<!
From: $FROM
Subject: $SUBJ
To: $TO

[ "$CC" ] && echo "Cc: $CC"

## Generate the MIME header #####

cat <<!
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"

This is a Mime message, which your mail reader may not understand. Parts
of the message will appear as text. If the remainder appears as random
characters in the message body, instead of as attachments, then you'll
have to extract these parts and decode them manually.

Content-Type: text/plain; name="message.txt"; charset=US-ASCII
Content-Disposition: inline; filename="message.txt"
Content-Transfer-Encoding: 7bit


# Read the standard input as the main text of the message ...
# -------------------------------------------------------

cat -

# Now process the File attachments ...
# -----------------------------------------

if [ "$FILES" ]
for fl in $FILES

BASE=`basename $fl`
echo --DMW.Boundary.605592468
echo Content-Type: application/octet-stream\; name=\"$BASE\"
echo Content-Disposition: attachment\; filename=\"$BASE\"
echo Content-Transfer-Encoding: 7bit

cat $fl


# append the final boundary line ...

echo --DMW.Boundary.605592468--

) | /usr/lib/sendmail -t

Thanks & Regards
# 2  
Old 01-25-2006
Hi Vamsi,

run the command unix2dos for the attachment file and attach the new file of dos format...

Unix new line character is different from windows notepad's new line character and everything appears in the same line when we open it in notepad or similar like other web emails... but the same opens correctly in wordpad or ms word...

Please try the above stuff.. not sure whether it is because of something else as well.
# 3  
Old 01-27-2006
Sending attachments using email through shell script

Hi Mahendra,

thanks for the information. You almost resolved my problem. I installed UNIX2DOS on my PC and converted the attachments which i sent through my shell script to DOS mode and they are fine now.

But we are sending these attachments to our clients. So we cannot ask them to install UNIX2DOS, copy the attachments on their PC and run the command to correct the format.

Is there any way in which i run the UNIX2DOS (or similar) command within the script and then send the attachment to users?? I am working on HP-UX and there is no UNIX2DOS inbuilt function on my OS. You know any similar function in HP-UX which does the work of UNIX2DOS?

Thanks & Regards
Vamsi Sudhakar.
# 4  
Old 01-27-2006
HP has ux2dos and dos2ux instead of unix2dos and dos2unix.
# 5  
Old 01-27-2006
Sending attachments using email through shell script


I used the ux2dos command to convert the file to dos mode. But strangely still the problem persists. I mean when I saved the attachment from my GMAIL and open it, still the whole data is coming in a single line.

Where as I if i run UNIX2DOS (on my windows as mentioned in my previous thread) after extracting the attachment from my GMAIL, then i am getting the data in correct format.

I am really confused why it is not working when i do the same from my UNIX. Following is the way i am running my commands:
$ /usr/bin/ux2dos TEST_Alert_File_01232006.txt > xyz.txt
$ sh send_mail -t -s "ALRT FILES" -a xyz.txt
Enter body of main message ( end with Ctrl-D ) ...
Kindly note that the problem is coming only if i am copying the attachment to my local folder from the mail and open it. If i am directly opening the file or use the 'VIEW' option of the mail, then i am not getting this problem.

Any thoughts on why this is happening like this?

Thanks & Regards
Vamsi Sudhakar.
# 6  
Old 01-27-2006
It looks strange...

I have another solution for this, if you are facing the problem only when you save and open from you disk, then I would advice you to name the attachment file as test.rtf instead of test.txt, so that it would open in wordpad or msword directly, and they both support unix new line character...
# 7  
Old 01-27-2006
Sending attachments using email through shell script

Thanks Mahindra and RTM.
The .rtf has resolved the problem.

I just have to get back to the client and see whether he is OK with the rtf format rather than txt format.

Thanks & Regards
Vamsi Sudhakar.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending email in shell script

Can anyone help me out how to send the email without the word success while sending to the recipient. echo "success" | mailx -s "webshell instance has been started noiw" (8 Replies)
Discussion started by: ramkumar15
8 Replies

2. Shell Programming and Scripting

Mailx sending attachments to personal email, but not SharePoint?

Hello! I am writing a KornShell script that (in addition to other things) sends a file from a Unix directory to a SharePoint directory. However when executing the script there is no sign of the file on the SharePoint. I have tested the script using my personal email as the destination and the... (3 Replies)
Discussion started by: mattkoz
3 Replies

3. Shell Programming and Scripting

Sending an email with a body and attachments using uuencode

Hi, Im having a bit of an issue with using the uuencode command and sending out an email. My aim is to send an email out which has a body and also have attachments. Currently I can either get one or the other and not both on the same email. uuencode... (4 Replies)
Discussion started by: 02JayJay02
4 Replies

4. Shell Programming and Scripting

Email Attachments in shell script

Hi Fellas, I have a script that queries a sybase DB through isql and appends to a file, say file.csv I want to use the mail command in the shell script to email the file to me. i tried the following command but it doesn't work. can any one suggest whats wrong here. Note that i need the file... (2 Replies)
Discussion started by: Irishboy24
2 Replies

5. Shell Programming and Scripting

Sending email from shell script

Hi, I need to send email from a shell script. i echoed some information to a file and i am doing cat command in the email syntax thanks (2 Replies)
Discussion started by: rocky1954
2 Replies

6. HP-UX

Sending Unix files as attachments in an email

Hi, I am executing the following command in order to send a file as an attachment: mailx -s "Subject" < Testfile.txt Instead of attaching the file Testfile.txt, it is writing the contents of the file in the email message body. Please advise on how I can send the file as an... (7 Replies)
Discussion started by: sangharsh
7 Replies

7. Shell Programming and Scripting

Shell script for sending automatic email to personal mail id

hi guys, I need a shell script to send mail automatically to my personal mail id like but while experimenting with "mail" command I faced following problems. cat text1.txt | mail -s 'test mail' command successfully executed but while checking for... (4 Replies)
Discussion started by: rrd1986
4 Replies

8. Shell Programming and Scripting

Running a shell script in not sending - help??

I am pretty new to Unix shell scripting, but wondered if anyone could help (in layman's terms if possible!!) :) I have a shell script which ultimately sends an alert to an email address if part of a batch of programs fails. Here's the script that sends the email: Script: 6check.csh... (8 Replies)
Discussion started by: tjhorwood
8 Replies

9. UNIX for Dummies Questions & Answers

Sending email attachments

Hello, I've search the forum, but I cannot find an answer to my specific question. I'm trying to send some files to my professor. Upon his request, I used the following: tar -cvf vh.tar vh_part1.c vh_part2.c vh_part3.c vh_part4.c vh_sample_run15.txt uuencode vh.tar vh.tar > proj1 mail... (2 Replies)
Discussion started by: venush
2 Replies

10. UNIX for Dummies Questions & Answers

Email sending through a shell script

Hi everyone, I have a shell script that I use to send some emails with attachments. Based on the attachment file that is being sent, I choose the recipient. My file_list.txt looks like this CAJ.txt; KXLD.txt; I do a grep on this file to look for the file name and... (3 Replies)
Discussion started by: memonks
3 Replies
Login or Register to Ask a Question

Featured Tech Videos