$date = date('dmY');
$direk = "D:/Data/".$date;
mkdir($direk);
set_time_limit(3000);
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'username gmail'; # e.g somebody@gmail.com
$password = 'password gmail';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'ALL');
$max_emails = 2000;
if($emails) {
$count = 1;
rsort($emails);
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']);
}
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
$fp = fopen($direk."/".$email_number."-".$filename,"w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
}
}
if($count++ >= $max_emails) break;
}
}
imap_close($inbox);
echo "Done";
?>
Keterangan
Pada script :
- $date = date('dmY');
- $direk = "D:/Data/".$date;
- mkdir($direk);
Merupakan script untuk membuat folder, yang kemudian akan digunakan untuk tempat menyimpan file downloadan, ini diperlukan apabila file yang kita download ingin dipisahkan berdasarkan tanggal.
- $max_emails = 2000;
Merupakan jumlah maksimal file yang akan kita download.
- $fp = fopen($direk."/".$email_number."-".$filename,"w+");
Ini merupakan script untuk mendownload file, disana ada $direk yaitu folder yang nantinya digunakan untuk tempat downloadan kita, script $direk didapat dari script pling awal, script $email_number merupakan antisipasi apabila ada file yg sama, sehingga file yang akan kita download di kasih number agar bisa terdownload semua
catatan :
Apabila tempat atau folder kita tidak berubah-ubah, bisa gunakan
$fp = fopen("folder_download/".$email_number."-".$filename,"w+"); dan script di atas hapus saja
A Short Description about youself
Any feedback, questions or ideas are always welcome. In case you are posting Code ,then first escape it using Postify and then paste it in the comments
0 comments: