17

I can send email from my gmail account by ssmtp in Linux now. But how can I attach files to the email?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
deepsky
  • 423
  • 2
  • 5
  • 8

5 Answers5

25
echo -e "to: [email protected]\nsubject: subject\n"| (cat - && uuencode /path/to/attachment attachment.name) | ssmtp [email protected]

This solution does not depend on mutt.

d5c0d3
  • 3
  • 2
metrix
  • 353
  • 3
  • 5
9

To send an attachment, you need to encode the message using MIME.

You could use Mutt

mutt -s SUBJECT -a ATTACHMENT_FILE_1 ATTACHMENT_FILE_2 -- EMAIL_ADDRESS < MESSAGE_FILE

or mpack

mpack -s SUBJECT -D MESSAGE_FILE ATTACHMENT_FILE EMAIL_ADDRESS

See also:

Iwan B.
  • 103
  • 2
Mikel
  • 56,387
  • 13
  • 130
  • 149
  • Thanks. So is that to say that it is NOT possible to send an attachment by ssmtp alone? – deepsky Jul 26 '12 at 15:26
  • 3
    It's possible, but you'd have to generate all the MIME headers somehow. Why do you need to only use `ssmtp`? If `ssmtp` provides `/usr/lib/sendmail` or `/usr/sbin/sendmail`, any local mail program should be able to send via `ssmtp`. – Mikel Jul 26 '12 at 15:34
  • 3
    @deepsky ssmtp is not a user interface. It's an MTA (mail transport agent), which is infrastructure meant to be accessed via an MUA (mail user agent, also known as a "mailreader"). It's sort of impressive that you were able to wrangle ssmtp by yourself without an MUA, but it's not a reasonable method of doing things. It's a good network-debugging skill you've learned though! – Alan Curry Jul 27 '12 at 04:47
1
$ echo -e "to: [email protected]\nsubject: test\n"| (cat - && uuencode /path/to/file file.name) | ssmtp [email protected]

Provided that SSMTP is configured, and you've verified that messages without attachments are reaching their destination and does not depend on mutt.

metrix
  • 353
  • 3
  • 5
Anon
  • 21
  • 1
0
 $ sudo apt-get install uudeview
 $ echo -e "From: [email protected]\nTo: [email protected]\nSubject: mySubject\n\nBody-Text"|uuenview -a -bo MyAttachment|sendmail -t
Michi
  • 9
  • 1
0

another alternative to uuencode is to use base64 commend instead ->

cat msg_source.txt | (cat - base64 && attachment.bin) | ssmtp -vvvv [email protected]

where msg_source.txt contains header tags, like To:, From:, Subject:, Content-Type:, etc

Zonder
  • 1