| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Webino (http://webino.sk/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link        https://github.com/webino/WebinoDev/ for the canonical source repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright   Copyright (c) 2014-2018 Webino, s. r. o. (http://webino.sk/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license     BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace WebinoDev\Mail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use eXorus\PhpMimeMailParser\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Zend\Mail\Message as BaseMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Extended Zend's mail message with attachments support | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | class Message extends BaseMessage | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @var Parser | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     private $parser; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * Return mail parser | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @return Parser | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private function getParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if (null === $this->parser) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $this->parser = new Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $this->fixUniqueHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->parser->setText($this->toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return $this->parser; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Return mail sent date | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function getDate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         /** @var \Zend\Mail\Header\Date $date */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $date = $this->getHeaders()->get('Date'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $date->getFieldValue(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * Return the message body | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function getBody() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if ($this->getAttachments()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 return $this->getParser()->getMessageBody('html'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } catch (\Throwable $exc) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 error_log($exc); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return parent::getBody(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * Return array of mail attachments | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @return \eXorus\PhpMimeMailParser\Attachment[] | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function getAttachments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         /** @var \eXorus\PhpMimeMailParser\Attachment[] $attachments */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $attachments = $this->getParser()->getAttachments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $attachments; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * Fixing headers uniqueness | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     private function fixUniqueHeaders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $headers = $this->getHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $headersCopy = clone $headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $headers->clearHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         foreach ($headersCopy as $header) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $name = $header->getFieldName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $headers->has($name) or $headers->addHeader($header); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * Create mail object from file | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $path Mail file path | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @return self | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public static function fromFile($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         /** @var self $message */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $message = static::fromString(join(PHP_EOL, file($path, FILE_IGNORE_NEW_LINES))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $message; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |