How can I send email with attachment xls file using shell script as application/xls mimetype without making the received file becoming unreadable

0

#!/bin/bash

ATTACH1=file.xls<br>
SUBJECT="subj"<br>
FROM=me@domain.com<br>
TO=you@domain.com<br>
CC=them@domain.com<br>
MIME="Application/vnd.ms-excel" <br>
FILE=$ATTACH1<br>
boundary="---my-unlikely-text-for-mime-boundary---$$--" <br>
(cat <<!<br>
From: $FROM<br>
To: $TO<br>
Subject: $SUBJECT<br>
Date: $(date +"%a, %b %e %Y %T %z")<br>
Mime-Version: 1.0<br>
Content-Type: multipart/mixed; boundary="$boundary"<br>


This email has attached the file<br>
--$boundary<br>
Content-Type: $MIME;name="$FILE"<br>
Content-Disposition: attachment;filename="$FILE"<br>

!<br>
uuencode ${ATTACH1} ${ATTACH1}<br>
) | sendmail -v ${TO}<br>

Hi,

The above code can send xls file using shell script. But the problem is that, the received file cant be open. Also, the file size of the xls becomes smaller. Example: original xls attachment size is 17kb before sending, but the received file becomes 378b in size after it was sent..

What can I do to make the xls that was received becomes readable when open by the receiver? What is wrong or missing in the above script? Please help!!!!

And by the way.. I CANNOT USE MUTT

RaymonN

Posted 2016-01-15T10:15:19.333

Reputation: 1

Where are you putting the file content? – a CVn – 2016-01-15T10:43:11.050

you mean the code above? I put it in as script1.sh file then execute it by ./script1.sh – RaymonN – 2016-01-15T10:45:19.683

No, I mean how does the data that gets piped into sendmail contain the contents of the file you want to attach to the email? – a CVn – 2016-01-15T10:49:34.343

Please see the updated script.. it now sends the same file size but now it contains begin 644 PRB0045758_EDI_UPLOAD_14-01-2016.xls M/#]X;6P@=F5R<VEO;CTB,2XP(C^"CQS<SI7;W)K8F]O:R!X;6QN<SIS<STB M=7)N.G-C:&5M87,M;6EC<F]S;V9T+6-O;3IO9F9I8V4Z<W!R96%D<VAE970B M/@H<W,Z4W1Y;&5S/@H<W,Z4W1Y;&4@<W,Z240](D]R86-L941A=&4B/@H
M<W,Z3G5M8F5R1F]R;6%T('-S.D9O<FUA=#TB9&0O;6TO>7EY>5P@:&@Z;6TZ M<W,B+SX/"]S<SI3='EL93X/"]S<SI3='EL97,^"CQS<SI7;W)K<VAE970@
– RaymonN – 2016-01-15T10:58:31.900

duplicate of http://stackoverflow.com/q/17359/7552

– glenn jackman – 2016-01-15T11:35:44.230

no its not duplicate – RaymonN – 2016-01-15T11:40:02.410

Answers

0

You need to specify the Content-Transfer-Encoding for the MIME part with the attachment. I don't know if uuencode is a standard one. Base64 is though.

You also need to send the closing boundary marker.

Your redirection into cat is wrong.

(cat << !
From: $FROM
To: $TO
Subject: $SUBJECT
Date: $(date +"%a, %b %e %Y %T %z")
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"

This email has attached the file

--$boundary
Content-Type: text/plain; charset=ISO-8859-1

Please see the attachmed file.

--$boundary
Content-Transfer-Encoding: base64
Content-Type: $MIME;name="$ATTACH1"
Content-Disposition: attachment;filename="$ATTACH1"

$(base64 "$ATTACH1")

--$boundary--
!
) | sendmail -v "$TO"

glenn jackman

Posted 2016-01-15T10:15:19.333

Reputation: 18 546

base64: command not found

:( – RaymonN – 2016-01-15T13:31:11.857

Try Content-Transfer-Encoding: uuencode and use uuencode on the file like you were doing. – glenn jackman – 2016-01-15T14:32:14.097

still not working..the attached file when received cannot be open – RaymonN – 2016-01-15T15:06:46.650

0

Instead of building your own program to send mail, just use one of the many that already exist. What you are looking for is a mail user agent (MUA). There are many to choose from to send mail on the commandline. For example, using Mutt:

mutt recipient@example.org -s "My example subject" -a attachment.xlsx < mailbody.txt

This will send a mail to recipient@example.org, reading the mail body text from the file "mailbody.txt", and with the attachment attachment.xlsx. Mutt will do all the nitty-gritty for ýou, such as encoding the mail body appropriately and choosing the right MIME type for the attachment.

Of course you can write all that yourself, but you may have to invest a few months...

sleske

Posted 2016-01-15T10:15:19.333

Reputation: 19 887