Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- email sql attachment issue
- <?php
- //Script configuration section.
- $D_hostname = 'localhost'; # Database hostname.
- $D_username = 'username'; # Database username.
- $D_password = 'password'; # Database password.
- $D_database = 'database'; # Database name.
- $E_from = '{BLOG BACKUP} <backup@mywebsite.com>'; # From header.
- $E_subject = '[' . date('d-m-Y') . ']: This weeks database backup.'; # Email subject.
- $E_content = 'This weeks database backup has been created, please find it attached to this email. Remember to save a copy and retain this email too!<br />'; #Email content.
- $E_filename = date('d-m-Y') . '_blog_backup.sql'; #Attachment filename.
- $E_filetype = 'application/octet-stream'; # Attachment type.
- $E_recipients = 'email@mywebsite.com, email@myclient.com'; #Email recipients.
- //Connect to the database.
- $connection = mysql_connect($D_hostname, $D_username, $D_password);
- //Select the database
- mysql_select_db($D_database, $connection);
- //Set up an empty array.
- $tables = array();
- //Get a list of all the databases tables.
- $Q_show_tables = mysql_query('SHOW TABLES');
- //Add each table to the array.
- while($R_show_tables = mysql_fetch_row($Q_show_tables)){
- $tables[] = $R_show_tables[0];
- }
- //Get all rows for each table and create a mySQL query.
- foreach($tables as $table) {
- //Get all rows from the table.
- $Q_get_rows = mysql_query('SELECT * FROM '.$table);
- $C_get_rows = mysql_num_fields($Q_get_rows);
- //Add a drop clause for the table
- $data_row .= 'DROP TABLE ' . $table . ';';
- //Get create table info.
- $Q_create_table = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
- //Add the create table clause to the query.
- $data_row.= "nn" . $Q_create_table[1] . ";nn";
- //Now gather all of the rows.
- for ($int_01 = 0; $int_01 < $C_get_rows; $int_01++) {
- while($R_get_rows = mysql_fetch_row($Q_get_rows)) {
- //Add the insert clause.
- $data_row .= 'INSERT INTO ' . $table . ' VALUES (';
- //For each field write out a row.
- for($int_02 = 0; $int_02 < $C_get_rows; $int_02++) {
- $R_get_rows[$int_02] = addslashes($R_get_rows[$int_02]);
- $R_get_rows[$int_02] = ereg_replace("n","\n",$R_get_rows[$int_02]);
- if (isset($R_get_rows[$int_02])) {
- $data_row .= '"'.$R_get_rows[$int_02].'"' ;
- } else {
- $data_row .= '""';
- }
- if ($int_02 < ($num_fields-1)) {
- $data_row .= ',';
- }
- }
- $data_row .= ");n";
- }
- }
- $data_row .="nnn";
- }
- //Split and encode the data.
- $data_split = chunk_split(base64_encode($data_row));
- //Create a unique boundary
- $new_boundary = md5(time());
- //This is your basic header information such as who the email is from and the date it was sent.
- $mail_header .= 'From: ' . $E_from . "rn";
- $mail_header .= 'Date: ' . date('r') . "rn";
- //This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
- $mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "rnrn";
- $mail_header .= 'MIME-Version: 1.0' . "rn";
- $mail_header .= 'This is a multi-part message in MIME format' . "rn";
- $mail_header .= '--' . $new_boundary . "rn";
- $mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "rn";
- $mail_header .= 'Content-Transfer-Encoding: 7bit' . "rnrn";
- $mail_header .= $E_content . "rnrn";
- //This part of the header is for the attachment and includes the contents of the file, the files name and other information.
- $mail_header .= '--' . $new_boundary . "rn";
- $mail_header .= 'Content-Type: ' . $E_filetype . '; name="' . $E_filename . '"' . "rn";
- $mail_header .= 'Content-Transfer-Encoding: base64' . "rn";
- $mail_header .= 'Content-Disposition: attachment; filename="' . $E_filename . '"' . "rnrn";
- $mail_header .= $data_split . "rnrn";
- //This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
- $mail_header .= '--' . $new_boundary . '--' . "rn";
- //This mails out all of the above.
- mail($E_recipients, $E_subject, '', $mail_header);
- ?>
- //Split and encode the data.
- $data_split = chunk_split(base64_encode($data_row));
- //Create a unique boundary
- $new_boundary = '-------'.md5(time()).'-----';
- //This is your basic header information such as who the email is from and the date it was sent.
- $mail_header = 'From: ' . $E_from . "rn";
- $mail_header .= 'Date: ' . date('r') . "rn";
- //This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
- $mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "rn";
- $mail_header .= 'MIME-Version: 1.0' . "rn";
- // The body (multipart message) starts here:
- $mail_body = 'This is a multi-part message in MIME format' . "rn";
- // HTML content of the email
- $mail_body .= "$new_boundaryrn";
- $mail_body .= 'Content-Type: text/html; charset="iso-8859-1"' . "rn";
- $mail_body .= 'Content-Transfer-Encoding: 7bit' . "rnrn";
- $mail_body .= $E_content . "rn";
- // This part of the body is for the attachment and includes the contents of the file, the files name and other information.
- $mail_body .= "$new_boundaryrn";
- $mail_body .= 'Content-Type: ' . $E_filetype . '; name="' . $E_filename . '"' . "rn";
- $mail_body .= 'Content-Transfer-Encoding: base64' . "rn";
- $mail_body .= 'Content-Disposition: attachment; filename="' . $E_filename . '"' . "rnrn";
- $mail_body .= $data_split . "rn";
- //This is needed to stop any rendering errors by email clients and signifies the end of the email's body.
- $mail_body .= "$new_boundary--";
- //This mails out all of the above.
- mail($E_recipients, $E_subject, $mail_body, $mail_header);
Add Comment
Please, Sign In to add comment