This post combines items that were gathered from a few sites. The problem we were trying to solve is emailing a MySQL database backup using PHP PEAR through Google Apps. This script could also be easily modified to send through gmail. The file includes some hints and links to assist you whenever/wherever possible. Thank you Peter for providing the starting point for this script.
// enhanced from original provided @ http://www.theblog.ca/mysql-email-backup
// install the following pear modules
// pear install mail_mime
// pear install mail
// pear install net_smtp
// see this entry for more information about pear mail
// http://www.phpmaniac.net/wiki/index.php/Pear_Mail
// mysql database connection information
$dbhost = "localhost"; // usually localhost
$dbuser = "mydbuser";
$dbpass = "mydbpass";
$dbname = "mydbname";
// create the mysql backup file
$backupfile = $dbname . "_" . date("Y_m_d") . '.sql';
system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile");
// includes
include('Mail.php');
include('Mail/mime.php');
// smtp settings
$params["host"] = "smtp.gmail.com";
$params["port"] = "587";
$params["auth"] = true;
$params["username"] = "myusername@mydomain.com";
$params["password"] = "mypassword";
// email settings
$cr = "\n";
$recipients[] = "Recipient 1
$recipients[] = "Recipient 2
$sendfrom = "sender
$sendsubject = "Backup - MySQL - mydomain.com";
$text = "Database backup performed on " . date("m/d/Y @ H:i:s", time()) . $cr;
$text .= "File name: " . $backupfile;
// *************************************
// do not edit below this line
// *************************************
$message = new Mail_mime();
$message->setTXTBody($text);
$message->AddAttachment($backupfile);
$body = $message->get();
$extraheaders = array("From"=>$sendfrom, "Subject"=>$sendsubject);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("smtp", $params);
$mail->send($recipients, $headers, $body);
// Delete the file from your server
unlink($backupfile);
?>
Tuesday, July 6, 2010
Send MySQL Backup thru Google Apps Email using PHP
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment