MIME="Application/vnd.ms-excel" <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>
Content-Type: $MIME;name="$FILE"<br>
Content-Disposition: attachment;filename="$FILE"<br>

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


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

  • Where are you putting the file content?
    – user
    Jan 15 '16 at 10:43
  • you mean the code above? I put it in as script1.sh file then execute it by ./script1.sh
    – RaymonN
    Jan 15 '16 at 10:45
  • 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?
    – user
    Jan 15 '16 at 10:49
  • 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
    Jan 15 '16 at 10:58
  • duplicate of stackoverflow.com/q/17359/7552 Jan 15 '16 at 11:35

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

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

Please see the attachmed file.

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

$(base64 "$ATTACH1")

) | sendmail -v "$TO"
  • base64: command not found :(
    – RaymonN
    Jan 15 '16 at 13:31
  • Try Content-Transfer-Encoding: uuencode and use uuencode on the file like you were doing. Jan 15 '16 at 14:32
  • still not working..the attached file when received cannot be open
    – RaymonN
    Jan 15 '16 at 15:06

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.