From ef87d54f36d944e4877a61d848e3b1cf504e18cb Mon Sep 17 00:00:00 2001 From: Mihail Date: Fri, 30 Oct 2015 12:38:46 +0200 Subject: [PATCH] add MailAttachmentSaver class --- backend/controllers/RgGrupController.php | 12 ++++++++++-- backend/views/rg-grup/index.php | 1 + common/components/parsers/MailAttachmentsSaver.php | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/components/parsers/MailParser.php | 105 --------------------------------------------------------------------------------------------------------- 4 files changed, 121 insertions(+), 107 deletions(-) create mode 100644 common/components/parsers/MailAttachmentsSaver.php delete mode 100644 common/components/parsers/MailParser.php diff --git a/backend/controllers/RgGrupController.php b/backend/controllers/RgGrupController.php index d380eb1..04d43b0 100644 --- a/backend/controllers/RgGrupController.php +++ b/backend/controllers/RgGrupController.php @@ -11,6 +11,7 @@ namespace backend\controllers; use backend\components\base\BaseController; use backend\models\UploadFileRgForm; use common\components\CustomVarDamp; +use common\components\parsers\MailAttachmentsSaver; use common\models\Margins; use common\models\MarginsGroups; use yii\filters\AccessControl; @@ -188,8 +189,15 @@ class RgGrupController extends BaseController return $this->render('index', ['model' => $configuration]); - } } -} \ No newline at end of file + + public function actionMail() + { + //$mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox', 'mt@soft-terra.com.ua', 'Wehrfyjd8'); + $mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'price@italauto.com.ua', '67853562'); + $mail_saver->saveAttachmentsTo(); + } + +} diff --git a/backend/views/rg-grup/index.php b/backend/views/rg-grup/index.php index 47addb0..09c4ac8 100644 --- a/backend/views/rg-grup/index.php +++ b/backend/views/rg-grup/index.php @@ -29,5 +29,6 @@ $button_label = 'Прочитать'; + 'btn btn-primary', 'name' => 'Mail',]) ?> diff --git a/common/components/parsers/MailAttachmentsSaver.php b/common/components/parsers/MailAttachmentsSaver.php new file mode 100644 index 0000000..1a93b4b --- /dev/null +++ b/common/components/parsers/MailAttachmentsSaver.php @@ -0,0 +1,110 @@ +connection = imap_open($hostname, $username, $password); + CustomVarDamp::dumpAndDie( $this->connection); + if ($this->connection === false) + throw new \Exception('Cannot connect to Gmail: ' . imap_last_error()); + } + + + public function saveAttachmentsTo() + { + + $emails = imap_search($this->connection, 'FROM forallthings'); + + /* if emails are returned, cycle through each... */ + if ($emails) { + + /* begin output var */ + $output = ''; + + /* put the newest emails on top */ + rsort($emails); + + foreach ($emails as $email_number) { + + /* get information specific to this email */ +// $overview = imap_fetch_overview($inbox, $email_number, 0); +// $message = imap_fetchbody($inbox, $email_number, 2); + $structure = imap_fetchstructure($this->connection, $email_number); + + $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']) { + $attachments[$i]['attachment'] = imap_fetchbody($this->connection, $email_number, $i + 1); + 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']); + } + } + } + + CustomVarDamp::dumpAndDie($attachments); + + if (count($attachments) != 0) { + + + foreach ($attachments as $at) { + + if ($at['is_attachment'] == 1) { + //die(__DIR__); + file_put_contents('test.csv', $at['attachment']); + + } + } + + } + + } + + // echo $output; + } + /* close the connection */ + imap_close($this->connection); + } + } +} \ No newline at end of file diff --git a/common/components/parsers/MailParser.php b/common/components/parsers/MailParser.php deleted file mode 100644 index 730f50a..0000000 --- a/common/components/parsers/MailParser.php +++ /dev/null @@ -1,105 +0,0 @@ -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']) { - $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i + 1); - 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']); - } - } - } // for($i = 0; $i < count($structure->parts); $i++) - } // if(isset($structure->parts) && count($structure->parts)) - - - if (count($attachments) != 0) { - - - foreach ($attachments as $at) { - - if ($at['is_attachment'] == 1) { - //die(__DIR__); - file_put_contents('test.csv', $at['attachment']); - - } - } - - } - - } - - // echo $output; - } - /* close the connection */ - imap_close($inbox); - } -} \ No newline at end of file -- libgit2 0.21.4