Guest User

Untitled

a guest
Aug 20th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. email sql attachment issue
  2. <?php
  3.  
  4. //Script configuration section.
  5. $D_hostname = 'localhost'; # Database hostname.
  6. $D_username = 'username'; # Database username.
  7. $D_password = 'password'; # Database password.
  8. $D_database = 'database'; # Database name.
  9.  
  10. $E_from = '{BLOG BACKUP} <backup@mywebsite.com>'; # From header.
  11. $E_subject = '[' . date('d-m-Y') . ']: This weeks database backup.'; # Email subject.
  12. $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.
  13. $E_filename = date('d-m-Y') . '_blog_backup.sql'; #Attachment filename.
  14. $E_filetype = 'application/octet-stream'; # Attachment type.
  15. $E_recipients = 'email@mywebsite.com, email@myclient.com'; #Email recipients.
  16.  
  17. //Connect to the database.
  18. $connection = mysql_connect($D_hostname, $D_username, $D_password);
  19.  
  20. //Select the database
  21. mysql_select_db($D_database, $connection);
  22.  
  23. //Set up an empty array.
  24. $tables = array();
  25.  
  26. //Get a list of all the databases tables.
  27. $Q_show_tables = mysql_query('SHOW TABLES');
  28.  
  29. //Add each table to the array.
  30. while($R_show_tables = mysql_fetch_row($Q_show_tables)){
  31. $tables[] = $R_show_tables[0];
  32. }
  33.  
  34. //Get all rows for each table and create a mySQL query.
  35. foreach($tables as $table) {
  36.  
  37. //Get all rows from the table.
  38. $Q_get_rows = mysql_query('SELECT * FROM '.$table);
  39. $C_get_rows = mysql_num_fields($Q_get_rows);
  40.  
  41. //Add a drop clause for the table
  42. $data_row .= 'DROP TABLE ' . $table . ';';
  43.  
  44. //Get create table info.
  45. $Q_create_table = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
  46.  
  47. //Add the create table clause to the query.
  48. $data_row.= "nn" . $Q_create_table[1] . ";nn";
  49.  
  50. //Now gather all of the rows.
  51. for ($int_01 = 0; $int_01 < $C_get_rows; $int_01++) {
  52. while($R_get_rows = mysql_fetch_row($Q_get_rows)) {
  53.  
  54. //Add the insert clause.
  55. $data_row .= 'INSERT INTO ' . $table . ' VALUES (';
  56.  
  57. //For each field write out a row.
  58. for($int_02 = 0; $int_02 < $C_get_rows; $int_02++) {
  59. $R_get_rows[$int_02] = addslashes($R_get_rows[$int_02]);
  60. $R_get_rows[$int_02] = ereg_replace("n","\n",$R_get_rows[$int_02]);
  61. if (isset($R_get_rows[$int_02])) {
  62. $data_row .= '"'.$R_get_rows[$int_02].'"' ;
  63. } else {
  64. $data_row .= '""';
  65. }
  66. if ($int_02 < ($num_fields-1)) {
  67. $data_row .= ',';
  68. }
  69. }
  70. $data_row .= ");n";
  71. }
  72. }
  73. $data_row .="nnn";
  74. }
  75.  
  76. //Split and encode the data.
  77. $data_split = chunk_split(base64_encode($data_row));
  78.  
  79. //Create a unique boundary
  80. $new_boundary = md5(time());
  81.  
  82. //This is your basic header information such as who the email is from and the date it was sent.
  83. $mail_header .= 'From: ' . $E_from . "rn";
  84. $mail_header .= 'Date: ' . date('r') . "rn";
  85.  
  86. //This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
  87. $mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "rnrn";
  88. $mail_header .= 'MIME-Version: 1.0' . "rn";
  89. $mail_header .= 'This is a multi-part message in MIME format' . "rn";
  90. $mail_header .= '--' . $new_boundary . "rn";
  91. $mail_header .= 'Content-Type:text/html; charset="iso-8859-1"' . "rn";
  92. $mail_header .= 'Content-Transfer-Encoding: 7bit' . "rnrn";
  93. $mail_header .= $E_content . "rnrn";
  94.  
  95. //This part of the header is for the attachment and includes the contents of the file, the files name and other information.
  96. $mail_header .= '--' . $new_boundary . "rn";
  97. $mail_header .= 'Content-Type: ' . $E_filetype . '; name="' . $E_filename . '"' . "rn";
  98. $mail_header .= 'Content-Transfer-Encoding: base64' . "rn";
  99. $mail_header .= 'Content-Disposition: attachment; filename="' . $E_filename . '"' . "rnrn";
  100. $mail_header .= $data_split . "rnrn";
  101.  
  102. //This is needed to stop any rendering errors by email clients and signifies the end of the emails header.
  103. $mail_header .= '--' . $new_boundary . '--' . "rn";
  104.  
  105. //This mails out all of the above.
  106. mail($E_recipients, $E_subject, '', $mail_header);
  107.  
  108. ?>
  109.  
  110. //Split and encode the data.
  111. $data_split = chunk_split(base64_encode($data_row));
  112.  
  113. //Create a unique boundary
  114. $new_boundary = '-------'.md5(time()).'-----';
  115.  
  116. //This is your basic header information such as who the email is from and the date it was sent.
  117. $mail_header = 'From: ' . $E_from . "rn";
  118. $mail_header .= 'Date: ' . date('r') . "rn";
  119.  
  120. //This part of the header first defines to the email client that it is a multipart message and adds the emails content/body.
  121. $mail_header .= 'Content-Type: multipart/mixed; boundary="' . $new_boundary . '"' . "rn";
  122. $mail_header .= 'MIME-Version: 1.0' . "rn";
  123.  
  124. // The body (multipart message) starts here:
  125. $mail_body = 'This is a multi-part message in MIME format' . "rn";
  126.  
  127. // HTML content of the email
  128. $mail_body .= "$new_boundaryrn";
  129. $mail_body .= 'Content-Type: text/html; charset="iso-8859-1"' . "rn";
  130. $mail_body .= 'Content-Transfer-Encoding: 7bit' . "rnrn";
  131. $mail_body .= $E_content . "rn";
  132.  
  133. // This part of the body is for the attachment and includes the contents of the file, the files name and other information.
  134. $mail_body .= "$new_boundaryrn";
  135. $mail_body .= 'Content-Type: ' . $E_filetype . '; name="' . $E_filename . '"' . "rn";
  136. $mail_body .= 'Content-Transfer-Encoding: base64' . "rn";
  137. $mail_body .= 'Content-Disposition: attachment; filename="' . $E_filename . '"' . "rnrn";
  138. $mail_body .= $data_split . "rn";
  139.  
  140. //This is needed to stop any rendering errors by email clients and signifies the end of the email's body.
  141. $mail_body .= "$new_boundary--";
  142.  
  143. //This mails out all of the above.
  144. mail($E_recipients, $E_subject, $mail_body, $mail_header);
Add Comment
Please, Sign In to add comment