<?php
/****需要配置的:
*$filebasedir:附件相对根目录。
*$key:安全性验证,必须一致,否则返回错误。
*$host:IMAP加载邮件的host
*$user:邮箱账号
*$pass:邮箱密码
*xf_num:每次接受邮件的最大值。
*/
/*
$key = "434343";
$getkey = "434343";
$filebasedir = "4343";
$imap_host = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$user = "4343";
$pass = "4343";
$xf_num = 22; //接收邮件的限制量
*/
$key = "45454";
$getkey = $_POST['key'];
$filebasedir = $_POST['filebasedir'];
$imap_host = $_POST['imap_host'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$xf_num = $_POST['xf_num']+0;
if($key == $getkey){
$inbox = imap_open($imap_host,$user,$pass) or die('Cannot connect to server: ' . imap_last_error());
//获取邮件number
$ri = 1;
//
//$emails = imap_search($inbox,'UNSEEN');
$after_date = date ( "d M Y", strToTime("2013-11-23 00:00:00") );
$emails = imap_search($inbox,'UNSEEN');
$email_total_array = array();
if($emails) {
$output = '';
rsort($emails);
//遍历所有邮件。
foreach($emails as $email_number) {
if($ri>$xf_num){break;}
$ri++;
//得到structure、
$email_array = array();
$structure = imap_fetchstructure($inbox, $email_number);
$message = "";
$tt = 0;
//得到message
if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) {
$part = $structure->parts[1];
$message = imap_fetchbody($inbox,$email_number,1.2);
if($message == ""){
$message = imap_fetchbody($inbox,$email_number,2);
}else{
//$tt = 1;
}
$coding = $part->encoding;
if($tt == 0){
if ($coding == 0)
{
$message = $message;
}
elseif ($coding == 1)
{
$message = $message;
}
elseif ($coding == 2)
{
$message = $message;
}
elseif ($coding == 3)
{
$mess = $message;
$message = imap_base64($message);
if(($message=="")||(!($message))){
$message = $mess;
}
//echo "&&&&".$message;
}
elseif ($coding == 4)
{
$message = imap_qprint($message);
}
elseif ($coding == 5)
{
$message = $message;
}
}
//mix
$struct = $structure;
if (strtolower($struct->subtype)=="alternative" || strtolower($struct->subtype)=="mixed"){
for($i=0; $i< count($struct->parts); $i++) {
$type[$i]=$struct->parts[$i]->subtype;
}
if (strtolower($type[0])=="html")
{
$text=imap_fetchbody($inbox, $email_number, "1");
if ($struct->parts[0]->encoding ==3) {
//echo $text;
$message = imap_base64($text);
//echo $message;
$html = 1;
}
else if ($struct->parts[0]->encoding == 4 ) {
$message = imap_qprint($text);
$html = 1;
//echo $text;
}
else {
$message = $text;
$html = 1;
}
}
elseif (strtolower($type[1])=="html")
{
$text=imap_fetchbody($inbox, $email_number, "2");
if ($struct->parts[1]->encoding ==3) {
$message = imap_base64($text);
$html = 1;
}
else if ($struct->parts[1]->encoding == 4 ) {
$message = imap_qprint($text);
$html = 1;
}
else {
$message = $text;
$html = 1;
}
}
elseif (strtolower($type[0])=="plain" || strtolower($type[1])=="plain")
{
for ($i=0;$i<count($type);$i++)
{
$text=imap_fetchbody($inbox, $email_number, $i+1);
if ($message=='' && $text && strtolower($struct->parts[$i]->subtype)=="plain")
{
$message=$text;
}
}
}
}
//
}else if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[0])) {
$part = $structure->parts[0];
$message = imap_fetchbody($inbox,$email_number,1.2);
if($message == ""){
$message = imap_fetchbody($inbox,$email_number,2);
}else{
//$tt = 1;
}
if($message == ""){
$message = imap_fetchbody($inbox,$email_number,1);
}
$coding = $part->encoding;
if ($coding == 0)
{
$message = $message;
}
elseif ($coding == 1)
{
$message = $message;
}
elseif ($coding == 2)
{
$message = $message;
}
elseif ($coding == 3)
{
$mess = $message;
$message = imap_base64($message);
if(($message=="")||(!($message))){
$message = $mess;
}
//echo "&&&&".$message;
}
elseif ($coding == 4)
{
$message = imap_qprint($message);
}
elseif ($coding == 5)
{
$message = $message;
}
}else{
$coding = $structure->encoding;
$message = imap_fetchbody($inbox,$email_number,1);
if ($coding == 0)
{
$message = $message;
}
elseif ($coding == 1)
{
$message = $message;
}
elseif ($coding == 2)
{
$message = $message;
}
elseif ($coding == 3)
{ $mess = $message;
$message = imap_base64($message);
if(($message=="")||(!($message))){
$message = $mess;
}
//echo "&&&&".$message;
}
elseif ($coding == 4)
{
$message = imap_qprint($message);
}
elseif ($coding == 5)
{
$message = $message;
}
}
//得到发件人邮箱地址。
$header = imap_headerinfo($inbox, $email_number);
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;
//echo $fromaddr."##<br>";
//得到邮件的标头部分:是否已读,发件人,日期,标题。
$overview = imap_fetch_overview($inbox,$email_number,0);
$sender = imap_utf8($overview[0]->from);
$seen = $overview[0]->seen ? 'read' : 'unread';
$date = $overview[0]->date;
$subject = imap_utf8($overview[0]->subject);
if($structure->subtype == "MIXED"){
$text = $overview[0]->subject;
$elements = imap_mime_header_decode($text);
$subject = iconv($elements[0]->charset, "UTF-8", $elements[0]->text);
}
$SendEmailAddress = $fromaddr;
//echo $sender."**<br/>";
//得到message的字符编码。
if($tt == 0){
$encodedd = mb_detect_encoding($message, array('UTF-8','US-ASCII','GB2312', 'ISO-8859-1','ISO-8859-2','ISO-8859-4','ISO-8859-5','ISO-8859-6','ISO-8859-7','ISO-8859-8','ISO-8859-9','ISO-8859-10','ISO-2022-JP','ISO-2022-KR'));
//进行转码。
if($encodedd == "EUC-CN"){
$message= iconv("GB2312", "UTF-8", $message);
}else if($encodedd == "ISO-8859-1"){
$message= iconv("ISO-8859-1", "UTF-8", $message);
}else if($encodedd == "ISO-8859-2"){
$message= iconv("ISO-8859-2", "UTF-8", $message);
}else if($encodedd == "ISO-8859-4"){
$message= iconv("ISO-8859-4", "UTF-8", $message);
}else if($encodedd == "ISO-8859-5"){
$message= iconv("ISO-8859-5", "UTF-8", $message);
}else if($encodedd == "ISO-8859-6"){
$message= iconv("ISO-8859-6", "UTF-8", $message);
}else if($encodedd == "ISO-8859-7"){
$message= iconv("ISO-8859-7", "UTF-8", $message);
}else if($encodedd == "ISO-8859-8"){
$message= iconv("ISO-8859-8", "UTF-8", $message);
}else if($encodedd == "ISO-8859-9"){
$message= iconv("ISO-8859-9", "UTF-8", $message);
}else if($encodedd == "ISO-8859-10"){
$message= iconv("ISO-8859-10", "UTF-8", $message);
}else if($encodedd == "ISO-2022-JP"){
$message= iconv("ISO-2022-JP", "UTF-8", $message);
}else if($encodedd == "ISO-2022-KR"){
$message= iconv("ISO-2022-KR", "UTF-8", $message);
}else{
$message= $message;
}
}else{
$message= $message;
}
//邮箱信息保存到数组。
//加载附件
$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'] = imap_utf8($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'] = imap_utf8($object->value);
}
}
}
if($attachments[$i]['is_attachment'])
{
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 encoding */
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
/* iterate through each attachment and save it */
$atta_array = array();
$kiol = 0;
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$kiol++;
$filename = $attachment['name'];
if(empty($filename)) $filename = $attachment['filename'];
if(empty($filename)) $filename = time() . ".dat";
/* prefix the email number to the filename in case two emails
* have the attachment with the same file name.
*/
$l_filename = $filename;
$file_hz = strrev(substr(strrev($filename),0,4));
$datatimes = date('Y-m-d-G-i-s', time());
//$datatimes = str_replace(" ","-",$datatimes);
//$datatimes = $datatimes;
$user_email = str_replace("@","-",$user);
$user_email = str_replace(".","-",$user_email);
$filename = $user_email."-".$email_number."-".$datatimes.$kiol.$file_hz;
$file_dir = "var/email/".$filename;
$atta_array[] = array("filename"=>$filename,"filealiases"=>$l_filename,"filedir"=>$file_dir,$attachment['attachment']);
//win 7下面的文件编码都是gb2312,所以需要转换编码。
// $filename = iconv("UTF-8", "GB2312",$filename);
//echo $l_filename;
$fp = fopen("./".$file_dir, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
}
}
$email_total_array[] = array("attament"=>$atta_array ,"email_number"=>$email_number,"seen"=>$seen,"date"=>$date,"sender"=>$sender,"subject"=>$subject,"message"=>$message,"SendEmailAddress"=>$SendEmailAddress);
}
//输出json格式数组。
$email_total_array = array_reverse($email_total_array);
echo json_encode(array("status"=>"success","data"=>$email_total_array,"user"=>$user,"filebasedir"=>$filebasedir));
}else{
echo json_encode(array("status"=>"no email message"));
exit;
}
imap_close($inbox);
}else{
echo json_encode(array("status"=>"key error"));
}
?>
gmail,imap 接收邮件
最新推荐文章于 2022-10-28 16:14:26 发布