First of all.

Take a look at MIME tutorial @

its great.. read all 3.

i wrote a perl script after that tutorial.. 

it attach a .log file..

take a look im 100% sure you can port it to php using mail() function

just build up $header and insert into the function.


use strict;
use MIME::Base64;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + 0 *
24 * 60 * 60);
$year += 1900;
if ($mday < 10) {
        $mday = "0$mday";
if ($mon < 10) {
        $mon = "0$mon";

my $baseDir = "/home/mysqlBackup";
my $baseFileName = "mysql-$year-$mon-$mday";

# mysql-yyyy-mm-dd.tar(.gz)
my $backupFile = "$baseFileName.tar";
my $backupFileType = "application/octet-stream";

# mysql-yyyy-mm-dd.log
my $logFile = "$baseFileName.log";
my $logFileType ="text/plain";

# Where mysql database are located.
my $DBFileDir = "/var/lib/mysql";

&doBackup("$baseDir/$logFile", "$baseDir/$backupFile", "$DBFileDir");

my $encBackupFile = &_encodeFile("$baseDir/$backupFile.gz");
my $encLogFile = &_encodeFile("$baseDir/$logFile");

        "Backup completed ($year-$mon-$mday)"
        , "admin\"
        , "$logFile"
        , "$logFileType"
        , "$encLogFile"

sub sendMail {
        my ($subject, $to, $File, $FileType, $Base64) = @_;
        my $priority = "normal";
        my $mailprog = "/usr/local/bin/sendmail";
        my $mime_text = "This part of the E-mail should never be seen. If you ar
e reading this, consider upgrading your e-mail client to a MIME-compatible clien
        if ($to ne "") {
                my $status = open (MAIL,"|$mailprog -t");
                if ($status) {
                        print MAIL "To: $to\n";
                        print MAIL "Subject: $subject\n";
                        print MAIL "Importance: $priority\n";
                        print MAIL "MIME-Version: 1.0\n";

                        print MAIL "Content-Type: multipart/mixed;\n\tboundary=\
                        print MAIL "$mime_text\n";

                        print MAIL "--ZEND-12345\n";
                        print MAIL "Content-Type: text/plain;\n\n";
                        print MAIL "Backup on completed.\n\nFiles a
re located at /home/mysqlBackup\n\nLook at attached logfile for info\n";

                        print MAIL "--ZEND-12345\n";
                        print MAIL "Content-Type: $FileType;\n\tname=\"$File\"\n
                        print MAIL "Content-Transfer-Encoding: base64\n";
                        print MAIL "Content-Disposition: attachment\n\tfilename=
                        print MAIL "$Base64\n";
                        print MAIL "--ZEND-12345--\n";


sub doBackup {
        my ($logFile, $backupFile, $backupDir) = @_;
        `tar cvvf $backupFile $backupDir > $logFile`;
        `gzip $backupFile`;
        `chmod 400 $logFile $backupFile.gz`;

sub _encodeFile {
        my ($file) = @_;
        my ($buf, $encoded);
        open(FILE, "$file") or die "$!";
                while (read(FILE, $buf, 60*57)) {
                $encoded.= encode_base64($buf);
        return $encoded;

/Mattias aka BoNzO

PHP Database Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to