public function actionIndex() { $model = new Receive(); error_reporting(E_ALL ^ E_WARNING); $obj = new receiveMail('tclrg', 'Luanruitest', '*****@*****.**', 'imap.126.com', 'imap', '993', 'ture'); $obj->connect(); $tot = $obj->getTotalMails(); //查看邮箱是否有新邮件 $old_email_count = Email::find()->count(); for ($i = $tot; $i > $old_email_count; $i--) { $model = new Receive(); $head = $obj->getHeaders($i); $head['subject'] = imap_mime_header_decode($head['subject'])[0]->text; $head['from'] = imap_mime_header_decode($head['from'])[0]->text; $head['fromName'] = imap_mime_header_decode($head['fromName'])[0]->text; $model->subject = $head['subject']; $model->sender = $head['from']; // echo $i.$head['date']."</br>"; // echo $i.strtotime($head['date'])."</br>"; // echo $i.date("Y-m-d H:i:s",strtotime($head['date']))."</br>"; $model->sendtime = date("Y-m-d H:i:s", strtotime($head['date'])); $model->text = $obj->getBody($i); // $model->sendtime=$head['date']; $model->save(); // echo $i.$head['date']; } $obj->close_mailbox(); }
public function actionReceiver() { $model = new Receive(); error_reporting(E_ALL || ~E_WARNING || ~E_NOTICE); $mailaccount = setting::find()->one(); $user = $mailaccount->user; $password = $mailaccount->password; $username = $mailaccount->username; $receivehost = $mailaccount->receivehost; $receiveapply = "imap"; $receiveport = $mailaccount->receiveport; $obj = new receiveMail($user, $password, $username, $receivehost, $receiveapply, $receiveport, 'ture'); $obj->connect(); $tot = $obj->getTotalMails(); //查看邮箱是否有新邮件 $old_email_count = Email::find()->count(); for ($i = $tot; $i > $old_email_count; $i--) { $model = new Receive(); $head = $obj->getHeaders($i); $head['subject'] = imap_mime_header_decode($head['subject'])[0]->text; $head['from'] = imap_mime_header_decode($head['from'])[0]->text; $head['fromName'] = imap_mime_header_decode($head['fromName'])[0]->text; $text = $obj->getBody($i); $encode = mb_detect_encoding($text, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5", "EUC-CN")); if ($encode == "EUC-CN") { $encode = "GB2312"; } if ($encode != "UTF-8") { $text = iconv("{$encode}", "UTF-8", $text); } $model->text = $text; $attachments = $obj->GetAttach($i, "../attachment/receiveattachment"); $attach = ""; if ($attachments != "") { $attach = $attachments; $encode = mb_detect_encoding($attachments, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5", "EUC-CN")); if ($encode == "EUC-CN") { $encode = "GB2312"; } if ($encode != "UTF-8") { $attach = iconv("{$encode}", "UTF-8", $attachments); } } $model->subject = $head['subject']; $model->sender = $head['from']; $model->sendtime = date("Y-m-d H:i:s", strtotime($head['date'])); $model->attachment = $attach; $model->save(); } $obj->close_mailbox(); }