GNU/Linux | Homebrew designs | Perl | Ruby | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | ERP | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS

·Homebrew designs
·Information Grab Bag
·GNU/Linux ABCs
·Name Resolution
·Network Services
·Remote Control
·All Categories

Automated email of attachments
Topic:Email   Date: 2001-09-13
Printer Friendly: Print   Mobile View: mobile

<<  <   >  >>


It is quite easy to email security or traffic reports automatically. We will use mpack, sendmail, and cron, but certainly the techniques could be combined with other utilities. The challenge with emailing files is that you have to encode the attachments into the email message using MIME. For the nitty gritty on MIME, see

First, let's pack up the file we wish to send. Get a copy of mpack from if you don't already have it. There are binaries, but compiling/installing for us on Linux was as simple as running make and copying the binary to /usr/bin.

Let's say the file we want to send is /usr/tmp/nightly.txt. To create an email named /usr/tmp/nightmail with the file nightly.txt as an attachment and a subject of nightstat:
mpack -s nightstat -o /usr/tmp/nightmail /usr/tmp/nightly.txt

[root@u-1 tmp]# cat nightly.txt
10  listening
20 horror
30 fix horror now
[root@u-1 tmp]#
[root@u-1 tmp]# mpack -s nightstat -o /usr/tmp/nightmail /usr/tmp/nightly.txt
[root@u-1 tmp]# ls -l
total 8
-rw-rw-r--    1 root     root           42 Sep 13 10:38 nightly.txt
-rw-rw-r--    1 root     root          557 Sep 13 10:47 nightmail
[root@u-1 tmp]#
[root@u-1 tmp]# cat nightmail
Message-ID: <[email protected]>
Mime-Version: 1.0
Subject: nightstat
Content-Type: multipart/mixed; boundary="-"
This is a MIME encoded message.  Decode it with "munpack"
or any other MIME reading software.  Mpack/munpack is available
via anonymous FTP in
Content-Type: application/octet-stream; name="nightly.txt"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="nightly.txt"
Content-MD5: RoK4ggHUnLFMrZA2emnmsg==
[root@u-1 tmp]#

To send the mail, use:
[root@u-1 tmp]# sendmail [email protected] < /usr/tmp/nightmail

Finally, just set this up in your crontab to mail the results nightly:
0 1 * * * mpack -s nightstat -o /usr/tmp/nightmail
0 2 * * * sendmail [email protected] < /usr/tmp/nightmail

This will pack the stats at 1am and mail them at 2am every day.


Please read our Terms of Use and our Privacy Policy
Microsoft, Windows, Windows Server are either trademarks or registered trademarks of Microsoft Corporation. is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2013