require_once('../app/Mage.php');
//加载地址信息
$host = "{imap.gmail.com:993/imap/ssl}INBOX";
$user = "";
$pass = "";
$inbox = imap_open($host,$user,$pass) or die('Cannot connect to server: ' . imap_last_error());
//获取邮件number
$ri = 1;
$emails = imap_search($inbox,'ALL');
$email_total_array = array();
if($emails) {
$output = '';
rsort($emails);
//遍历所有邮件。
foreach($emails as $email_number) {
$ri++;
if($ri>6){break;}
//得到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)
{
$message = imap_base64($message);
}
elseif ($coding == 4)
{
$message = imap_qprint($message);
}
elseif ($coding == 5)
{
$message = $message;
}
}
//echo "
^^
".$message."
$$
";
//echo "
";
}
//得到邮件的标头部分:是否已读,发件人,日期,标题。
$header = imap_headerinfo($inbox, $email_number);
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;
echo $fromaddr."##
";
$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);
$SendEmailAddress = $fromaddr;
echo $sender."**
";
//得到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();
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$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 = $email_number."-".$datatimes;
$filename = $datatimes.$file_hz;
$file_dir = "file/".$filename;
$atta_array[] = array("filename"=>$filename,"filealiases"=>$l_filename,"filedir"=>$file_dir);
//win 7下面的文件编码都是gb2312,所以需要转换编码。
// $filename = iconv("UTF-8", "GB2312",$filename);
//echo $l_filename;
$fp = fopen("./".$file_dir, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
}
}
//var_dump($atta_array);
$email_total_array[] = array("attament"=>$atta_array ,"email_number"=>$email_number,"seen"=>$seen,"date"=>$date,"sender"=>$sender,"subject"=>$subject,"message"=>$message,"SendEmailAddress"=>$SendEmailAddress);
//$email_total_array['attament'] = $atta_array;
}
////////加载附件代码结束。
//把信息写入到数据库中。
$app = Mage::app();
foreach($email_total_array as $ea){
$n_sender = $ea['sender'];
if(strstr($n_sender," <")){
$e_sender = substr($n_sender,0,strpos($n_sender," <"));
}else{
$e_sender = $n_sender;
}
$email_list = Mage::getModel("email/list");
$email_list->setEmailMessageNo($ea['email_number']);
$email_list->setSendEmailAddress($ea['SendEmailAddress']);
$email_list->setEmailSender($e_sender);
$email_list->setReceiveEmailAddress($user);
$email_list->setEmailSendTime($ea['date']);
$email_list->setEmailTitle($ea['subject']);
$email_list->setEmailContent($ea['message']);
$email_list->setOrderNo();
$email_list->setCustomerService();
$email_list->setOrderStyle();
$email_list->setLanguage();
$email_list->setOrderTotal();
$email_list->setCaseStatus();
$email_list->save();
$email_id = $email_list->getId();//
$attament = $ea['attament'];
foreach($attament as $aa){
$filename = $aa['filename'];//
$filealiases = $aa['filealiases'];//
$filedir = $aa['filedir'];//
//保存
$email_attachment = Mage::getModel("email/attachment");
$email_attachment->setEmailId($email_id);
$email_attachment->setAttachmentName($filename);
$email_attachment->setAttachmentAliases($filealiases);
$email_attachment->setAttachmentDir($filedir);
$email_attachment->save();
}
}
}
imap_close($inbox);
?>