Blame view

common/components/parsers/MailAttachmentsSaver.php 3.92 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
   */
  
bedb55fe   Mihail   fixed Image class
9
  namespace common\components\parsers;
02359b64   Mihail   add mail parser a...
10
11
  
  
8e7f5c9b   Mihail   add MailAttachmen...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  use common\components\CustomVarDamp;
  
  class MailAttachmentsSaver
  {
  //$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox';
      protected $connection;
  
      public function __construct($hostname, $username, $password)
      {
          $this->connection = imap_open($hostname, $username, $password);
          CustomVarDamp::dumpAndDie( $this->connection);
          if ($this->connection === false)
              throw new \Exception('Cannot connect to Gmail: ' . imap_last_error());
      }
02359b64   Mihail   add mail parser a...
26
  
02359b64   Mihail   add mail parser a...
27
  
8e7f5c9b   Mihail   add MailAttachmen...
28
29
      public function saveAttachmentsTo()
      {
02359b64   Mihail   add mail parser a...
30
  
8e7f5c9b   Mihail   add MailAttachmen...
31
          $emails = imap_search($this->connection, 'FROM forallthings');
02359b64   Mihail   add mail parser a...
32
33
  
          /* if emails are returned, cycle through each... */
8e7f5c9b   Mihail   add MailAttachmen...
34
          if ($emails) {
02359b64   Mihail   add mail parser a...
35
36
37
38
39
40
41
  
              /* begin output var */
              $output = '';
  
              /* put the newest emails on top */
              rsort($emails);
  
02359b64   Mihail   add mail parser a...
42
43
44
              foreach ($emails as $email_number) {
  
                  /* get information specific to this email */
8e7f5c9b   Mihail   add MailAttachmen...
45
46
47
  //                $overview = imap_fetch_overview($inbox, $email_number, 0);
  //                $message = imap_fetchbody($inbox, $email_number, 2);
                  $structure = imap_fetchstructure($this->connection, $email_number);
02359b64   Mihail   add mail parser a...
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
74
75
76
  
                  $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']) {
8e7f5c9b   Mihail   add MailAttachmen...
77
                              $attachments[$i]['attachment'] = imap_fetchbody($this->connection, $email_number, $i + 1);
02359b64   Mihail   add mail parser a...
78
79
80
81
82
83
                              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...
84
                      }
02359b64   Mihail   add mail parser a...
85
  
8e7f5c9b   Mihail   add MailAttachmen...
86
                      CustomVarDamp::dumpAndDie($attachments);
02359b64   Mihail   add mail parser a...
87
  
8e7f5c9b   Mihail   add MailAttachmen...
88
                      if (count($attachments) != 0) {
02359b64   Mihail   add mail parser a...
89
90
  
  
8e7f5c9b   Mihail   add MailAttachmen...
91
                          foreach ($attachments as $at) {
02359b64   Mihail   add mail parser a...
92
  
8e7f5c9b   Mihail   add MailAttachmen...
93
94
95
                              if ($at['is_attachment'] == 1) {
                                  //die(__DIR__);
                                  file_put_contents('test.csv', $at['attachment']);
02359b64   Mihail   add mail parser a...
96
  
8e7f5c9b   Mihail   add MailAttachmen...
97
                              }
02359b64   Mihail   add mail parser a...
98
                          }
8e7f5c9b   Mihail   add MailAttachmen...
99
  
02359b64   Mihail   add mail parser a...
100
101
102
103
                      }
  
                  }
  
8e7f5c9b   Mihail   add MailAttachmen...
104
                  // echo $output;
02359b64   Mihail   add mail parser a...
105
              }
8e7f5c9b   Mihail   add MailAttachmen...
106
107
              /* close the connection */
              imap_close($this->connection);
02359b64   Mihail   add mail parser a...
108
          }
02359b64   Mihail   add mail parser a...
109
110
      }
  }