0

#!/bin/bash

ATTACH1=file.xls SUBJECT="subject"
FROM=me@domain.com
TO=you@domain.com
CC=them@domain.com
MIME="application/xls"
FILE=$ATTACH1
boundary="---my-unlikely-text-for-mime-boundary---$$--"
(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: $MIME;name="$FILE"
Content-Disposition: attachment;filename="$FILE"

!
) | sendmail -v ${TO}

RaymonN
  • 1
  • 4
  • Where did you find this script?? Why is there html mixed with bash, the two are usually exclusive. – asimovwasright Jan 14 '16 at 12:49
  • no please ignore the html tags.. anyway, is it possible to attach xls file and email it using shell script? – RaymonN Jan 14 '16 at 12:51
  • Yes, using a valid email sending command (on an equally setup server) you can attach anything you like to an email. But to troubleshoot **this** script, you will need to give some more info about the script itself. Where does it come from, how do you execute it? – asimovwasright Jan 14 '16 at 12:53
  • Possibly just change to var line to: `ATTACH1="file.xls"` – asimovwasright Jan 14 '16 at 12:55
  • But the quotes are equally important. – asimovwasright Jan 14 '16 at 12:58
  • @mindas, I cant use mutt because It is not installed in server – RaymonN Jan 14 '16 at 12:59
  • There are other answers apart from `mutt` - just plain `mail` would do, and I believe it comes as a default in most distros. – mindas Jan 14 '16 at 13:00
  • 1
    You need to be more explicit about what "doesn't work" means. Do you get an error? Does the mail never arrive? Does it arrive corrupted in some way? Does a voice come over the speaker saying "I'm afraid I can't let you do that, Dave."? – IMSoP Jan 14 '16 at 13:02
  • Are you able to install sendEmail - it is much simpler, but can be handy for scripting ease.. – asimovwasright Jan 14 '16 at 13:04
  • I think you need to extend your email body so it supports `mimetypes` and then you indicate `mimetype=` (something, maybe `xls`). Sorry, I worked this out 10 years ago, but don't have the code anymore. I've seen a few Qs here that mention this, so search here for `sendmail mimetype` (and plural). Hmm, I'll bet there is a `perl` module that will do this, but if you don't know perl, that will take time to get it working. In general, solving this by trial-and-error will take a while. Work with your boss about best tradeoffs on time. Good luck. – shellter Jan 14 '16 at 13:10
  • @ mindas I tried: sendemail -> but error is: sendemail: command not found.. mail -> but error is: mail: Not a recognized flag: a uuencode -> only .txt and other formats can be sent but not .xls all other solutions in the 'How do I send a file as an email attachment using Linux command line' needs to install something first to work... – RaymonN Jan 14 '16 at 13:16
  • @ IMSoP 'doesnt work' means no error when I have executed it but I never received the email :( – RaymonN Jan 14 '16 at 13:17
  • @ asimovwasright I cant install anything on the server.. is sending of email with xls file attachment impossible without installing something? – RaymonN Jan 14 '16 at 13:19
  • @ shelter can you atleast give me some link? I desperately needed it.. please... – RaymonN Jan 14 '16 at 13:22
  • I am still confused how the other file types **did** work. Could you try using the `-m` flag on uuencode, to force base64 encoding instead of default. And please quote the var `ATTACH1="file.xls"` – asimovwasright Jan 14 '16 at 14:31
  • Might also consider `ATTACH1="./file.xls"` And according to the uuencode man page, there is no need for output argument, so it should just be `uuencode ${ATTACH1}` e.g. `tar cf - src_tree | compress | uuencode src_tree.tar.Z | mail sys1!sys2!user` – asimovwasright Jan 14 '16 at 14:37
  • Also I just noticed that you have an extra delimiter for the here documents, I don't think there should be the last `!` after the uuencode line, since both here docs are already ended prior to that. – asimovwasright Jan 14 '16 at 14:53
  • @asimovwasright Thank you for your response.. I have searched other methods how to send xls file, and the resolution was to add mimetype on the script. but I still have a problem.. I cannot read the file that was sent.. also it's file size has been decreased.. How can I open a this file now? – RaymonN Jan 14 '16 at 14:59
  • mmmmm, decreased by how much? – asimovwasright Jan 14 '16 at 15:02
  • @asimovwasright The file was originally 17kb but when the file was received it becomes 358bytes only – RaymonN Jan 14 '16 at 15:04
  • open the file as ascii and tell me whats inside – asimovwasright Jan 14 '16 at 15:05
  • @asimovwasright it was just blank.. by the way.. I updated the script above – RaymonN Jan 14 '16 at 15:12
  • 358b can't be blank. Did you open it in nano? Also could you give me the output of `file ./new_file_received` where 'new_file_received' of course is the 358b file. – asimovwasright Jan 14 '16 at 15:26
  • In the new script, shouldn't the `!` at the end be replaced by `--$boundary` – asimovwasright Jan 14 '16 at 15:32
  • uhm..sorry im new to this.. how can I upload the file? – RaymonN Jan 14 '16 at 15:37
  • I don't think you can :-) Do you have a bash shell open on the computer where the file was received? – asimovwasright Jan 14 '16 at 15:42
  • yes..can you convert this to chat? it seems that we cant have a long thread of comments – RaymonN Jan 14 '16 at 15:50
  • I can't move to chat, I don't know why there is no option in this case. – asimovwasright Jan 14 '16 at 15:57
  • On the other hand, since you placed your email address in the quesion, would you like me to contact you directly? – asimovwasright Jan 14 '16 at 17:06
  • I'll just create another question regarding this.. :) – RaymonN Jan 15 '16 at 06:59
  • Please refer to the question http://stackoverflow.com/questions/34806131/how-can-i-send-email-with-attachment-xls-file-using-shell-script-as-application Thank you! – RaymonN Jan 15 '16 at 07:25

0 Answers0