There are several answers here suggesting mail
or mailx
so this is more of a background to help you interpret these in context. But there are some practical suggestions near the end.
Historical Notes
The origins of Unix mail
go back into the mists of the early history of Bell Labs Unix™ (1969?), and we probably cannot hope to go into its full genealogy here. Suffice it to say that there are many programs which inherit code from or reimplement (or inherit code from a reimplementation of) mail
and that there is no single code base which can be unambiguously identified as "the" mail
.
However, one of the contenders to that position is certainly "Berkeley Mail" which was originally called Mail
with an uppercase M in 2BSD (1978); but in 3BSD (1979), it replaced the lowercase mail
command as well, leading to some new confusion. SVR3 (1986) included a derivative which was called mailx
. The x
was presumably added to make it unique and distinct; but this, too, has now been copied, reimplemented, and mutilated so that there is no single individual version which is definitive.
Back in the day, the de facto standard for sending binaries across electronic mail was uuencode
. It still exists, but has numerous usability problems; if at all possible, you should send MIME attachments instead, unless you specifically strive to be able to communicate with the late 1980s.
MIME was introduced in the early 1990s to solve several problems with email, including support for various types of content other than plain text in a single character set which only really is suitable for a subset of English (and, we are told, Hawai'ian). This introduced support for multipart messages, internationalization, rich content types, etc, and quickly gained traction throughout the 1990s.
(The Heirloom mail
/mailx
history notes were most helpful when composing this, and are certainly worth a read if you're into that sort of thing.)
Current Offerings
As of 2018, Debian has three packages which include a mail
or mailx
command. (You can search for Provides: mailx
.)
debian$ aptitude search ~Pmailx
i bsd-mailx - simple mail user agent
p heirloom-mailx - feature-rich BSD mail(1)
p mailutils - GNU mailutils utilities for handling mail
(I'm not singling out Debian as a recommendation; it's what I use, so I am familiar with it; and it provides a means of distinguishing the various alternatives unambiguously by referring to their respective package names. It is obviously also the distro from which Ubuntu gets these packages.)
bsd-mailx
is a relatively simple mailx
which does not appear to support sending MIME attachments. See its manual page and note that this is the one you would expect to find on a *BSD system, including MacOS, by default.
heirloom-mailx
is now being called s-nail
and does support sending MIME attachments with -a
. See its manual page and more generally the Heirloom project
mailutils
aka GNU Mailutils includes a mail
/mailx
compatibility wrapper which does support sending MIME attachments with -A
With these concerns, if you need your code to be portable and can depend on a somewhat complex package, the simple way to portably send MIME attachments is to use mutt
.
If you know what you are doing, you can assemble an arbitrary MIME structure with the help of echo
and base64
and e.g. qprint
(or homegrown replacements; both base64
and qprint
can easily be implemented as Perl one-liners) and pipe it to sendmail
; but as several other answers on this page vividly illustrate, you probably don't.
( printf '%s\n' \
"From: myself <[email protected]>" \
"To: backup address <[email protected]>" \
"Subject: Backup of $(date)" \
"MIME-Version: 1.0" \
"Content-type: application/octet-stream; filename=\"mysqldbbackup.sql\"" \
"Content-transfer-encoding: base64" \
""
base64 < mysqldbbackup.sql ) |
sendmail -oi -t
This assumes that sendmail
is on your PATH
; sometimes it's not (and of course, sometimes it's simply not installed at all). Look in /usr/lib
, /usr/sbin
, /usr/libexec
or etc; or query your package manager. Once you find it, you might want to augment your PATH
in the script, or hardcode the full pathname to sendmail
(and ditto for any other nonstandard binaries which may or may not be installed on your system).
This still does not attempt to provide any solution for situations where you need to send non-ASCII Unicode text or lines longer than what SMTP allows, etc etc etc. For a robust solution, I would turn to an existing tool like mutt
, or a modern scripting language like Python; https://docs.python.org/3/library/email.examples.html has examples for many common use cases.
mysqldump
and then attaching the output to an email (withmutt
). I may have even had a step that compressed the output to a zip/tar.gz as well... – Pantry