I need to sent a file via mailx or mail, but I wat to sent it as attachment not in the body message. Is there any way how to do it ? Eventually is there any other tool in solaris which can be used for such as procedure ? Thanks
5 Answers
You can attach files to mailx using -a like so
echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg Someone@Domain.com
so long as your in the same directory as your attachment that should work fine. If not you can just state the directory like `
samachPicsFolder/samachpic.jpg

- 233,099
- 56
- 391
- 304

- 674
- 1
- 5
- 17
-
If you have a `mailx` which supports `-a`, good for you. If you don't, maybe look at `mutt`. If you don't want external dependencies and need to be portable across U*x platforms, you will have to write a few lines of script. – tripleee Oct 01 '14 at 03:25
If your mailx
doesn't support the -a
option and you don't have access to mutt
, and you don't want to turn to uuencode
as a fallback from the 1980s, as a last resort you can piece together a small MIME wrapper yourself.
#!/bin/sh
# ... do some option processing here. The rest of the code
# assumes you have subject in $subject, file to be attached
# in $file, recipients in $recipients
boundary="${RANDOM}_${RANDOM}_${RANDOM}"
(
cat <<____HERE
Subject: $subject
To: $recipients
Mime-Version: 1.0
Content-type: multipart/related; boundary="$boundary"
--$boundary
Content-type: text/plain
Content-transfer-encoding: 7bit
____HERE
# Read message body from stdin
# Maybe apply quoted-printable encoding if you anticipate
# overlong lines and/or 8-bit character codes
# - then you should change the last body part header above to
# Content-Transfer-Encoding: quoted-printable
cat
cat <<____HERE
--$boundary
Content-type: application/octet-stream; name="$file"
Content-disposition: attachment; filename="$file"
Content-transfer-encoding: base64
____HERE
# If you don't have base64 you will have to reimplement that, too /-:
base64 "$file"
cat <<____HERE
--$boundary--
____HERE
) | sendmail -oi -t
The path to sendmail
is often system-dependent. Try /usr/sbin/sendmail
or /usr/lib/sendmail
or ... a myriad other weird places if it's not in your PATH
.
This is quick and dirty; for proper MIME compliance, you should do RFC2047 encoding of the subject if necessary, etc, and see also the notes in the comments in the code. But for your average US-centric 7-bit English-language cron job, it will do just fine.

- 175,061
- 34
- 275
- 318
-
1Awesome!!! Thanks!! This script works great for all the text files. I haven't tried for images. – Prasanth Pennepalli Jul 13 '17 at 00:44
-
`base64` should work fine for any type of content, the expected complications would be mainly if you cannot figure out the correct value to put in the `Content-Type:` header of the attachment body part. – tripleee Jul 13 '17 at 06:48
Try using this command in order to send an attachment using Mailx:
uuencode source_file encoded_filename |mailx -m -s "Subject" something@something.com

- 7,036
- 7
- 41
- 64

- 11
- 1
-
1Can you explain your answer? In particular, why use uuencode, when email uses mostly base64 for attachments? Why not use the `-a` option to mailx? – Robert Mar 14 '17 at 20:20
-
`uuencode` is an older pseudo-standard which predates MIME and base64 by at least a decade. It's not pretty, but if you are stuck on a dinosaur where nothing newer is available, it gets the job done. Granted, most use these days is probably by people who just don't know better. As for `mailx` with a particular option (other than something really basic like `-s`) it's not portable - the OP probably only has the version which lacks that option. – tripleee Apr 12 '19 at 03:08
Regarding mailx, you can find some inspiration here http://www.shelldorado.com/articles/mailattachments.html
I would recommend you to have a look at mutt http://www.mutt.org/

- 44,604
- 7
- 83
- 130
-
thanks but mutt is not installed there, and I have no permission to install it. So is there any other way how to solve it ? – slafik Jun 22 '11 at 09:22
I'd recommend using mutt
for it, which is light-weight enough to quickly install on any system.

- 1
- 1

- 7,274
- 1
- 32
- 50
-
I have no mutt there and no possibility to install it. Is there any way how can I sent that file via mailx or mail ? I have tried via mail tool but its not working, I mean the file is sent in body of mail and that is not sufficient for me. any ideas ? – slafik Jul 01 '11 at 21:24
-