@@ -27,27 +27,43 @@ discard block |
||
| 27 | 27 | $compiled_head, |
| 28 | 28 | $compiled_body; |
| 29 | 29 | |
| 30 | - public function __construct($email=null){ |
|
| 30 | + public function __construct($email=null) { |
|
| 31 | 31 | if ($email) { |
| 32 | 32 | $email = (object)$email; |
| 33 | - if(isset($email->to)) $this->to($email->to); |
|
| 34 | - if(isset($email->from)) $this->from($email->from); |
|
| 35 | - if(isset($email->cc)) $this->cc($email->cc); |
|
| 36 | - if(isset($email->bcc)) $this->bcc($email->bcc); |
|
| 37 | - if(isset($email->replyTo)) $this->replyTo($email->replyTo); |
|
| 38 | - if(isset($email->subject)) $this->subject($email->subject); |
|
| 39 | - if(isset($email->message)) $this->message($email->message); |
|
| 40 | - if(isset($email->attachments)) $this->attach($email->attachments); |
|
| 33 | + if(isset($email->to)) { |
|
| 34 | + $this->to($email->to); |
|
| 35 | + } |
|
| 36 | + if(isset($email->from)) { |
|
| 37 | + $this->from($email->from); |
|
| 38 | + } |
|
| 39 | + if(isset($email->cc)) { |
|
| 40 | + $this->cc($email->cc); |
|
| 41 | + } |
|
| 42 | + if(isset($email->bcc)) { |
|
| 43 | + $this->bcc($email->bcc); |
|
| 44 | + } |
|
| 45 | + if(isset($email->replyTo)) { |
|
| 46 | + $this->replyTo($email->replyTo); |
|
| 47 | + } |
|
| 48 | + if(isset($email->subject)) { |
|
| 49 | + $this->subject($email->subject); |
|
| 50 | + } |
|
| 51 | + if(isset($email->message)) { |
|
| 52 | + $this->message($email->message); |
|
| 53 | + } |
|
| 54 | + if(isset($email->attachments)) { |
|
| 55 | + $this->attach($email->attachments); |
|
| 56 | + } |
|
| 41 | 57 | } |
| 42 | 58 | $this->uid = '_CORE_'.md5(uniqid(time())); |
| 43 | 59 | |
| 44 | 60 | } |
| 45 | 61 | |
| 46 | - protected function add_emails(&$pool, $emails, $append=true){ |
|
| 62 | + protected function add_emails(&$pool, $emails, $append=true) { |
|
| 47 | 63 | $this->compiled_head = null; |
| 48 | 64 | foreach ((array)$emails as $values) { |
| 49 | 65 | foreach(preg_split('/\s*,\s*/',$values) as $value) { |
| 50 | - if(strpos($value,'<')!==false){ |
|
| 66 | + if(strpos($value,'<')!==false) { |
|
| 51 | 67 | $value = str_replace('>','',$value); |
| 52 | 68 | $parts = explode('<',$value,2); |
| 53 | 69 | $name = trim(current($parts)); |
@@ -56,89 +72,123 @@ discard block |
||
| 56 | 72 | } else { |
| 57 | 73 | $address = $value; |
| 58 | 74 | } |
| 59 | - if ($append) $pool[] = $address; else $pool = $address; |
|
| 75 | + if ($append) { |
|
| 76 | + $pool[] = $address; |
|
| 77 | + } else { |
|
| 78 | + $pool = $address; |
|
| 79 | + } |
|
| 60 | 80 | } |
| 61 | 81 | } |
| 62 | 82 | } |
| 63 | 83 | |
| 64 | - public function from($value=null){ |
|
| 84 | + public function from($value=null) { |
|
| 65 | 85 | if ($value!==null && $value) { |
| 66 | 86 | $this->add_emails($this->from, $value, false); |
| 67 | - } else if ($value===false) $this->from = ''; |
|
| 87 | + } else if ($value===false) { |
|
| 88 | + $this->from = ''; |
|
| 89 | + } |
|
| 68 | 90 | return $this->from; |
| 69 | 91 | } |
| 70 | 92 | |
| 71 | - public function to($value=null){ |
|
| 93 | + public function to($value=null) { |
|
| 72 | 94 | if ($value!==null && $value) { |
| 73 | 95 | $this->add_emails($this->to, $value); |
| 74 | - } else if ($value===false) $this->to = []; |
|
| 96 | + } else if ($value===false) { |
|
| 97 | + $this->to = []; |
|
| 98 | + } |
|
| 75 | 99 | return $this->to; |
| 76 | 100 | } |
| 77 | 101 | |
| 78 | - public function cc($value=null){ |
|
| 102 | + public function cc($value=null) { |
|
| 79 | 103 | if ($value!==null && $value) { |
| 80 | 104 | $this->add_emails($this->cc, $value); |
| 81 | - } else if ($value===false) $this->cc = []; |
|
| 105 | + } else if ($value===false) { |
|
| 106 | + $this->cc = []; |
|
| 107 | + } |
|
| 82 | 108 | return $this->cc; |
| 83 | 109 | } |
| 84 | 110 | |
| 85 | - public function bcc($value=null){ |
|
| 111 | + public function bcc($value=null) { |
|
| 86 | 112 | if ($value!==null && $value) { |
| 87 | 113 | $this->add_emails($this->bcc, $value); |
| 88 | - } else if ($value===false) $this->bcc = []; |
|
| 114 | + } else if ($value===false) { |
|
| 115 | + $this->bcc = []; |
|
| 116 | + } |
|
| 89 | 117 | return $this->bcc; |
| 90 | 118 | } |
| 91 | 119 | |
| 92 | - public function replyTo($value=null){ |
|
| 120 | + public function replyTo($value=null) { |
|
| 93 | 121 | if ($value!==null && $value) { |
| 94 | 122 | $this->add_emails($this->replyTo, $value, false); |
| 95 | - } else if ($value===false) $this->replyTo = ''; |
|
| 123 | + } else if ($value===false) { |
|
| 124 | + $this->replyTo = ''; |
|
| 125 | + } |
|
| 96 | 126 | return $this->replyTo; |
| 97 | 127 | } |
| 98 | 128 | |
| 99 | - public function subject($value=null){ |
|
| 129 | + public function subject($value=null) { |
|
| 100 | 130 | if ($value!==null && $value) { |
| 101 | 131 | $this->compiled_head = null; |
| 102 | 132 | $this->subject = $value; |
| 103 | - } else if ($value===false) $this->subject = ''; |
|
| 133 | + } else if ($value===false) { |
|
| 134 | + $this->subject = ''; |
|
| 135 | + } |
|
| 104 | 136 | return $this->subject; |
| 105 | 137 | } |
| 106 | 138 | |
| 107 | - public function contentType($value=null){ |
|
| 139 | + public function contentType($value=null) { |
|
| 108 | 140 | if ($value!==null && $value) { |
| 109 | - if (empty($this->attachments)) $this->compiled_head = null; |
|
| 141 | + if (empty($this->attachments)) { |
|
| 142 | + $this->compiled_head = null; |
|
| 143 | + } |
|
| 110 | 144 | $this->compiled_body = null; |
| 111 | 145 | $this->contentType = $value; |
| 112 | - } else if ($value===false) $this->contentType = ''; |
|
| 146 | + } else if ($value===false) { |
|
| 147 | + $this->contentType = ''; |
|
| 148 | + } |
|
| 113 | 149 | return $this->contentType; |
| 114 | 150 | } |
| 115 | 151 | |
| 116 | - public function message($value=null){ |
|
| 152 | + public function message($value=null) { |
|
| 117 | 153 | if ($value!==null && $value) { |
| 118 | 154 | $this->compiled_body = null; |
| 119 | 155 | $this->message = $value; |
| 120 | - } else if ($value===false) $this->message = ''; |
|
| 156 | + } else if ($value===false) { |
|
| 157 | + $this->message = ''; |
|
| 158 | + } |
|
| 121 | 159 | return $this->message; |
| 122 | 160 | } |
| 123 | 161 | |
| 124 | - public function attach($file){ |
|
| 162 | + public function attach($file) { |
|
| 125 | 163 | $this->compiled_body = null; |
| 126 | 164 | if (isset($file->content) || isset($file['content'])) { |
| 127 | 165 | $this->attachments[] = $file; |
| 128 | - } else foreach ((array)$file as $curfile) { |
|
| 166 | + } else { |
|
| 167 | + foreach ((array)$file as $curfile) { |
|
| 129 | 168 | $this->attachments[] = $curfile; |
| 130 | 169 | } |
| 170 | + } |
|
| 131 | 171 | } |
| 132 | 172 | |
| 133 | - public function head($recompile = false){ |
|
| 134 | - if ($recompile || (null === $this->compiled_head)){ |
|
| 173 | + public function head($recompile = false) { |
|
| 174 | + if ($recompile || (null === $this->compiled_head)) { |
|
| 135 | 175 | $head = []; |
| 136 | 176 | $head[] = "Subject: {$this->subject}"; |
| 137 | - if($this->from) $head[] = "From: {$this->from}"; |
|
| 138 | - if(is_array($this->to) && !empty($this->to)) $head[] = "To: " . implode(', ',$this->to); |
|
| 139 | - if(is_array($this->cc) && !empty($this->cc)) $head[] = "Cc: " . implode(', ',$this->cc); |
|
| 140 | - if(is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
| 141 | - if($this->replyTo) $head[] = "Reply-To: {$this->replyTo}"; |
|
| 177 | + if($this->from) { |
|
| 178 | + $head[] = "From: {$this->from}"; |
|
| 179 | + } |
|
| 180 | + if(is_array($this->to) && !empty($this->to)) { |
|
| 181 | + $head[] = "To: " . implode(', ',$this->to); |
|
| 182 | + } |
|
| 183 | + if(is_array($this->cc) && !empty($this->cc)) { |
|
| 184 | + $head[] = "Cc: " . implode(', ',$this->cc); |
|
| 185 | + } |
|
| 186 | + if(is_array($this->bcc) && !empty($this->bcc)) { |
|
| 187 | + $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
| 188 | + } |
|
| 189 | + if($this->replyTo) { |
|
| 190 | + $head[] = "Reply-To: {$this->replyTo}"; |
|
| 191 | + } |
|
| 142 | 192 | $head[] = 'MIME-Version: 1.0'; |
| 143 | 193 | if (!empty($this->attachments)) { |
| 144 | 194 | $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->uid}\""; |
@@ -151,8 +201,8 @@ discard block |
||
| 151 | 201 | return \Filter::with( 'core.email.source.head', $this->compiled_head); |
| 152 | 202 | } |
| 153 | 203 | |
| 154 | - public function body($recompile = false){ |
|
| 155 | - if ($recompile || (null === $this->compiled_body)){ |
|
| 204 | + public function body($recompile = false) { |
|
| 205 | + if ($recompile || (null === $this->compiled_body)) { |
|
| 156 | 206 | if (!empty($this->attachments)) { |
| 157 | 207 | $body[] = "--{$this->uid}"; |
| 158 | 208 | $body[] = "Content-Type: {$this->contentType}"; |
@@ -162,10 +212,12 @@ discard block |
||
| 162 | 212 | $body[] = quoted_printable_encode($this->message); |
| 163 | 213 | $body[] = ''; |
| 164 | 214 | |
| 165 | - if (!empty($this->attachments)) foreach ((array)$this->attachments as $file) { |
|
| 215 | + if (!empty($this->attachments)) { |
|
| 216 | + foreach ((array)$this->attachments as $file) { |
|
| 166 | 217 | |
| 167 | 218 | if (is_string($file)) { |
| 168 | 219 | $name = basename($file); |
| 220 | + } |
|
| 169 | 221 | $data = file_get_contents($file); |
| 170 | 222 | } else { |
| 171 | 223 | $name = isset($file['name']) ? $file['name'] : 'untitled'; |
@@ -187,7 +239,7 @@ discard block |
||
| 187 | 239 | return \Filter::with( 'core.email.source.body', $this->compiled_body); |
| 188 | 240 | } |
| 189 | 241 | |
| 190 | - public function build(){ |
|
| 242 | + public function build() { |
|
| 191 | 243 | return \Filter::with( 'core.email.source', $this->head() . "\r\n" . $this->body() ); |
| 192 | 244 | } |
| 193 | 245 | |