Commit ef87d54f36d944e4877a61d848e3b1cf504e18cb
1 parent
42078ac6
add MailAttachmentSaver class
Showing
3 changed files
with
45 additions
and
31 deletions
Show diff stats
backend/controllers/RgGrupController.php
| ... | ... | @@ -11,6 +11,7 @@ namespace backend\controllers; |
| 11 | 11 | use backend\components\base\BaseController; |
| 12 | 12 | use backend\models\UploadFileRgForm; |
| 13 | 13 | use common\components\CustomVarDamp; |
| 14 | +use common\components\parsers\MailAttachmentsSaver; | |
| 14 | 15 | use common\models\Margins; |
| 15 | 16 | use common\models\MarginsGroups; |
| 16 | 17 | use yii\filters\AccessControl; |
| ... | ... | @@ -188,8 +189,15 @@ class RgGrupController extends BaseController |
| 188 | 189 | |
| 189 | 190 | return $this->render('index', ['model' => $configuration]); |
| 190 | 191 | |
| 191 | - | |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | } |
| 195 | -} | |
| 196 | 195 | \ No newline at end of file |
| 196 | + | |
| 197 | + public function actionMail() | |
| 198 | + { | |
| 199 | + //$mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox', 'mt@soft-terra.com.ua', 'Wehrfyjd8'); | |
| 200 | + $mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'price@italauto.com.ua', '67853562'); | |
| 201 | + $mail_saver->saveAttachmentsTo(); | |
| 202 | + } | |
| 203 | + | |
| 204 | +} | ... | ... |
backend/views/rg-grup/index.php
common/components/parsers/MailParser.php renamed to common/components/parsers/MailAttachmentsSaver.php
| ... | ... | @@ -9,21 +9,29 @@ |
| 9 | 9 | namespace common\components\parsers; |
| 10 | 10 | |
| 11 | 11 | |
| 12 | -class MailParser { | |
| 13 | - public static function requestAction(){ | |
| 14 | - $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox'; | |
| 15 | - $username = 'сюда твой логин'; | |
| 16 | - $password = 'сюда твой пароль'; | |
| 12 | +use common\components\CustomVarDamp; | |
| 13 | + | |
| 14 | +class MailAttachmentsSaver | |
| 15 | +{ | |
| 16 | +//$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox'; | |
| 17 | + protected $connection; | |
| 18 | + | |
| 19 | + public function __construct($hostname, $username, $password) | |
| 20 | + { | |
| 21 | + $this->connection = imap_open($hostname, $username, $password); | |
| 22 | + CustomVarDamp::dumpAndDie( $this->connection); | |
| 23 | + if ($this->connection === false) | |
| 24 | + throw new \Exception('Cannot connect to Gmail: ' . imap_last_error()); | |
| 25 | + } | |
| 17 | 26 | |
| 18 | - /* try to connect */ | |
| 19 | - $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); | |
| 20 | 27 | |
| 28 | + public function saveAttachmentsTo() | |
| 29 | + { | |
| 21 | 30 | |
| 22 | - $emails = imap_search($inbox,'FROM forallthings'); | |
| 31 | + $emails = imap_search($this->connection, 'FROM forallthings'); | |
| 23 | 32 | |
| 24 | 33 | /* if emails are returned, cycle through each... */ |
| 25 | - /* if emails are returned, cycle through each... */ | |
| 26 | - if($emails) { | |
| 34 | + if ($emails) { | |
| 27 | 35 | |
| 28 | 36 | /* begin output var */ |
| 29 | 37 | $output = ''; |
| ... | ... | @@ -31,16 +39,12 @@ class MailParser { |
| 31 | 39 | /* put the newest emails on top */ |
| 32 | 40 | rsort($emails); |
| 33 | 41 | |
| 34 | - | |
| 35 | 42 | foreach ($emails as $email_number) { |
| 36 | 43 | |
| 37 | 44 | /* get information specific to this email */ |
| 38 | - $overview = imap_fetch_overview($inbox, $email_number, 0); | |
| 39 | - $message = imap_fetchbody($inbox, $email_number, 2); | |
| 40 | - $structure = imap_fetchstructure($inbox, $email_number); | |
| 41 | - | |
| 42 | - | |
| 43 | - | |
| 45 | +// $overview = imap_fetch_overview($inbox, $email_number, 0); | |
| 46 | +// $message = imap_fetchbody($inbox, $email_number, 2); | |
| 47 | + $structure = imap_fetchstructure($this->connection, $email_number); | |
| 44 | 48 | |
| 45 | 49 | $attachments = array(); |
| 46 | 50 | if (isset($structure->parts) && count($structure->parts)) { |
| ... | ... | @@ -70,36 +74,37 @@ class MailParser { |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | 76 | if ($attachments[$i]['is_attachment']) { |
| 73 | - $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i + 1); | |
| 77 | + $attachments[$i]['attachment'] = imap_fetchbody($this->connection, $email_number, $i + 1); | |
| 74 | 78 | if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 |
| 75 | 79 | $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); |
| 76 | 80 | } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE |
| 77 | 81 | $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); |
| 78 | 82 | } |
| 79 | 83 | } |
| 80 | - } // for($i = 0; $i < count($structure->parts); $i++) | |
| 81 | - } // if(isset($structure->parts) && count($structure->parts)) | |
| 84 | + } | |
| 82 | 85 | |
| 86 | + CustomVarDamp::dumpAndDie($attachments); | |
| 83 | 87 | |
| 84 | - if (count($attachments) != 0) { | |
| 88 | + if (count($attachments) != 0) { | |
| 85 | 89 | |
| 86 | 90 | |
| 87 | - foreach ($attachments as $at) { | |
| 91 | + foreach ($attachments as $at) { | |
| 88 | 92 | |
| 89 | - if ($at['is_attachment'] == 1) { | |
| 90 | - //die(__DIR__); | |
| 91 | - file_put_contents('test.csv', $at['attachment']); | |
| 93 | + if ($at['is_attachment'] == 1) { | |
| 94 | + //die(__DIR__); | |
| 95 | + file_put_contents('test.csv', $at['attachment']); | |
| 92 | 96 | |
| 97 | + } | |
| 93 | 98 | } |
| 99 | + | |
| 94 | 100 | } |
| 95 | 101 | |
| 96 | 102 | } |
| 97 | 103 | |
| 104 | + // echo $output; | |
| 98 | 105 | } |
| 99 | - | |
| 100 | - // echo $output; | |
| 106 | + /* close the connection */ | |
| 107 | + imap_close($this->connection); | |
| 101 | 108 | } |
| 102 | - /* close the connection */ | |
| 103 | - imap_close($inbox); | |
| 104 | 109 | } |
| 105 | 110 | } |
| 106 | 111 | \ No newline at end of file | ... | ... |