Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Import załącznika z e-maila do bazy danych, prośba o sprawdzenie kodu
ufo1990
post 26.01.2022, 16:43:54
Post #1





Grupa: Zarejestrowani
Postów: 84
Pomógł: 0
Dołączył: 26.07.2019

Ostrzeżenie: (0%)
-----


Co dziennie otrzymuje na skrzynkę e-mail wiadomość z załącznikiem, który do tej pory pobierałem i ręcznie importowałem do programu. Zrobiłem skrypt, który pobiera załącznik ze skrzynki a następnie wpisuje dane zawarte w załączniku do bazy danych. Kod ogólnie działa jednak proszę o informacje czy wszystko jest zrobione tak jak trzeba, trochę nie daje mi spokoju czy dobrze zrobiłem że dwa razy występuje


  1. <?php
  2. $hostname = "{XXXXXXX:993/imap/ssl/novalidate-cert}INBOX";
  3. $username = XXXXXXXXX";
  4. $password = "XXXXXXXXX";
  5.  
  6. $inbox = imap_open($hostname,$username,$password);
  7. $emails = imap_search($inbox,'ALL');
  8.  
  9. if($emails)
  10. {
  11. $count = 1;
  12. foreach($emails as $email_number)
  13. {
  14. $overview = imap_fetch_overview($inbox,$email_number,0);
  15. $message = imap_fetchbody($inbox,$email_number,2);
  16. $structure = imap_fetchstructure($inbox, $email_number);
  17. $attachments = array();
  18.  
  19. if(isset($structure->parts) && count($structure->parts))
  20. {
  21. for($i = 0; $i < count($structure->parts); $i++)
  22. {
  23. $attachments[$i] = array(
  24. 'is_attachment' => false,
  25. 'filename' => '',
  26. 'name' => '',
  27. 'attachment' => ''
  28. );
  29.  
  30. if($structure->parts[$i]->ifdparameters)
  31. {
  32. foreach($structure->parts[$i]->dparameters as $object)
  33. {
  34. if(strtolower($object->attribute) == 'filename')
  35. {
  36. $attachments[$i]['is_attachment'] = true;
  37. $attachments[$i]['filename'] = $object->value;
  38. }
  39. }
  40. }
  41.  
  42. if($structure->parts[$i]->ifparameters)
  43. {
  44. foreach($structure->parts[$i]->parameters as $object)
  45. {
  46. if(strtolower($object->attribute) == 'name')
  47. {
  48. $attachments[$i]['is_attachment'] = true;
  49. $attachments[$i]['name'] = $object->value;
  50. }
  51. }
  52. }
  53.  
  54. if($attachments[$i]['is_attachment'])
  55. {
  56. $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  57.  
  58. if($structure->parts[$i]->encoding == 3)
  59. {
  60. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  61. }
  62. else
  63. {
  64. if($structure->parts[$i]->encoding == 4)
  65. {
  66. $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  67. }
  68. }
  69. }
  70. }
  71. }
  72.  
  73. require_once "connect.php";
  74. $connect = @new mysqli($host, $db_user, $db_password, $db_name);
  75.  
  76. if($connect->connect_error)
  77. {
  78. throw new Exception(mysqli_connect_errno());
  79. }
  80.  
  81. foreach($attachments as $attachment)
  82. {
  83. if($attachment['is_attachment'] == 1)
  84. {
  85. $filename = $attachment['name'];
  86. $filename_save = fopen($filename, "w+");
  87. $file = fwrite($filename_save, $attachment['attachment']);
  88.  
  89. if(isset($file))
  90. {
  91. ini_set('auto_detect_line_endings', TRUE);
  92. $filename_open = fopen($filename, "r");
  93.  
  94. while(($emapData = fgetcsv($filename_open, 10000, " ")) !== FALSE)
  95. {
  96. substr($emapData[1], 0, 4) .'-'. substr($emapData[1], 4,2) .'-'. substr($emapData[1],6,2);
  97. if($connect->query("INSERT into employees (`id_employee`, `sap_number`, `startdate`, `name`,`lastname`,`status`,`anonymization`) values(null,'$emapData[0]','$emapData[1]','$emapData[3]','$emapData[2]','','0')"))
  98. {
  99. unlink($filename);
  100. header('Location: employees.php');
  101. }
  102. else
  103. {
  104. throw new Exception(mysqli_connect_errno());
  105. }
  106. }
  107. fclose($filename_open);
  108. }
  109. fclose($filename_save);
  110. }
  111. }
  112. }
  113. }
  114. imap_delete($inbox,'1:*');
  115. imap_expunge($inbox);
  116. imap_close($inbox);
  117. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 17.05.2024 - 11:47