
Couldnt resist
I searched for ages to find a half decient example to send mime attachments from the command line that allows you to specify the from and to address. I found some applications however some either didnt let you specify the from address and others didnt let you do multi-part mime messages and the rest suggested to use uuencode. Uuencode is horrible, no just no!
So I wrote my own using sendmail and openssl and it works very well and is incredibly flexible and small.
Here it is:
#!/bin/bash
SENDMAIL="/usr/sbin/sendmail"
FILESPATH="/home/user/"
FILENAME="1.pdf"
FILETOSEND="${FILESPATH}${FILENAME}"
SUBJ="Subject"
SNDR="test@websurfshack.net"
RECP="user@gmail.com"
MIMEBOUNDRY="0016364d261349d0be047105fb55"
(
echo "Mime-version: 1.0"
echo "Subject: $SUBJ"
echo "From: $SNDR"
echo "To: $RECP"
#cc:
#bcc:
echo "Content-Type: multipart/mixed; boundary=${MIMEBOUNDRY}"
echo "--${MIMEBOUNDRY}"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "Content-Transfer-Encoding: 7bit"
echo # This echo is important or else you get a mime error
echo INSERT MESSAGE HERE
echo "--${MIMEBOUNDRY}"
echo "Content-Type: application/pdf; name=\"${FILENAME}\""
echo "Content-Disposition: attachment; filename=\"${FILENAME}\""
echo "Content-Transfer-Encoding: base64"
echo # This echo is important or else you get a mime error
openssl enc -base64 -in ${FILETOSEND}
echo "--${MIMEBOUNDRY}--"
) | ${SENDMAIL} -t
There we have it, Simple isnt it. It is very basic but im sure you’ll be able to modify it to suit your needs.
UPDATE: ive since found out that you can specify -t as a sendmail switch to force sendmail to use header data as reciepient information. This then enabled you to specify a BCC field. This works with postfix sendmail… not sure about others.
If you know what Unix Sed is it is a way to use regular expressions to change the contents of a file and then save or display it. Generally sed commands are written like this…
cat filename | sed 's/thsi/this/'
Every occurance of ‘thsi’ will be replaced with this. You can also add on the redirect operator to output to a file ‘>’.
You may also want to use an enviroment variable in your statements. I this case use double quotes instead of single.
cat filename | sed "s/thsi/${MYVAR}/"
Now the problem I ran in to that made me want to write this is that you run in to problems if ${MYVAR} is a path as sed tries to make sense of the slashes in the path name and ofcourse we dont want this so you can use different seperators for example the equals sign eg.
MYVAR="/home/user/file"
cat filename | sed "s=%%path%%=${MYVAR}="
This command will take the variable MYVAR and replace '%%path%%' with the contents of it.

Hay look another irrelevant picture!
There is a very useful application with plenty of features that can be downloaded easily.
One of the best features is the ability to start a screen session in ssh from one location, disconnect it then reconnect it at a later time from a different session.
Get it now: aptitude install screen
As it is to be used in console and key commands need to be passed to the bash session that it contains the key commands can be quite complex.
As rule of the thumb all commands start with ^A that is control + a then let go followed by a key or combination of keys. You can get a full list of commands available by pressing ^a + ? in screen.
Create window ^a +c
Close window “exit” no prior key seq.
Go to window 0 ^a + 0
Go to window 2 ^a + 2
Go to last window ^a + ^a
View list of windows ^a + ” (yes quote marks you’ll need to press shift too)
I could go on but this is it in a nutshell.
A common task in bash is to retrieve the details from a string containing a filename. This handy summary helps you quickly get the data you need from the variable.
Consider the following variable
FILE = ref/paper/some.ps.gz
then the following vars are available:
${FILE#*/} returns the path with the first directory removed: paper/some.ps.gz
${FILE##*/} returns the file name with all directories removed: some.ps.gz
$(basename $FILE) returns the file name with all directories removed: some.ps.gz
${FILE#*.} returns all extensions: ps.gz
${FILE##*.} returns the last extension: gz
${FILE%/*} returns the directories: ref/paper
$(dirname $FILE) returns the directories: ref/paper
${FILE%%/*} returns the first directory: ref
${FILE%.*} returns the path with the last extension removed: ref/paper/some.ps
${FILE%%.*} returns the path with all extensions removed: ref/paper/some
Originally from http://itb.biologie.hu-berlin.de/~benda/software/bash.html
Also see http://www.faqs.org/docs/abs/HTML/string-manipulation.html