Blame view

common/components/mail/MailAttachmentsSaver.php 4.04 KB
02359b64   Mihail   add mail parser a...
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 01.09.2015
   * Time: 10:53
   */
  
7f6a9301   Mihail   add mails classes
9
  namespace common\components\mail;
02359b64   Mihail   add mail parser a...
10
11
  
  
8e7f5c9b   Mihail   add MailAttachmen...
12
13
14
15
  use common\components\CustomVarDamp;
  
  class MailAttachmentsSaver
  {
7f6a9301   Mihail   add mails classes
16
17
18
      protected $mail_reader;
      protected $massage_type;
      public $file_name_prefix;
8e7f5c9b   Mihail   add MailAttachmen...
19
  
7f6a9301   Mihail   add mails classes
20
      public function __construct( MailReader $mail_reader, $massage_type )
8e7f5c9b   Mihail   add MailAttachmen...
21
      {
7f6a9301   Mihail   add mails classes
22
23
24
          $this->mail_reader = $mail_reader;
          $this->massage_type = $massage_type;
  
8e7f5c9b   Mihail   add MailAttachmen...
25
      }
02359b64   Mihail   add mail parser a...
26
  
02359b64   Mihail   add mail parser a...
27
  
7f6a9301   Mihail   add mails classes
28
      public function saveAttachmentsTo( $destination )
8e7f5c9b   Mihail   add MailAttachmen...
29
      {
02359b64   Mihail   add mail parser a...
30
  
7f6a9301   Mihail   add mails classes
31
          $emails = $this->mail_reader->getEmails( $this->massage_type );
02359b64   Mihail   add mail parser a...
32
33
  
          /* if emails are returned, cycle through each... */
7f6a9301   Mihail   add mails classes
34
  
8e7f5c9b   Mihail   add MailAttachmen...
35
          if ($emails) {
02359b64   Mihail   add mail parser a...
36
37
38
39
40
41
  
              /* begin output var */
              $output = '';
  
              /* put the newest emails on top */
              rsort($emails);
7f6a9301   Mihail   add mails classes
42
             // CustomVarDamp::dump($emails);
02359b64   Mihail   add mail parser a...
43
44
              foreach ($emails as $email_number) {
  
7f6a9301   Mihail   add mails classes
45
                  $structure = $this->mail_reader->getCurrentEmailStructure($email_number);
02359b64   Mihail   add mail parser a...
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
72
73
                  $attachments = array();
                  if (isset($structure->parts) && count($structure->parts)) {
                      for ($i = 0; $i < count($structure->parts); $i++) {
                          $attachments[$i] = array(
                              'is_attachment' => false,
                              'filename' => '',
                              'name' => '',
                              'attachment' => '');
  
                          if ($structure->parts[$i]->ifdparameters) {
                              foreach ($structure->parts[$i]->dparameters as $object) {
                                  if (strtolower($object->attribute) == 'filename') {
                                      $attachments[$i]['is_attachment'] = true;
                                      $attachments[$i]['filename'] = $object->value;
                                  }
                              }
                          }
  
                          if ($structure->parts[$i]->ifparameters) {
                              foreach ($structure->parts[$i]->parameters as $object) {
                                  if (strtolower($object->attribute) == 'name') {
                                      $attachments[$i]['is_attachment'] = true;
                                      $attachments[$i]['name'] = $object->value;
                                  }
                              }
                          }
  
                          if ($attachments[$i]['is_attachment']) {
7f6a9301   Mihail   add mails classes
74
                              $attachments[$i]['attachment'] = imap_fetchbody($this->mail_reader->connection, $email_number, $i + 1);
02359b64   Mihail   add mail parser a...
75
76
77
78
79
80
                              if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
                                  $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                              } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
                                  $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                              }
                          }
8e7f5c9b   Mihail   add MailAttachmen...
81
                      }
02359b64   Mihail   add mail parser a...
82
  
8e7f5c9b   Mihail   add MailAttachmen...
83
                      if (count($attachments) != 0) {
02359b64   Mihail   add mail parser a...
84
  
7f6a9301   Mihail   add mails classes
85
86
                                  foreach($attachments as $key => &$val){
                                      if ($val['is_attachment'] == 1) {
02359b64   Mihail   add mail parser a...
87
  
7f6a9301   Mihail   add mails classes
88
89
90
91
92
93
                                          if( isset($this->file_name_prefix) ){
                                              $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']);
                                          }else{
                                              $name = $destination . '/' . mb_decode_mimeheader($val['name']);
                                          }
                                      mb_internal_encoding("UTF-8");
02359b64   Mihail   add mail parser a...
94
  
7f6a9301   Mihail   add mails classes
95
96
97
                                      //file_put_contents(\Yii::getAlias('@temp_upload') . '/' . $name, $val['attachment']);
                                      file_put_contents( $name, $val['attachment'] );
                                  }
02359b64   Mihail   add mail parser a...
98
  
8e7f5c9b   Mihail   add MailAttachmen...
99
                              }
02359b64   Mihail   add mail parser a...
100
                          }
8e7f5c9b   Mihail   add MailAttachmen...
101
  
02359b64   Mihail   add mail parser a...
102
103
104
105
                      }
  
                  }
  
02359b64   Mihail   add mail parser a...
106
              }
7f6a9301   Mihail   add mails classes
107
  
02359b64   Mihail   add mail parser a...
108
          }
02359b64   Mihail   add mail parser a...
109
  }