DavidDunham
DavidDunham

Reputation: 1362

Shell Script (Csh shell) send attachement

I have created a shell script to send emails from linux which works.

I am trying to modify it so that it can send attachements (never ending story, I know). -> Code below

The script accepts 6 parameters as follows

1. Subject
2. Recipient
3. CC
4. From
5. html body (from file)
6. attachement

The script can be execuded as follows:

mailertest.csh "test subject" "[email protected]" "" "[email protected]" "mailbody.txt" "attachement.pdf"

I've gotten so far that it does send attachements but anything other than plain .txt files appear unreadable.

When I compare the files (original .pdf, received .pdf) the only! difference it this top part (I use notepad++ to view it)

Original Document (Working!):

%PDF-1.5
%âãÏÓ
10 0 obj
<</Linearized 1/L 69639/O 12/E 65128/N 1/T 69334/H [ 460 154]>>
endobj

Received Document (Corrupted!):

5128/N 1/T 69334/H [ 460 154]>>
endobj

I am sure I am almost there, but there is something small missing!?

Here is the entire script

#!/bin/csh
set LOGF1=$MXIB_PLOG/mxmail09.log
set SENDSJ="$1"
set SENDTO="$2"
set SENDCC="$3"
set SENDFM="$4"
set MAIFI=$5
set TMPFI=$5_tmp
set ATTACH=$6

set ERRIND=0
if ($?MXIB_MAILPROG == 0) set MXIB_MAILPROG=''
if ($?MXIB_MAILSYFM == 0) set MXIB_MAILSYFM=''
echo "`date` $1 $2 $3 $4 $5"            >>$LOGF1
if ($ERRIND == 0) then
    if ($SENDTO == '') then
        echo "missing mail to address"      >>$LOGF1
        set ERRIND=1
    endif
endif
if ($ERRIND == 0) then
    if ($SENDFM == 'system') then
        if ($MXIB_MAILSYFM == '') then
            set SENDFM=system@`hostname`
        else
            set SENDFM="$MXIB_MAILSYFM"
        endif
    endif


    echo "To: $SENDTO"                  >$TMPFI
    echo "MIME-Version: 1.0"            >>$TMPFI
    echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"' >>$TMPFI
    echo >>$TMPFI
    echo '---q1w2e3r4t5' >>$TMPFI

    echo "Content-Type: text/html"      >>$TMPFI
    echo "Content-Disposition: inline"      >>$TMPFI

    echo "<html>"                   >>$TMPFI
    echo "<body>"                   >>$TMPFI
    echo "<pre style="font: monospace">"        >>$TMPFI
    cat $MAIFI | sed 's/</ /g' | sed 's/>/ /g'  >>$TMPFI
    echo "</pre>"                   >>$TMPFI
    echo "</body>"              >>$TMPFI
    echo "</html>"              >>$TMPFI

    echo "---q1w2e3r4t5"            >>$TMPFI


    echo "Content-Type: application; name=" basename $ATTACH  >>$TMPFI
    echo "Content-Transfer-Encoding: base64"            >>$TMPFI
    echo "Content-Disposition: attachment; filename=$ATTACH"                >>$TMPFI


    base64 $ATTACH  >>$TMPFI


     echo "---q1w2e3r4t5--"                             >>$TMPFI

    if ($MXIB_MAILPROG == 'mutt') then
        if ($SENDCC == '' || $SENDCC == 'none') then
            cat $TMPFI | mutt -s "$1" $SENDTO
        else
            cat $TMPFI | mutt -s "$1" -c $SENDCC $SENDTO
        endif
    else
        ##/usr/lib/sendmail -F $SENDFM -f $SENDFM $SENDTO $SENDCC <$TMPFI
        echo "Subject: $SENDSJ" | cat - $TMPFI | /usr/lib/sendmail -F $SENDFM -f $SENDFM -t $SENDTO
    endif
endif
unset ERRIND
unset SENDFM
unset SENDCC
unset SENDTO
unset SENDSJ
unset MAIFI
unset LOGF1
exit(0)

Upvotes: 1

Views: 231

Answers (1)

DavidDunham
DavidDunham

Reputation: 1362

There needed to be a blank line before the base64 encoded file

This is the line that was missing:

echo ""             >>$TMPFI

in context:

echo "Content-Disposition: attachment; filename=$basename"              >>$TMPFI
    echo ""             >>$TMPFI
    base64 $ATTACH  >>$TMPFI

Upvotes: 1

Related Questions