Blame view

common/components/parsers/MailAttachmentsSaver.php 3.92 KB
5c2732df   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
   */
  
912e6b15   Mihail   fixed Image class
9
  namespace common\components\parsers;
5c2732df   Mihail   add mail parser a...
10
11
  
  
ef87d54f   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());
      }
5c2732df   Mihail   add mail parser a...
26
  
5c2732df   Mihail   add mail parser a...
27
  
ef87d54f   Mihail   add MailAttachmen...
28
29
      public function saveAttachmentsTo()
      {
5c2732df   Mihail   add mail parser a...
30
  
ef87d54f   Mihail   add MailAttachmen...
31
          $emails = imap_search($this->connection, 'FROM forallthings');
5c2732df   Mihail   add mail parser a...
32
33
  
          /* if emails are returned, cycle through each... */
ef87d54f   Mihail   add MailAttachmen...
34
          if ($emails) {
5c2732df   Mihail   add mail parser a...
35
36
37
38
39
40
41
  
              /* begin output var */
              $output = '';
  
              /* put the newest emails on top */
              rsort($emails);
  
5c2732df   Mihail   add mail parser a...
42
43
44
              foreach ($emails as $email_number) {
  
                  /* get information specific to this email */
ef87d54f   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);
5c2732df   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']) {
ef87d54f   Mihail   add MailAttachmen...
77
                              $attachments[$i]['attachment'] = imap_fetchbody($this->connection, $email_number, $i + 1);
5c2732df   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']);
                              }
                          }
ef87d54f   Mihail   add MailAttachmen...
84
                      }
5c2732df   Mihail   add mail parser a...
85
  
ef87d54f   Mihail   add MailAttachmen...
86
                      CustomVarDamp::dumpAndDie($attachments);
5c2732df   Mihail   add mail parser a...
87
  
ef87d54f   Mihail   add MailAttachmen...
88
                      if (count($attachments) != 0) {
5c2732df   Mihail   add mail parser a...
89
90
  
  
ef87d54f   Mihail   add MailAttachmen...
91
                          foreach ($attachments as $at) {
5c2732df   Mihail   add mail parser a...
92
  
ef87d54f   Mihail   add MailAttachmen...
93
94
95
                              if ($at['is_attachment'] == 1) {
                                  //die(__DIR__);
                                  file_put_contents('test.csv', $at['attachment']);
5c2732df   Mihail   add mail parser a...
96
  
ef87d54f   Mihail   add MailAttachmen...
97
                              }
5c2732df   Mihail   add mail parser a...
98
                          }
ef87d54f   Mihail   add MailAttachmen...
99
  
5c2732df   Mihail   add mail parser a...
100
101
102
103
                      }
  
                  }
  
ef87d54f   Mihail   add MailAttachmen...
104
                  // echo $output;
5c2732df   Mihail   add mail parser a...
105
              }
ef87d54f   Mihail   add MailAttachmen...
106
107
              /* close the connection */
              imap_close($this->connection);
5c2732df   Mihail   add mail parser a...
108
          }
5c2732df   Mihail   add mail parser a...
109
110
      }
  }