I've created a script that runs every night on my Linux server that uses mysqldump
to back up each of my MySQL databases to .sql files and packages them together as a compressed .tar file. The next step I want to accomplish is to send that tar file through email to a remote email server for safekeeping. I've been able to send the raw script in the body an email by piping the backup text file to mailx
like so:
$ cat mysqldbbackup.sql | mailx backup@email.com
cat
echoes the backup file's text which is piped into the mailx
program with the recipient's email address passed as an argument.
While this accomplishes what I need, I think it could be one step better, Is there any way, using shell scripts or otherwise, to send the compressed .tar file to an outgoing email message as an attachment? This would beat having to deal with very long email messages which contain header data and often have word-wrapping issues etc.
Another alternative - Swaks (Swiss Army Knife for SMTP).
This is how I am doing with one large log file in CentOS:
If the file is text, you can send it easiest in the body as:
There are several answers here suggesting
mail
ormailx
so this is more of a background to help you interpret these in context.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 lowercasemail
command as well, leading to some new confusion. SVR3 (1986) included a derivative which was calledmailx
. Thex
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
ormailx
command. (You can search forProvides: mailx
.)(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 simplemailx
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 calleds-nail
and does support sending MIME attachments with-a
. See its manual page and more generally the Heirloom projectmailutils
aka GNU Mailutils includes amail
/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
.mailx
does have a-a
option now for attachments.I used
and this worked well for me....