Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my script:

<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

backup_tables('localhost','xxxxx','xxxxx','xxxxx');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);

    //get all of the tables
    if($tables == '*')
    {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while($row = mysql_fetch_row($result))
        {
            $tables[] = $row[0];
        }
    }
    else
    {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }

    //cycle through
    foreach($tables as $table)
    {
        $result = mysql_query('SELECT * FROM '.$table);
        $num_fields = mysql_num_fields($result);

        $return.= 'DROP TABLE '.$table.';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        $return.= "\n\n".$row2[1].";\n\n";

        for ($i = 0; $i < $num_fields; $i++) 
        {
            while($row = mysql_fetch_row($result))
            {
                $return.= 'INSERT INTO '.$table.' VALUES(';
                for($j=0; $j<$num_fields; $j++) 
                {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                    if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                    if ($j<($num_fields-1)) { $return.= ','; }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n";
    }

    $backup_file = $_SERVER['DOCUMENT_ROOT'] . '/scripts_and_crons/fr_internetlead-'.time().'.sql';
    //save file
    $handle = fopen($backup_file,'w+');
    fwrite($handle,$return);
    fclose($handle);
}

//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($backup_file))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/sql; name="<?php echo $backup_file; ?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

?>

The only debugging info I get in php are

Notice: Undefined variable: return in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 38

and

Deprecated: Function ereg_replace() is deprecated in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 50 x infinity

The only attachment I receive in the email is ATT00001.

echo '<pre>';
echo $return;
echo '</pre>';

Prints out the correct data.

Even doing

echo '<pre>';
echo file_get_contents($backup_file);
echo '</pre>';

Prints out the correct data so the file is being written to but for some reason the script fails at attaching it to the email.

Can't see what could be wrong, any ideas?

Martin

UPDATE

Found the answer in another SO question - PHP mail() attachment problems

That is the correct way to do it.

share|improve this question
1  
It must be unique so we use the MD5 algorithm to generate a random hash That was a good laugh. –  Bobby Apr 29 at 12:02
 
Also mysql_* is deprecated and is replaced by mysqli_* or even better PDO. –  Bobby Apr 29 at 12:04
 
yes, mysql_* are deprecated, apart from that please. –  martincarlin87 Apr 29 at 12:06
 
not really sure but $return variable doesn't have a first value but you are trying to connect something to it with .= –  Kuzgun Apr 29 at 12:13
 
@Kuzgun - thanks, that's why the notice is reported but it's not really anything serious and shouldn't stop the script from working. –  martincarlin87 Apr 29 at 12:15
add comment

1 Answer

up vote 0 down vote accepted

If your php version is higher than 5.2 you should replace ereg_replace with preg_match and change (“ parts as (“/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.