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
<?php
$hostname = "{XXXXXXX:993/imap/ssl/novalidate-cert}INBOX";
$username = XXXXXXXXX";
$password = "XXXXXXXXX";
$inbox = imap_open($hostname,$username,$password);
$emails = imap_search($inbox,'ALL');
if($emails)
{
$count = 1;
foreach($emails as $email_number)
{
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
$structure = imap_fetchstructure($inbox, $email_number);
$attachments = array();
if(isset($structure->parts) && count($structure->parts))
{
for($i = 0; $i < count($structure->parts); $i++)
{
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters)
{
foreach($structure->parts[$i]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters)
{
foreach($structure->parts[$i]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment'])
{
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
else
{
if($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
}
require_once "connect.php";
$connect = @new mysqli($host, $db_user, $db_password, $db_name);
if($connect->connect_error)
{
throw new Exception(mysqli_connect_errno());
}
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
$filename_save = fopen($filename, "w+");
$file = fwrite($filename_save, $attachment['attachment']);
if(isset($file))
{
ini_set('auto_detect_line_endings', TRUE);
$filename_open = fopen($filename, "r");
while(($emapData = fgetcsv($filename_open, 10000, " ")) !== FALSE)
{
substr($emapData[1], 0, 4) .'-'. substr($emapData[1], 4,2) .'-'. substr($emapData[1],6,2);
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')"))
{
unlink($filename);
header('Location: employees.php');
}
else
{
throw new Exception(mysqli_connect_errno());
}
}
fclose($filename_open);
}
fclose($filename_save);
}
}
}
}
imap_delete($inbox,'1:*');
imap_expunge($inbox);
imap_close($inbox);
?>