Bash Script using mailx to mail attachment

DoCnTex picture DoCnTex · Jan 11, 2012 · Viewed 15.7k times · Source

I have a bash script that runs a query in postgres, it outputs to csv. I want to add to that script to use mailx to email that .csv file to a particular email.

The problem I am having is it will not email the file. I can get the email so I know mailx is setup correctly. I just cannot get it to email it as an attachment. It can also email the output in the body of the email.

So here is the code.

    #!/bin/bash
    NOWDATE=`date +%m-%d-%Y`
    PGPASSWORD=password psql -w -h host -p 5432 -d database -U user -o /tmp/folder/file-$NOWDATE.csv <<EOF
    Query is here

    # remove the first 2 lines of the report as they are headers
    sed -i '2d' /tmp/folder/file-$NOWDATE.csv

    uuencode /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]

I have tried the mailx part with:

    uuencode /tmp/folder/file-$NOWDATE.csv /tmp/folder/file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]

and

    uuencode /tmp/folder/file-$NOWDATE.csv file-$NOWDATE.csv | mailx -s "Accounts No Credit Card Report for '$NOWDATE'" [email protected]

So the problem I get is it spits out this error when I run the .sh file.

    uuencode: fopen-ing /tmp/folder/file-01-11-2011.csv: Unknown system error

Answer

Raghuram picture Raghuram · Jan 12, 2012

If the problem is with uuencode...why cant you try mailx -a option which can also attach the files to the mail. Check this link for more info.