SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Using PHP to send database via email

    Hello,

    Below is a script that I found to copy my database to a zipped file and then mail it to me. I get no error messages, the file shows up, but there's no data inside the file. This is my first time using mysqldump, but from what I've seen online, the line that does the dump looks fine. If anybody has any thoughts, that would be great!

    (One thing that I'm not clear about, is how the script connects with the DB on my server...shouldn't it have some sort of server name connection??

    Also, I can't imagine that this has anything to do with anything, but I'm using GoDaddy.com as my host)

    -Eric

    PHP Code:
    <?php
    include('/users/admin/database_configuration.php'); //where I keep my $dbusername, $dbpassword, $dbname
    $datestamp date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD

    /* CONFIGURE THE FOLLOWING  VARIABLES TO MATCH YOUR SETUP */
    $filename"backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file
    $to "support@myalgebrabook.com";      // Email address to send dump file to
    $from "support@myalgebrabook.com";      // Email address message will show as coming from.
    $subject "MySQL backup file";      // Subject of email

    $command "mysqldump -u $dbusername --password=$dbpassword $dbname | gzip > $filename";
    $result passthru($command);

    $attachmentname array_pop(explode("/"$filename));   // If a path was included, strip it out for the attachment name

    $message "Compressed database backup file $attachmentname attached.";
    $mime_boundary "< <<:" md5(time());
    $data chunk_split(base64_encode(implode(""file($filename))));

    $headers "From: $from\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: multipart/mixed;\r\n";
    $headers .= " boundary=\"".$mime_boundary."\"\r\n";

    $content "This is a multi-part message in MIME format.\r\n\r\n";
    $content.= "--".$mime_boundary."\r\n";
    $content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    $content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $content.= $message."\r\n";
    $content.= "--".$mime_boundary."\r\n";
    $content.= "Content-Disposition: attachment;\r\n";
    $content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
    $content.= "Content-Transfer-Encoding: base64\r\n\r\n";
    $content.= $data."\r\n";
    $content.= "--" $mime_boundary "\r\n";

    mail($to$subject$content$headers);
    echo 
    "copied";
    unlink($filename);   //delete the backup file from the server
    ?>

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code is working as it should, I've tested, it must be something in the include file or with the mail server.
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Location
    Bellingham, WA
    Posts
    450
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I appreciate your testing the code for me. I wound up going a different route, using "Zend_Mail" --- super easy for me to add attachments. After doing so I found some posts saying that my host doesn't support mail() as is...so you're actually 100% correct: it WAS the server.

    Thanks again....
    Eric


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •