0
를 사용하여 CSV 인 경우에만 파일의 내용에 배열을 설정
이봐 그래서 ID가 뭘하려는 이메일의 첨부 파일이 extensione의 CSV가있는 경우 예 경우,보고 내 스크립트 수표를 가지고있다 그것의 내용을 배열의 배열에 추가하고 다음 이메일로 이동하면 모든 이메일을 보았을 때 배열의 배열을 별도의 파일로 저장합니다. 내가 고민하는 부분은 첨부 파일이 어떤 파일 형식인지 확인하고 파일 형식이 CSV 인 경우에만 진행되는 부분입니다. 온라인에서해야하는 키워드에 대한 경의를 찾는 것 같습니다. 사람이 올바른 방향으로 날 가리거나 할 내 코드를 적용 할 수있는 경우는 크게 그래서를 heres 내 코드 지금까지 을 주시면 감사하겠습니다 확인하고 도움을 주셔서 감사합니다 당신은 사전에 저를 줄 수 :이메일받은 편지함을 통해 찾고 및 파일 형식이 PHP
<?php include (application.php);
/* connect to gmail with your credentials */
$mailbox = '{imap.gmail.com:993/imap/ssl}INBOX'; /* This bit tells the script where to look and what folder to look for */
$email_server_username = 'YOUR_email_server_username'; /* The email servers username */
$email_server_username_server_password = 'YOUR_email_server_username_server_password'; /* The password asspciated with the above username */
/* connect to sql db with your credentials */
$username = "";
$password = "";
$databasename = "testdb"; /*Set testdb to the database name*/
$databasename = new mysqli("localhost", $username, $password, $databasename) or die("Connection to server failed, please check email_server_username, password and database name"); /*local host should be the ip of the server unless this script is run locally*/
set_time_limit(3000);
/* try to connect to server*/
$inbox = imap_open($mailbox,$email_server_username,$email_server_username_server_password) or die('Failed to connect to Gmail: ' . imap_last_error());
$email_server_usernames = imap_search($inbox, 'FROM "[email protected]"');
/* if any email_server_usernames are found, the script will iterate through each of them */
if($email_server_usernames) {
$count = 1;
/* sorts by newest first */
rsort($email_server_usernames);
/* for every value in email_server_username... */
foreach($email_server_usernames as $email_server_username_number){
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_server_username_number,0);
$message = imap_fetchbody($inbox,$email_server_username_number,2);
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email_server_username_number);
$attachments = array();
/* if any attachments found... */
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_server_username_number, $i+1);
/* Extracts the email contents into usable text, 3 = BASE64 encoding*/
if($structure->parts[$i]->encoding == 3){
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 4 = QUOTED-PRINTABLE encoding */
elseif($structure->parts[$i]->encoding == 4){
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
/* iterate through each attachment and save it */
foreach($attachments as $attachment){
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
if(empty($filename)) $filename = $attachment['filename'];
if(empty($filename)) $filename = time() . ".dat";
$folder = "attachment";
if(!is_dir($folder)){
mkdir($folder);
}
$fp = fopen("./". $folder ."/". $email_server_username_number . "-" . $filename, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
}
}
}
}
/* close the connection to the email_server_username server */
imap_close($inbox);
echo "####################Script ran with no errors####################";
?>