Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KornShell (ksh) code to send attachments with mailx and uuencode?

I need to attach a file with mailx but at the moment I am not having success.

Here's my code:

subject="Something happened"
to="[email protected]"
body="Attachment Test"
attachment=/path/to/somefile.csv

uuencode $attachment | mailx -s "$subject" "$to" << EOF

The message is ready to be sent with the following file or link attachments:

somefile.csv

Note: To protect against computer viruses, e-mail programs may prevent
sending or receiving certain types of file attachments.  Check your
e-mail security settings to determine how attachments are handled.

EOF

Any feedback would be highly appreciated.


Update I have added the attachment var to avoid having to use the path every time.

like image 453
Nano Taboada Avatar asked May 06 '26 09:05

Nano Taboada


1 Answers

You have to concat both the text of your message and the uuencoded attachment:

$ subject="Something happened"
$ to="[email protected]"
$ body="Attachment Test"
$ attachment=/path/to/somefile.csv
$
$ cat >msg.txt <<EOF
> The message is ready to be sent with the following file or link attachments:
>
> somefile.csv
>
> Note: To protect against computer viruses, e-mail programs may prevent
> sending or receiving certain types of file attachments.  Check your
> e-mail security settings to determine how attachments are handled.
>
> EOF
$ ( cat msg.txt ; uuencode $attachment somefile.csv) | mailx -s "$subject" "$to"

There are different ways to provide the message text, this is just an example that is close to your original question. If the message should be reused it makes sense to just store it in a file and use this file.

like image 67
Palmin Avatar answered May 09 '26 03:05

Palmin