嘿所以我喜欢做的是让我的脚本检查一下,看看电子邮件的附件是否有扩展名csv,如果是的话,将它的内容添加到一个数组数组中,然后移动到下一封电子邮件,然后查看所有电子邮件将数组数组保存为单独的文件。我正在努力的部分是我检查附件是什么文件类型的部分,只有在文件类型是csv时才会进行,我似乎无法对我应该在线进行的关键字发现任何敬意。如果有人能指出我正确的方向或调整我的代码进行检查,那将非常感谢所以我的代码到目前为止,感谢您提前给我的任何帮助:
<?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####################";
?>
if($attachments[$i]["file"]["type"] == "text/csv")
{Result}
这会对你有所帮助。