Guest User

Untitled

a guest
Dec 27th, 2017
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. <?php
  2. /* DISCLAIMER: I, the creator of this script am in no way responsible for any damage or data loss said script may cause directly or indirectly to the used databases, servers, clients and/or devices it may be run on. ANY and ALL responsibility for issues pertaining to the use of this script falls to the person that requested it be created. By using this script you confirm that you have read, fully understand and fully agree to this disclaimer */
  3. /* This script has no input sanitization which means that if someone uploads a csv with any php and possibly SQL commands too in it your server will do whatever those commands tell it to, this could range from adding a malformed entry to deleting everything in the database*/
  4.  
  5. /* ######################################## Required Configuration 'Settings' Start ######################################## */
  6. /* Please only change things between the "" characters unless you know what you're doing */
  7. /* Add your IMAP email server credentials in the next 3 lines */
  8. $mailbox = "{imap.gmail.com:993/imap/ssl}INBOX"; /* This bit tells the script where to look and what folder to look for (you may need to change INBOX to the apropriate folder name) */
  9. $emailAddress = "the_address_to_read_emails_from"; /* The email you wish to process emails from */
  10. $emailPassword = "the_password_for_the_above_address"; /* The password associated with the above username */
  11. /* Add your SQL database credentials in the below 4 lines*/
  12. $databaseHost = "localhost"; /* put the address/hostname of your database here */
  13. $dbUsername = "root"; /* put an admin username here, or at least one with write (s) */
  14. $dbPassword = ""; /* put the corresponding admin password here */
  15. $databaseName = "testdb"; /* The name of the database the script should look at */
  16. /* ######################################### Required Configuration 'Settings' End ######################################### */
  17.  
  18.  
  19.  
  20. /* ######################################## Optional Configuration 'Settings' Start ######################################## */
  21. /* These are here just encase you change your table names */
  22. $cT = "crew_tab"; /* The name of the first table the script should look at */
  23. $eT = "entry_tab"; /* The name of the second table the script should look at */
  24. /* ######################################### Optional Configuration 'Settings' End ######################################### */
  25.  
  26. $fieldSeparator = ",";
  27. $lineSeparator = "n";
  28. $csvFile = 'csv.csv';
  29.  
  30.  
  31.  
  32. if(!file_exists($csvFile)) {
  33. die("No CSV, Skipping to next email");
  34. }
  35. try {
  36. /* try to make a connection to the SQL server*/
  37. $database = new PDO("mysql:host=$databaseHost;dbname=$databaseName", $dbUsername, $dbPassword,
  38. array(
  39. PDO::MYSQL_ATTR_LOCAL_INFILE => true,
  40. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  41. )
  42.  
  43. );
  44. } catch (PDOException $e) {
  45. die("Database connection failed: ".$e->getMessage());
  46. }
  47.  
  48. $affectedRows = $database->exec("
  49. LOAD DATA LOCAL INFILE ".$database->quote($csvFile)." INTO TABLE `$eT`
  50. FIELDS TERMINATED BY ".$database->quote($fieldSeparator)."
  51. LINES TERMINATED BY ".$database->quote($lineSeparator));
  52.  
  53. echo "Loaded a total of $affectedRows records from the csv file into the database.n";
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. /* try to make a connection to the email server*/
  65. $inbox = imap_open($mailbox,$emailAddress,$emailPassword) or die('Failed to connect to Gmail: ' . imap_last_error());
  66. set_time_limit(3000);
  67. $emailAddresss = imap_search($database, 'FROM "abc@gmail.com"');
  68. /* if any emails are found, the script will iterate through each of them */
  69. if($emailAddresss) {
  70. $count = 1;
  71. /* sorts by newest first */
  72. rsort($emailAddresss);
  73. /* for every value in emailAddress... */
  74. foreach($emailAddresss as $emailAddressNumber){
  75. /* get information specific to this email */
  76. $overview = imap_fetch_overview($inbox,$emailAddressNumber,0);
  77. $message = imap_fetchbody($inbox,$emailAddressNumber,2);
  78. /* get mail structure */
  79. $structure = imap_fetchstructure($inbox, $emailAddressNumber);
  80. $attachments = array();
  81. /* if any attachments found... */
  82. if(isset($structure->parts) && count($structure->parts)){
  83. for($i = 0; $i < count($structure->parts); $i++){
  84. $attachments[$i] = array(
  85. 'is_attachment' => false,
  86. 'filename' => '',
  87. 'name' => '',
  88. 'attachment' => '',
  89. );
  90. if($structure->parts[$i]->ifdparameters){
  91. foreach($structure->parts[$i]->dparameters as $object){
  92. if(strtolower($object->attribute) == 'filename')
  93. {
  94. $attachments[$i]['is_attachment'] = true;
  95. $attachments[$i]['filename'] = $object->value;
  96. }
  97. }
  98. }
  99. if($structure->parts[$i]->ifparameters){
  100. foreach($structure->parts[$i]->parameters as $object){
  101. if(strtolower($object->attribute) == 'name'){
  102. $attachments[$i]['is_attachment'] = true;
  103. $attachments[$i]['name'] = $object->value;
  104. }
  105. }
  106. }
  107. if($attachments[$i]['is_attachment']){
  108. $attachments[$i]['attachment'] = imap_fetchbody($inbox, $emailAddressNumber, $i+1);
  109. /* Extracts the email contents into usable text, 3 = BASE64 encoding*/
  110. if($structure->parts[$i]->encoding == 3){
  111. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  112. }
  113. /* 4 = QUOTED-PRINTABLE encoding */
  114. elseif($structure->parts[$i]->encoding == 4){
  115. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  116. }
  117. }
  118. }
  119. }
  120. /* iterate through each attachment and save it */
  121. foreach($attachments as $attachment){
  122. if(($attachment['is_attachment'] == 1) )
  123. {
  124. $filename = $attachment['name'];
  125. if(empty($filename)) $filename = $attachment['filename'];
  126.  
  127. if(empty($filename)) $filename = time() . ".dat";
  128. $folder = "attachment";
  129. if(!is_dir($folder)){
  130. mkdir($folder);
  131. }
  132. $fp = fopen("./". $folder ."/". $emailAddressNumber . "-" . $filename, "w+");
  133. fwrite($fp, $attachment['attachment']);
  134. fclose($fp);
  135. }
  136. }
  137. }
  138. }
  139. /* close the connection to the email_address server */
  140. imap_close($inbox);
  141.  
  142. echo "####################Script ran with no errors####################";
  143. ?>
Add Comment
Please, Sign In to add comment