Guest User

Untitled

a guest
Jan 20th, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.76 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Author: Aego
  5.  * E-mail: alexkondrashin4@gmail.com
  6.  * Date: 19.09.12
  7.  * Time: 11:14
  8.  */
  9.  
  10. include_once('DaemonClass.php');
  11. include_once('htmlMimeMail.php');
  12.  
  13.  
  14. /**
  15.  * Класс работает с очередью писем из ЛК
  16.  */
  17. class UsoftMailQueueDaemon extends DaemonPHP
  18. {
  19.     const PRO_CONN_STR = 'mysql:host=89.111.180.221;dbname=lk_usoft_ru';
  20.     const DB_USER = '';
  21.     const DB_PASS = '';
  22.     const WAIT_TIME = 60; //Две минуты - время, сколько демон пробудет в sleep
  23.     const MAIL_PER_TRY = 30; //Сколько писем брать за итерацию
  24.  
  25.     /**
  26.      * @var null|PDO
  27.      */
  28.     public $PDO = null;
  29.     /**
  30.      * @var array
  31.      */
  32.     public $mailsPack = array();
  33.  
  34.     public function run()
  35.     {
  36.         $this->connectToDb();
  37.  
  38.  
  39.         while ( true ) {
  40.             $this->getMailsPack();
  41.             $this->processMailsPack();
  42.  
  43.             $slpTime = self::WAIT_TIME;
  44.             if ( date('G') > 19 || date('G') < 9 ) {
  45.                 $slpTime = 60 * 30; //30 mins
  46.             }
  47.  
  48.             sleep(self::WAIT_TIME);
  49.         }
  50.     }
  51.  
  52.     /**
  53.      * Устанавливает соединение с базой
  54.      */
  55.     public function connectToDb()
  56.     {
  57.         $this->PDO = new PDO(self::PRO_CONN_STR, self::DB_USER, self::DB_PASS);
  58.     }
  59.  
  60.     /**
  61.      * Достает из базы набор писем.
  62.      */
  63.     public function getMailsPack()
  64.     {
  65.         $cnt = self::MAIL_PER_TRY;
  66.         if ( date('G') > 19 || date('G') < 9 ) {
  67.             $cnt = 100;
  68.         }
  69.  
  70.         $sql = "SELECT *
  71.                FROM `mail_queue`
  72.                WHERE `is_sended` = 0
  73.                ORDER BY `id` ASC
  74.                LIMIT $cnt";
  75.         $state = $this->PDO->prepare($sql);
  76.         $state->execute();
  77.  
  78.  
  79.  
  80.         if ( $state->rowCount() > 0 ) {
  81.             $ids = array();
  82.  
  83.             while ( $entry = $state->fetch(PDO::FETCH_ASSOC) ) {
  84.                 $ids[] = $entry['id'];
  85.                 $this->mailsPack[] = $entry;
  86.             }
  87.             $this->markEntriesAsSended($ids);
  88.         }
  89.  
  90.     }
  91.  
  92.     /**
  93.      * Помечает письма как отправленные. Делаем это до отправки,
  94.      * чтобы случайно не отправить одно письмо несколько раз.
  95.      *
  96.      * @param array $ids
  97.      */
  98.     public function markEntriesAsSended(array $ids)
  99.     {
  100.         $inString = implode(',', $ids);
  101.         $now = time();
  102.  
  103.         $sql = "UPDATE `mail_queue`
  104.                SET
  105.                `is_sended` = 1,
  106.                `send_time` = $now
  107.                WHERE `id` IN ($inString)";
  108.         $this->PDO->query($sql);
  109.     }
  110.  
  111.     /**
  112.      * Соственно отсылает письма.
  113.      *
  114.      * @return mixed
  115.      */
  116.     public function processMailsPack()
  117.     {
  118.         if ( sizeof($this->mailsPack) <= 0  ) {
  119.             return;
  120.         }
  121.  
  122.         foreach ($this->mailsPack as $email) {
  123.             $mailer = new htmlMimeMail();
  124.             $recipients = unserialize($email['recipients']);
  125.             $mailer->setFrom($email['from']);
  126.             $mailer->setSubject($email['subject']);
  127.  
  128.             if ( $email['content_type'] == 'html' ) {
  129.                 $mailer->setHtml($email['content']);
  130.             } elseif ( $email['content_type'] == 'text' ) {
  131.                 $mailer->setText($email['content']);
  132.             }
  133.            
  134.             if ( $this->prepCcOrBss($email['cc']) ) {
  135.                 $mailer->setCc($this->prepCcOrBss($email['cc']));
  136.             }
  137.            
  138.             if ( $this->prepCcOrBss($email['ncc']) ) {
  139.                 $mailer->setCc($this->prepCcOrBss($email['ncc']));
  140.             }
  141.  
  142.             if ( !is_null($email['file_name']) && !is_null($email['file_data']) ) {
  143.                 $mailer->addAttachment($email['file_data'], $email['file_name']);
  144.             }
  145.  
  146.             foreach ($recipients as $recipient) {
  147.                 $mailer->send(array($recipient));
  148.             }
  149.  
  150.             unset($mailer);
  151.         }
  152.  
  153.         $this->mailsPack = array(); //Unsetting mails list
  154.     }
  155.  
  156.     /**
  157.      * Работает с копией и скрытой копией, которых может не быть
  158.      *
  159.      * @param array $ccOrBcc
  160.      * @return array|bool|mixed|string
  161.      */
  162.     public function prepCcOrBss($ccOrBcc)
  163.     {
  164.         if ( is_null($ccOrBcc) ) {
  165.             return false;
  166.         }
  167.  
  168.         $ccOrBcc = unserialize($ccOrBcc);
  169.  
  170.         if ( sizeof($ccOrBcc) == 1 ) {
  171.             return $ccOrBcc;
  172.         } else {
  173.             return implode(';', $ccOrBcc);
  174.         }
  175.     }
  176. }
  177.  
  178. $daemon = new UsoftMailQueueDaemon();
  179. $daemon->handle($argv);
  180. $daemon->run();
Add Comment
Please, Sign In to add comment