Home > Net >  Ubuntu mailx attach file without the whole path as the filename
Ubuntu mailx attach file without the whole path as the filename

Time:03-26

I use the below command to send emails in an Ubuntu server. This seems to attach the testreport.csv file with its full path as the filename.

 echo "This email is a test email" | mailx -s 'Test subject' [email protected] -A "/home/dxduser/reports/testreport.csv"

How can I stop this from happening? Is it possible to attach the file with its actual name? In this case "testreport.csv"?

I use mailx (GNU Mailutils) 3.7 version on Ubuntu 20.04

EDIT: Could someone please explain why I got downvoted for this question?

CodePudding user response:

There are multiple different mailx implementations around, so what exactly works will depend on the version you have installed.

However, as a quick and dirty workaround, you can temporarily cd into that directory (provided you have execute access to it):

(  cd /home/dxduser/reports
   echo "This email is a test email" |
   mailx -s 'Test subject' [email protected] -A testreport.csv
)

The parentheses run the command in a subshell so that the effect of the cd will only affect that subshell, and the rest of your program can proceed as before.

I would regard it as a bug if your mailx implementation puts in a Content-Disposition: with the full path of the file in the filename.

An alternative approach would be to use a different client. If you can't install e.g. mutt, creating a simple shell script wrapper to build the MIME structure around a base64 or quoted-printable encoding of your CSV file is not particularly hard, but you have to know what you are doing. In very brief,

( cat <<\:
Subject: test email
Content-type: text/csv
Content-disposition: attachment; filename="testreport.csv"
From: me <[email protected]>
To: you <[email protected]>
Content-transfer-encoding: base64

:
base64 </home/dxduser/reports/testreport.csv
) | /usr/lib/sendmail -oi -t

where obviously you have to have base64 and sendmail installed, and probably tweak the path to sendmail (or just omit it if it's in your PATH).

  • Related