307

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.example

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Kit Roed
  • 5,167
  • 5
  • 30
  • 34
  • 1
    Can you share the script that backup your MySQL databases? – Almino Melo Nov 25 '14 at 17:38
  • Sorry, I haven't been doing this for a while now. I know it involved invoking `mysqldump` and then attaching the output to an email (with `mutt`). I may have even had a step that compressed the output to a zip/tar.gz as well... – Kit Roed Dec 10 '14 at 14:47
  • 1
    Purely curious, why email your backups vs scp or rsync them? – jchook Apr 05 '18 at 00:04
  • cat dados | mailx xxxx@gmail.com worked perfectly for me inside a python program with dados being a file containing the results of the program. I wrote a function to catch the results of the program, "dados" then the line os.system(' cat dados | mailx xxxx@gmail.com') has sent to my e-mail the file. Perfect! – user3648948 Sep 01 '21 at 19:55

25 Answers25

308

None of the mutt ones worked for me. It was thinking the email address was part of the attachment. Had to do:

echo "This is the message body" | mutt -a "/path/to/file.to.attach" -s "subject of message" -- recipient@domain.example
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
rynop
  • 50,086
  • 26
  • 101
  • 112
  • 1
    I'm using mutt 1.5.21 (2010-09-15) and it requires -a parameter to be after recipient email – nurettin Jan 30 '14 at 07:14
  • Worked for me using Mutt 1.5.24 (2015-08-30) on openSUSE Leap 42.1. – Antônio Medeiros Oct 21 '16 at 13:33
  • 3
    @fugitive means "end of options". Take a look at http://unix.stackexchange.com/questions/11376/what-does-double-dash-mean-also-known-as-bare-double-dash – rynop Feb 09 '17 at 16:27
  • 1
    Is there a way to check for exceptions and retry sending? – ti034 Jul 16 '18 at 19:02
  • That's a complex follow-up question. Roughly speaking, there are two failure modes: `mutt` fails and you can pick up its exit code; or, sending succeeds but you get a delivery notification (bounce) eventually and then have to react to that. The latter is complex enough to handle properly that it's the business idea of several companies. – tripleee Dec 07 '22 at 15:20
  • This is all perfect, so long as SPF is not in place (like sending emails to Gmail in 2023, for example). Make sure you adjust your `.muttrc` to reflect the proper MTA settings. – ikaerom Mar 03 '23 at 18:01
80

Or, failing mutt:

gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB" backup@email.com
Daniel Fone
  • 2,825
  • 2
  • 23
  • 20
  • 27
    This sends the uuencoded part **inline** and not as an **attachment**. Many mail-clients recognize this though and _display_ the uuencoded part as an attachment. – FuePi Jul 06 '11 at 13:14
  • 4
    Don't use uuencode in this day and age. MIME is slightly more complex but a lot more user-friendly. – tripleee Sep 24 '12 at 18:59
  • @DavidGiven: See for example (by quick glance) all the other answers to this question. – tripleee Sep 11 '13 at 20:00
  • 6
    None of them use mail! – David Given Sep 12 '13 at 09:09
  • Then e.g. https://stackoverflow.com/questions/3317174/sending-html-mail-using-a-shell-script and replace `text/html` with whatever MIME type makes sense for your attachment. (For this concrete example, I guess `application/gzip`.) – tripleee Apr 07 '18 at 12:32
52

Depending on your version of Linux it may be called mail. To quote @David above:

mail -s "Backup" -a mysqldbbackup.sql backup@email.example < message.txt

or also:

cat message.txt | mail -s "Backup" -a mysqldbbackup.sql backup@email.example
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • Both solutions doesn't work for me. I received the email with outlook 2013 and the mail only contains the filename – nickel715 Aug 26 '14 at 14:07
  • @nickel715: could it be that `mail` on your system is aliased to anything? – Nathan Fellman Aug 26 '14 at 17:45
  • 2
    my manpage reads: ``-a, --append=HEADER: VALUE append given header to the message being sent`` – exhuma Oct 27 '14 at 14:01
  • @exhuma and Nathan, what system are you on with this `mail` program and where does it come from? In Ubuntu the "mail" program is the GNU mail program from mailutils, which doesn't have a `-a` option to attach a file. http://manpages.ubuntu.com/manpages/lucid/en/man1/mail.1.html – nealmcb Jan 14 '15 at 14:47
  • @nealmcb Mine also only has an option to append a header with the `-a` option. Also on Ubuntu using mailutils. – exhuma Jan 15 '15 at 15:27
  • 2
    Nathan, it looks like your quote from David is wrong - he used the `mutt` command, not `mail`. Also as others have pointed out, mutt now seems to require a `--` argument before the address. And I see that @exhuma and I actually agree on what the `-a` option in `mail` does - I got confused there for a minute ;) – nealmcb Jan 15 '15 at 15:37
  • 13
    instead of -a you should use -A: `-a, --append=HEADER: VALUE append given header to the message being sent` `-A, --attach=FILE attach FILE` – Victor Perov Oct 22 '15 at 14:06
  • @Victor Perov I'm pretty sure that is wrong, at least in current flavours of mutt... -A : expand an alias, -a [...] : attach a file to a message – Gavin Jackson Jul 28 '17 at 12:34
  • Worked for me as is on RHEL 7.9 where mail is symlinked to mailx – Wayne Apr 29 '22 at 06:38
  • Like my answer explains, there are several incompatible implementations of `mail`/`mailx` which no doubt adds to the confusion here in the comments. – tripleee Aug 20 '22 at 06:40
41

From looking at man mailx, the mailx program does not have an option for attaching a file. You could use another program such as mutt.

echo "This is the message body" | mutt -a file.to.attach -s "subject of message" recipient@example.com

Command line options for mutt can be shown with mutt -h.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Chris N
  • 627
  • 7
  • 7
31

I use SendEmail, which was created for this scenario. It's packaged for Ubuntu so I assume it's available

sendemail -f sender@some.where -t receiver@some.place -m "Here are your files!" -a file1.jpg file2.zip

http://caspian.dotconf.net/menu/Software/SendEmail/

Fredrik Wendt
  • 988
  • 1
  • 10
  • 17
  • I believe, it should be "sendEmail" instead of "sendemail" in your example. – Davit Jun 29 '14 at 20:04
  • 1
    Yes and no - the source (caspian) uses an uppercase E, while this is uncommon naming strategy for command line tools (in the Unix world), at least the Ubuntu packaging of this software provides both `sendemail`and `sendEmail` in `/usr/bin/`. – Fredrik Wendt Jun 30 '14 at 17:15
  • My Xubuntu 14.04.3 not have installed `SendEmail` – Vitaly Zdanevich Sep 16 '15 at 16:01
  • 3
    "sudo apt install sendemail" to install sendemail and add "-f mandatory@email.com" a mandatory from field for the command to work. `sendemail -f mandatory@email.com-t to@some.one -m "Here are your files!" -a file1.jpg file2.zip` – Sailendra Pinupolu Dec 05 '16 at 01:37
  • 1
    Probably the best option in Ubuntu: no mess with the different mail/mailx/Mail packages, can attach files and can specify a custom From address (instead of the ugly default root@ip.address.or.domain) – golimar Nov 24 '20 at 09:17
  • the link to the sendemail docu is dead. – Timo Feb 28 '22 at 19:01
  • well, the configuration caspian.dotconf.net is broken - if you go to the "root" of the domain, and then back, the page loads fine :-/ – Fredrik Wendt Mar 12 '22 at 18:16
29

I use mpack.

mpack -s subject file user@example.com

Unfortunately mpack does not recognize '-' as an alias for stdin. But the following work, and can easily be wrapped in an (shell) alias or a script:

mpack -s subject /dev/stdin loser@example.com < file
  • This could work in bash for stdin. I don't have mpack, so I have not tried: `mpack -s subject /dev/stdin loser@example.com <(stdout_generating_program)` – thomasa88 Apr 01 '14 at 05:44
24
 echo -e 'Hi, \n These are contents of my mail. \n Thanks' | mailx -s 'This is my email subject' -a /path/to/attachment_file.log -b bcc.email@example.com -c cc.email@example.com -r from.email@example.com to.email1@example.com to.email2@example.com to.email3@example.com
Sourabh Potnis
  • 1,431
  • 1
  • 17
  • 26
  • 5
    This should be the accepted answer. Uses default mailx and works perfectly. Mutt v1.5.21 refuses to send >1MB attachments when using cron. – Pavin Joseph May 05 '16 at 17:46
  • I was looking for something which works in EC2 and this worked fine. – singularity Jun 28 '16 at 13:24
  • 8
    `mailx` is not properly standardized. Any answer which recommends it should point out this caveat. There are at least three incompatible variants in common use. – tripleee Feb 02 '18 at 17:31
  • 1
    (Also, anything which uses `echo -e` should probably not receive upvotes, though I'm not upset enough to downvote just for that.) – tripleee Feb 09 '22 at 08:02
14

I once wrote this function for ksh on Solaris (uses Perl for base64 encoding):

# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
    to="$1"
    cc="$2"
    subject="$3"
    body="$4"
    filename="${5:-''}"
    boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
    {
        print -- "To: $to"
        print -- "Cc: $cc"
        print -- "Subject: $subject"
        print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
        print -- "Mime-Version: 1.0"
        print -- ""
        print -- "This is a multi-part message in MIME format."
        print -- ""
        print -- "--$boundary"
        print -- "Content-Type: text/plain; charset=ISO-8859-1"
        print -- ""
        print -- "$body"
        print -- ""
        if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
            print -- "--$boundary"
            print -- "Content-Transfer-Encoding: base64"
            print -- "Content-Type: application/octet-stream; name=$filename"
            print -- "Content-Disposition: attachment; filename=$filename"
            print -- ""
            print -- "$(perl -MMIME::Base64 -e 'open F, shift; @lines=<F>; close F; print MIME::Base64::encode(join(q{}, @lines))' $filename)"
            print -- ""
        fi
        print -- "--${boundary}--"
    } | /usr/lib/sendmail -oi -t
}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
13

You can use mutt to send the email with attachment

mutt -s "Backup" -a mysqldbbackup.sql backup@example.com < message.txt
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
David Schlosnagle
  • 4,263
  • 2
  • 23
  • 16
  • 2
    At least as of mutt 1.5.21 (Ubuntu trusty), you need to put the `-a` option after the recipient: `mutt -s "Backup" backup@email.com -a mysqldbbackup.sql < message.txt`, or use the `--` option before the recipient as shown in rynop's answer. – nealmcb Jan 15 '15 at 15:45
11

Send a Plaintext body email with one plaintext attachment with mailx:

(
  /usr/bin/uuencode attachfile.txt myattachedfilename.txt; 
  /usr/bin/echo "Body of text"
) | mailx -s 'Subject' youremail@example.com

Below is the same command as above, without the newlines

( /usr/bin/uuencode /home/el/attachfile.txt myattachedfilename.txt; /usr/bin/echo "Body of text" ) | mailx -s 'Subject' youremail@example.com

Make sure you have a file /home/el/attachfile.txt defined with this contents:

<html><body>
Government discriminates against programmers with cruel/unusual 35 year prison
sentences for making the world's information free, while bankers that pilfer 
trillions in citizens assets through systematic inflation get the nod and 
walk free among us.
</body></html>

If you don't have uuencode read this: https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work

On Linux, Send HTML body email with a PDF attachment with sendmail:

Make sure you have ksh installed: yum info ksh

Make sure you have sendmail installed and configured.

Make sure you have uuencode installed and available: https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work

Make a new file called test.sh and put it in your home directory: /home/el

Put the following code in test.sh:

#!/usr/bin/ksh
export MAILFROM="el@defiant.com"
export MAILTO="youremail@example.com"
export SUBJECT="Test PDF for Email"
export BODY="/home/el/email_body.htm"
export ATTACH="/home/el/pdf-test.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
export MAILPART_BODY=`uuidgen` ## Generates Unique ID

(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
 echo ""
 echo "--$MAILPART"
 echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
 echo ""
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/plain; charset=ISO-8859-1"
 echo "You need to enable HTML option for email"
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/html; charset=ISO-8859-1"
 echo "Content-Disposition: inline"
 cat $BODY
 echo "--$MAILPART_BODY--"

 echo "--$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 echo ""
 uuencode $ATTACH $(basename $ATTACH)
 echo "--$MAILPART--"
) | /usr/sbin/sendmail $MAILTO

Change the export variables on the top of test.sh to reflect your address and filenames.

Download a test pdf document and put it in /home/el called pdf-test.pdf

Make a file called /home/el/email_body.htm and put this line in it:

<html><body><b>this is some bold text</b></body></html>

Make sure the pdf file has sufficient 755 permissions.

Run the script ./test.sh

Check your email inbox, the text should be in HTML format and the pdf file automatically interpreted as a binary file. Take care not to use this function more than say 15 times in a day, even if you send the emails to yourself, spam filters in gmail can blacklist a domain spewing emails without giving you an option to let them through. And you'll find this no longer works, or it only lets through the attachment, or the email doesn't come through at all. If you have to do a lot of testing on this, spread them out over days or you'll be labelled a spammer and this function won't work any more.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
user1651561
  • 197
  • 3
  • 3
  • 1
    For me it worked the other way round. `(echo 'Email Body'; uuencode filename filename) | mailx -s 'Subject' user@domain.com` – Vicky Sep 18 '13 at 07:41
  • 1
    `uuencode` is *not* properly an attachment. It simply embeds a oomputer-readable blob of text in the middle of some other text. It used to work fine when there wasn't any better mechanism, but that was 20+ years ago. – tripleee Feb 02 '18 at 17:47
  • None of the code here seems to use any `ksh` features. If you have something which doesn't work with POSIX `sh`, probably prefer Bash over `ksh`, which often uses the same syntax anyway. – tripleee Jul 23 '22 at 10:48
11

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 <sender@example.org>" \
    "To: backup address <backup@email.example>" \
    "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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I originally posted this as an answer to a moderately popular duplicate question https://stackoverflow.com/questions/902591/how-to-attach-a-file-using-mail-command-on-linux – tripleee Feb 02 '18 at 17:39
  • For a quick and dirty intro to MIME structures, perhaps see https://stackoverflow.com/questions/48562935/what-are-the-parts-in-a-multipart-email/48563281#48563281 – tripleee Nov 03 '22 at 12:26
  • The simple example shell script allegedly doesn't work with Outlook; my recommendation would be to get rid of Outlook in that scenario, but if you can't, try adding a useless text part before the attachment. – tripleee Dec 07 '22 at 17:05
6

Another alternative - Swaks (Swiss Army Knife for SMTP).

swaks -tls \
    --to ${MAIL_TO} \
    --from ${MAIL_FROM} \
    --server ${MAIL_SERVER} \
    --auth LOGIN \
    --auth-user ${MAIL_USER} \
    --auth-password ${MAIL_PASSWORD} \
    --header "Subject: $MAIL_SUBJECT" \
    --header "Content-Type: text/html; charset=UTF-8" \
    --body "$MESSAGE" \
    --attach mysqldbbackup.sql
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
  • 1
    This is absolutely brilliant. It's the only solution which in the end allowed me to use the `smtp.gmail.com` server and an app password (https://support.google.com/accounts/answer/185833#app-passwords) to send emails from the command line. A lot has changed since the early 90s, apparently, when I last used to send email from CLI. – ikaerom Mar 03 '23 at 18:26
3

Mailutils makes this a piece of cake

echo "Body" | mail.mailutils -M -s "My Subject" -A attachment.pdf mail@example.org
  • -A file attaches a file
  • -M enables MIME, so that you can have an attachment and plaintext body.

If not yet installed, run

sudo apt install mailutils
rumpel
  • 7,870
  • 2
  • 38
  • 39
2

metamail has the tool metasend

metasend -f mysqlbackup.sql.gz -t backup@email.com -s Backup -m application/x-gzip -b
Gunstick
  • 431
  • 4
  • 3
  • 1
    This used to be installed almost everywhere, but almost never used. Because it was unmaintained for a long time (and still is AFAIK) it has been removed from the *de facto* standard toolset on many platforms. – tripleee Jun 13 '19 at 04:03
1

I usually only use the mail command on RHEL. I have tried mailx and it is pretty efficient.

mailx -s "Sending Files" -a First_LocalConfig.conf -a
Second_LocalConfig.conf Recipient@myemail.com

This is the content of my msg.

.
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
1

I used

echo "Start of Body" && uuencode log.cfg readme.txt | mail -s "subject" "a@b.c" 

and this worked well for me....

poncho
  • 19
  • 1
  • The `echo` is useless here; it will output the text to the terminal, not into the pipe to `mail`. As in other similar answers here, `uuencode` is not "an attachment", although some email clients will helpfully hide the ugliness so that it appears to be one if you don't look closely. – tripleee Mar 28 '22 at 08:58
1

From source machine

mysqldump --defaults-extra-file=sql.cnf database | gzip | base64 | mail me@myemail.com

On Destination machine. Save the received mail body as db.sql.gz.b64; then..

base64 -D -i db.sql.gz.b64 | gzip -d | mysql --defaults-extra-file=sql.cnf
Konchog
  • 1,920
  • 19
  • 23
  • 1
    This gets the data across, but lacks the MIME headers to tell the user what to do with it. Unless they know what it is, they will probably not be able to figure out how to use it. A proper MIME structure would at least display the base64 data as an attachment instead of as the actual message text. – tripleee Mar 28 '22 at 08:56
  • You got me there @tripleee - I was not really considering using such a mechanism for third parties. Most people I know wouldn’t know what to do with it even if they did have Mime headers to help them. – Konchog Mar 28 '22 at 09:27
1

using mailx command

 echo "Message Body Here" | mailx -s "Subject Here" -a file_name user@example.com

using sendmail

#!/bin/ksh

fileToAttach=data.txt

`(echo "To: user@company.example"
  echo "Cc: user@company.example"
  echo "From: Application"
  echo "Subject: your subject"
  echo  your body
  uuencode $fileToAttach $fileToAttach
  )| eval /usr/sbin/sendmail -t `;
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
  • 1
    sendmail is nice example, glad I found it here. – Honza P. Nov 22 '18 at 09:42
  • Note that you need an empty line at the start of "your body", otherwise the body will disappear into the headers, or possibly break the message entirely. Also see notes elsewhere about avoiding `uuencode` in favor of MIME. – tripleee Jun 13 '19 at 04:06
  • And the monstrous `eval` and the mystery backticks around the whole contraption are completely unnecessary here. – tripleee Jun 13 '19 at 04:07
0

Just to add my 2 cents, I'd write my own PHP Script:

http://php.net/manual/en/function.mail.php

There are lots of ways to do the attachment in the examples on that page.

Mike Graf
  • 5,077
  • 4
  • 45
  • 58
  • 2
    Not every server may have PHP installed. If you really want to go down the "write your own script" path, then perl or python are much better suited as they are usually available by default. – exhuma Oct 27 '14 at 14:04
  • `sh` is even more ubiquitous. There are duplicate questions with answers with good examples; [here is mine.](/a/26133780/874188) – tripleee Nov 18 '18 at 13:22
0

mailx does have a -a option now for attachments.

sth
  • 222,467
  • 53
  • 283
  • 367
Allan Pinto
  • 144
  • 1
  • 1
  • 10
  • 2
    The "-a" option is for headers – Yves Martin Feb 14 '14 at 11:37
  • man mail[x], version 12.5 of 10/9/10 (a few years ago) clearly says -a file Attach the given file to the message.` – fche Aug 12 '14 at 14:27
  • 4
    *some* versions of ``mailx`` do. I believe there are two implementations. On one ``-a`` is for attachments, on the other it is for headers. – exhuma Oct 27 '14 at 14:02
  • 1
    The version of mailx in Ubuntu comes from GNU and there -a means add a header. http://manpages.ubuntu.com/manpages/lucid/en/man1/mailx.1.html Which system and which mailx does an attachment? – nealmcb Jan 14 '15 at 14:51
  • 3
    In new implementation "-a" is for Headers and "-A" is for attchments – avimehenwal Aug 18 '15 at 11:42
  • It's not old vs new, it's just different heritages. – tripleee Feb 02 '18 at 17:45
0

Not a method for sending email, but you can use an online Git server (e.g. Bitbucket or a similar service) for that.

This way, you can use git push commands, and all versions will be stored in a compressed and organized way.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Yoav
  • 5,962
  • 5
  • 39
  • 61
0

the shortest way for me is

file=filename_or_filepath;uuencode $file $file|mail -s "optional subject" email_address

so for your example it'll be

file=your_sql.log;gzip -c $file;uuencode ${file}.gz ${file}|mail -s "file with magnets" ph.gachoud@gmail.com

the good part is that I can recall it with Ctrl+r to send another file...

Pipo
  • 4,653
  • 38
  • 47
  • The braces won't do you much good but to be completely portable you should use double quotes around the variables. See also https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642 – tripleee Jun 13 '19 at 04:05
0

This is how I am doing with one large log file in CentOS:

#!/bin/sh
MAIL_CMD="$(which mail)"
WHOAMI="$(whoami)"
HOSTNAME="$(hostname)"
EMAIL"your@email.address"
LOGDIR="/var/log/aide"
LOGNAME="$(basename "$0")_$(date "+%Y%m%d_%H%M")"

if cd ${LOGDIR}; then
  /bin/tar -zcvf "${LOGDIR}/${LOGNAME}".tgz "${LOGDIR}/${LOGNAME}.log" > /dev/null 2>&1
  if [ -n "${MAIL_CMD}" ]; then
  # This works too. The message content will be taken from text file below
  # echo 'Hello!' >/root/scripts/audit_check.sh.txt
  # echo "Attachment" | ${MAIL_CMD} -s "${HOSTNAME} Aide report" -q /root/scripts/audit_check.sh.txt -a ${LOGNAME}.tgz -S from=${WHOAMI}@${HOSTNAME} ${EMAIL}
    echo "Attachment" | ${MAIL_CMD} -s "${HOSTNAME} Aide report" -a "${LOGNAME}.tgz" -S from="${WHOAMI}@${HOSTNAME}" "${EMAIL}"
    /bin/rm "${LOGDIR}/${LOGNAME}.log"
  fi
fi
dagorv
  • 9
  • 2
  • Why are you defining `WHOAMI` and `HOSTNAME` **twice?** – David C. Rankin Sep 04 '15 at 14:26
  • This has several shell coding style mistakes. http://shellcheck.net/ will point out some, but not all, of them. – tripleee Feb 02 '18 at 17:29
  • Using `which` to find a program on your `PATH` so you can call it with the full path is ... silly, and also not properly portable. Just use `mail` instead of `${MAIL_CMD}` everywhere for for exactly the same effect, with better robustness and portability. The properly POSIX-compatible equivalent of `which` is `command -v` or `type`, but you don't need any of that here. – tripleee Aug 20 '22 at 06:42
0

If the file is text, you can send it easiest in the body as:

sendmail recipient@example.com < message.txt
nurp
  • 1,239
  • 2
  • 14
  • 23
  • 1
    This does not send it as an attachment, or even as the message body. Sendmail expects its input to be a complete, well-formed RFC5322 email message, and may fail in interesting ways if it isn't. – tripleee Feb 02 '18 at 17:41
-1

If mutt is not working or not installed,try this-

*#!/bin/sh

FilePath=$1
FileName=$2
Message=$3
MailList=$4

cd $FilePath

Rec_count=$(wc -l < $FileName)
if [ $Rec_count -gt 0 ]
then
(echo "The attachment contains $Message" ; uuencode $FileName $FileName.csv ) | mailx -s "$Message" $MailList
fi*
Paras Singh
  • 383
  • 1
  • 3
  • 11
  • This has numerous coding errors which will make it appear to work with trivial examples, but break in real life. See http://shellcheck.net/ for some recommendations, though it will not tell you to avoid `uuencode` – tripleee Feb 09 '22 at 08:04