<?php
/**
 * 每分钟检查一次报错信息，将报错内容以邮件形式通知
 * * * * * * 
 */
$process = `ps aux | grep ReportMail | grep -v grep | grep -v sh | wc -l`;
$process = intval($process);
if ($process > 1)
{
    exit(0);
}

require_once dirname(dirname(__FILE__)) . '/init.php';
require ROOT_PATH . 'libs/phpmailer/class.phpmailer.php';

$reports = $db->select(array('id','uid','username','bid','bookname','chaptername','remark','createdate'))->from(Report::table())->where('ismail=0')->find();
if (empty($reports))
{
    exit(0);
}

foreach ($reports as $report)
{
    $subject = '小说《'.$report['bookname'].'》章节有错['.date('Y-m-d H:i:s', $report['createdate']).']';
    $body = '书名：《'.$report['bookname']."》[".$report['bid']."]\n章节：".$report['chaptername']."\n备注：".$report['remark'];
//    $mail = new PHPMailer();
//    $mail->IsSMTP();
//    $mail->SMTPAuth = true;
//    $mail->CharSet = 'utf-8';
//    $mail->Host = 'ssl://smtp.gmail.com';
//    $mail->Port = 465;
//    $mail->From = 'yzxh24@gmail.com';
//    $mail->FromName = 'yzxh24@gmail.com';
//    $mail->Username = 'yzxh24@gmail.com';
//    $mail->Password = 'wangtao48462662';
//    $mail->AddAddress('419933707@qq.com');
//    $mail->Subject = $subject;
//    $mail->Body = $body;
//    $mail->Send();
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->CharSet = 'utf-8';
    $mail->Host = 'smtp.qq.com';
    $mail->Port = 25;
    $mail->From = '473603089@qq.com';
    $mail->FromName = '473603089@qq.com';
    $mail->Username = '473603089@qq.com';
    $mail->Password = 'zhiku123';
    $mail->AddAddress('419933707@qq.com');
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->Send();
    
    $db->update(Report::table())->value(array('ismail' => 1))->where('id='.$report['id'])->q();
}