3
使用PHP IMAP功能我可以閱讀電子郵件,但我需要自動運行或手動運行以下載郵件,我如何下載所有帶或不帶附件的電子郵件並將其保存到主要網絡的郵件文件夾名稱中的本地驅動器或服務器的Gmail,雅虎,Hotmail,AOL)。如何使用PHP IMAP將所有帶附件的郵件下載到服務器?
使用PHP IMAP功能我可以閱讀電子郵件,但我需要自動運行或手動運行以下載郵件,我如何下載所有帶或不帶附件的電子郵件並將其保存到主要網絡的郵件文件夾名稱中的本地驅動器或服務器的Gmail,雅虎,Hotmail,AOL)。如何使用PHP IMAP將所有帶附件的郵件下載到服務器?
請嘗試此代碼以獲取目錄中的電子郵件和商店附件。
您也可以使用imap_delete和imap_expunge從郵箱中提取郵件後刪除郵件。
在下面的代碼中設置您的網站名稱,郵箱用戶名,密碼和存儲附件的路徑。
if($mbox = imap_open("{yoursitename.com:110/pop3/notls}INBOX", "username of mailbox", "password of mailbox")){
$path = "set path here for where the attachments are store";
$check = imap_mailboxmsginfo($mbox);
function getmsg($mbox,$mid) {
global $charset,$htmlmsg,$plainmsg,$attachments,$from,$to,$subj,$timages,$path;
$htmlmsg = $plainmsg = $charset = '';
$attachments = array();
// HEADER
$h = imap_headerinfo($mbox,$mid);
// add code here to get date, from, to, cc, subject...
$date = $h->date;
$from = $h->fromaddress;
$to = $h->toaddress;
$subj = htmlspecialchars($h->Subject);
// BODY
$s = imap_fetchstructure($mbox,$mid);
if (!$s->parts) // simple
getpart($mbox,$mid,$s,0); // pass 0 as part-number
else { // multipart: cycle through each part
foreach ($s->parts as $partno0=>$p)
getpart($mbox,$mid,$p,$partno0+1);
}
}
function getpart($mbox,$mid,$p,$partno) {
// $partno = '1', '2', '2.1', '2.1.3', etc for multipart, 0 if simple
global $htmlmsg,$plainmsg,$charset,$attachments,$partid,$last_mail_id,$patterns,$pic,$newstr,$c,$ok,$timages,$subj,$path;
$patterns = array();
$pic = array();
$image=array();
$data = ($partno) ? imap_fetchbody($mbox,$mid,$partno) : imap_body($mbox,$mid); // simple
if ($p->encoding==4)
$data = quoted_printable_decode($data);
else if ($p->encoding==3)
$data = base64_decode($data);
// PARAMETERS // get all parameters, like charset, filenames of attachments, etc.
$params = array();
if ($p->parameters)
foreach ($p->parameters as $x)
$params[strtolower($x->attribute)] = $x->value;
if ($p->dparameters)
foreach ($p->dparameters as $x)
$params[strtolower($x->attribute)] = $x->value;
// ATTACHMENT // Any part with a filename is an attachment,
// so an attached text file (type 0) is not mistaken as the message.
if ($params['filename'] || $params['name']) {
$partid = htmlentities($p->id,ENT_QUOTES);
// filename may be given as 'Filename' or 'Name' or both
$filename = ($params['filename'])? $params['filename'] : $params['name'];
// filename may be encoded, so see imap_mime_header_decode()
$attachments[$filename] = $data; // this is a problem if two files have same name
//store id and filename in array
$image[$key] = $filename;
}
//save the attachments in the directory
foreach($attachments as $key => $val){
$fname = $key;
$fp = fopen("$path/$fname","w");
fwrite($fp, $val);
fclose($fp);
}
// TEXT
if ($p->type==0 && $data) {
// Messages may be split in different parts because of inline attachments, // so append parts together with blank row.
if (strtolower($p->subtype)=='plain')
$plainmsg .= trim($data)."\n\n";
else
//preg_match_all('/<img[^>]+>/i',$data, $result);
$htmlmsg .= $data."<br><br>";
$charset = $params['charset']; // assume all parts are same charset
}
// There are no PHP functions to parse embedded messages, so this just appends the raw source to the main message.
else if ($p->type==2 && $data) {
$plainmsg .= $data."\n\n";
}
// SUBPART RECURSION
if ($p->parts) {
foreach ($p->parts as $partno0=>$p2)
getpart($mbox,$mid,$p2,$partno.'.'.($partno0+1)); // 1.2, 1.2.1, etc.
}
}
$attachments = array();
$num_msg = imap_num_msg($mbox);
if($num_msg>0) {
getmsg($mbox,1);
}else {
echo "Sorry!...No Messages in MailBox...<br>";
}
//imap_delete and imap_expunge are used to delete the mail after fetching....Uncomment it if you want to delete the mail from mailbox
//imap_delete($mbox,1);
//imap_expunge($mbox);
imap_close($mbox);
}else { exit ("Can't connect: " . imap_last_error() ."\n"); echo "FAIL!\n"; };
雅感謝它的工作,再次感謝 – senthilbp 2012-04-24 08:59:20
閱讀:http://www.electrictoolbox.com/php-imap-download-email/ – Raptor 2012-04-24 06:21:23
@shivan - 感謝,但其基本只查看郵件,我需要通過下載並運行循環所有郵件在我的本地服務器 – senthilbp 2012-04-24 06:22:19