CakePHPで不達メールの一覧を取得するモデルを作ってみた

お問い合わせフォームからメールを送信することもままあると思いますが、
不達メールの問題はついて回るかと思います。
そこでPOP3でメールサーバに接続し不達メールの一覧を取得するモデルを作ってみました。
[PHP]
/**
* 不達メールモデル
*/
class BounceMail extends AppModel {
var $useTable = false;
var $name = ‘BounceMail’;

var $mboxconnstr = “{メールサーバーホスト名:110/pop3}INBOX”;
var $user = “ユーザー名”;
var $passwd = “パスワード”;
var $subject = “Undelivered Mail Returned to Sender”;

function find($conditions = null, $extra = array()) {
$records = array();
$mbox = imap_open($this->mboxconnstr, $this->user, $this->passwd);
if ($mbox == false) {
return $records;
}
$mail_check = imap_check($mbox);
for($i=1;$i<=$mail_check->Nmsgs;$i++) {
$headinfo = imap_headerinfo($mbox, $i);
$bodyinfo = imap_body($mbox, $i);
if ($headinfo->Subject == $this->subject) {
$rtn = preg_match(“/<.+@.+?>/”, $bodyinfo, $rslt);
if ($rtn == true) {
if (is_array($rslt)) {
foreach($rslt as $key=>$val) {
if (!empty($conditions)) {
foreach($conditions as $condition){
// 日付範囲指定の条件
if (isset($condition[‘dateFrom’])) {
if (date(“Ymd”, strtotime($headinfo->date)) < date("Ymd", strtotime($condition['dateFrom']))) { continue 2; } } if (isset($condition['dateTo'])) { if (date("Ymd", strtotime($headinfo->date)) > date(“Ymd”, strtotime($condition[‘dateTo’]))) {
continue 2;
}
}
}
}
$records[$i][“datetime”] = date(“Y-n-j H:i:s”, strtotime($headinfo->date));
$records[$i][“mail”] = rtrim(ltrim($val, “<"), ">“);
break;
}
}
}
}
}
imap_close($mbox);
return $records;
}

function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$rslt = $this->find($conditions, $extra);
$count = count($rslt);
return count($rslt);
}

function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$rslt = $this->find($conditions, $extra);
$records = array();
$end = $page * $limit;
$start = $end – $limit + 1;
$i = 0;
foreach($rslt as $row){
if (++$i >= $start && $i <= $end) { $records[] = $row; } } return $records; } } [/PHP]