Blame view

src/app/tasks/MailTask.php 2.23 KB
ef60cd4d   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  <?php
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  class MailTask extends \Phalcon\CLI\Task
  {
  
      public function mainAction() {
          echo "\nThis is the default task and the default action1 \n";
      }
  
      public function sendmailAction( $params = [] )
      {
          $limit      = $this->filter->sanitize( $params['limit'], 'int' );
  
          $messages   = $this->models->getSendmail()->getMessagesFromQueue( $limit ? $limit : 30 );
  
          if( empty($messages) )
          {
              die( '['.date( 'd.m.Y H:i:s' ).'] nothing to send'."\n\n" );
          }
  
          foreach( $messages as $message )
          {
              echo( '['.date( 'd.m.Y H:i:s' ).'] sending message ID #'.$message['id'].'... ' );
  
              $email_from = 'robot@'.\config::getDomainByID( $message['domain_id'] );
  
              $header =
                  'From: '.$email_from."\n".
                  'MIME-Version: 1.0'."\n".
                  'Content-type: text/html; charset=UTF-8'."\n".
                  'Content-Transfer-Encoding: 8bit'."\n".
                  'X-Mailer: PHP/'.phpversion();
  
              if(
                  mail(
                      $message['email_to'],
                      $message['subject'],
                      $message['message'],
                      $header,
                      '-f '.$email_from
                      )
                )
              {
                  $this->models->getSendmail()->deleteMessageFromQueue( $message['id'] );
  
                  echo( "OK\n" );
              }
              else
              {
                  $message['errors_count']++;
  
                  if( $message['errors_count'] > 2 )
                  {
                      $this->models->getSendmail()->deleteMessageFromQueue( $message['id'] );
                  }
                  else
                  {
                      $this->models->getSendmail()->updateMessageErrorsCount( $message['id'], $message['errors_count'] );
                  }
  
                  echo( "ERROR\n" );
              }
          }
  
          die( "\n" );
      }
  }
  
  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////