mail: send email with attachment from commandline

141,914

Solution 1

The simple way: to use uuencode (part of sharutils package). Any formatting or body text are unavailable. Just a email with attachement and custom subject.

uuencode /path/to/file file_name.ext | mail -s subject [email protected]

The complex way: to use sendmail and html formatting:

v_mailpart="$(uuidgen)/$(hostname)"
echo "To: [email protected]
Subject: subject
Content-Type: multipart/mixed; boundary=\"$v_mailpart\"
MIME-Version: 1.0

This is a multi-part message in MIME format.
--$v_mailpart
Content-Type: text/html
Content-Disposition: inline

<html><body>Message text itself.</body></html>

--$v_mailpart
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream; name=file_name.ext
Content-Disposition: attachment; filename=file_name.ext

`base64 /path/to/file`
 --$v_mailpart--" | /usr/sbin/sendmail -t

in case with several attachments last part may be repeated.

Solution 2

With mutt instead of mail you would simply call

echo "body" | mutt -s "subject" -a attachment0 attachment1 [...] -- [email protected]

Here, attachmentN are the list of files that you want to attach.

Share:
141,914

Related videos on Youtube

Tam Borine
Author by

Tam Borine

Updated on September 18, 2022

Comments

  • Tam Borine
    Tam Borine almost 2 years

    I know how to send an email from command line (script)

    echo "body" | mail -s "subject" [email protected]
    

    Is it possible to send attachments from commandline (script) as well?

    I am using heirloom-mailx on Debian Wheezy.

    • 41754
      41754 over 10 years
      man mail didn't hint me a thing, so I suppose people do not use the standard mail in your UNIX or UNIX-like OS, or they just use another UNIX or UNIX-like OS.
    • Tam Borine
      Tam Borine over 10 years
      I am using heirloom-mailx
    • slm
      slm over 10 years
      See the answer in the duplicate I marked, it shows how to do this.
  • Angel Todorov
    Angel Todorov over 10 years
    The message text does not have to be html, you can use text/plain
  • rush
    rush over 10 years
    @glennjackman yes, that's right. thanks for your note.
  • Tam Borine
    Tam Borine over 10 years
    Thanks, but this does not work as I intended: uuencode /path/to/file file_name.ext | mail -s subject [email protected] I get the email, but instead of attachment, I see the uuencoded text in the body. i.e. there is no "attachment" displayed, no way to "save" the attachment. The only way I can imagine is to cut and paste the text and run it through uuencode again.
  • golimar
    golimar over 3 years
    Does it allow to specify a custom From address?
  • tkrennwa
    tkrennwa over 3 years
    You could use -e for setting a custom From: header, see also mutt.org/doc/manual/#my-hdr mutt -e "my_hdr From: My Name <[email protected]>" ...
  • golimar
    golimar over 3 years
    Works but with a strange result: imgur.com/a/GDwF2kn
  • tkrennwa
    tkrennwa over 3 years
    I guess you need to configure your outgoing mail server en.wikipedia.org/wiki/Message_submission_agent