@@ -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,88 +72,120 @@ 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 | 141 | $this->compiled_body = null; |
110 | 142 | $this->contentType = $value; |
111 | - } else if ($value===false) $this->contentType = ''; |
|
143 | + } else if ($value===false) { |
|
144 | + $this->contentType = ''; |
|
145 | + } |
|
112 | 146 | return $this->contentType; |
113 | 147 | } |
114 | 148 | |
115 | - public function message($value=null){ |
|
149 | + public function message($value=null) { |
|
116 | 150 | if ($value!==null && $value) { |
117 | 151 | $this->compiled_body = null; |
118 | 152 | $this->message = $value; |
119 | - } else if ($value===false) $this->message = ''; |
|
153 | + } else if ($value===false) { |
|
154 | + $this->message = ''; |
|
155 | + } |
|
120 | 156 | return $this->message; |
121 | 157 | } |
122 | 158 | |
123 | - public function attach($file){ |
|
159 | + public function attach($file) { |
|
124 | 160 | $this->compiled_body = null; |
125 | 161 | if (isset($file->content) || isset($file['content'])) { |
126 | 162 | $this->attachments[] = $file; |
127 | - } else foreach ((array)$file as $curfile) { |
|
163 | + } else { |
|
164 | + foreach ((array)$file as $curfile) { |
|
128 | 165 | $this->attachments[] = $curfile; |
129 | 166 | } |
167 | + } |
|
130 | 168 | } |
131 | 169 | |
132 | - public function head($recompile = false){ |
|
133 | - if ($recompile || (null === $this->compiled_head)){ |
|
170 | + public function head($recompile = false) { |
|
171 | + if ($recompile || (null === $this->compiled_head)) { |
|
134 | 172 | $head = []; |
135 | 173 | $head[] = "Subject: {$this->subject}"; |
136 | - if($this->from) $head[] = "From: {$this->from}"; |
|
137 | - if(is_array($this->to) && !empty($this->to)) $head[] = "To: " . implode(', ',$this->to); |
|
138 | - if(is_array($this->cc) && !empty($this->cc)) $head[] = "Cc: " . implode(', ',$this->cc); |
|
139 | - if(is_array($this->bcc) && !empty($this->bcc)) $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
140 | - if($this->replyTo) $head[] = "Reply-To: {$this->replyTo}"; |
|
174 | + if($this->from) { |
|
175 | + $head[] = "From: {$this->from}"; |
|
176 | + } |
|
177 | + if(is_array($this->to) && !empty($this->to)) { |
|
178 | + $head[] = "To: " . implode(', ',$this->to); |
|
179 | + } |
|
180 | + if(is_array($this->cc) && !empty($this->cc)) { |
|
181 | + $head[] = "Cc: " . implode(', ',$this->cc); |
|
182 | + } |
|
183 | + if(is_array($this->bcc) && !empty($this->bcc)) { |
|
184 | + $head[] = "Bcc: " . implode(', ',$this->bcc); |
|
185 | + } |
|
186 | + if($this->replyTo) { |
|
187 | + $head[] = "Reply-To: {$this->replyTo}"; |
|
188 | + } |
|
141 | 189 | $head[] = 'MIME-Version: 1.0'; |
142 | 190 | $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->uid}\""; |
143 | 191 | $this->compiled_head = implode("\r\n", $head); |
@@ -145,8 +193,8 @@ discard block |
||
145 | 193 | return \Filter::with( 'core.email.source.head', $this->compiled_head); |
146 | 194 | } |
147 | 195 | |
148 | - public function body($recompile = false){ |
|
149 | - if ($recompile || (null === $this->compiled_body)){ |
|
196 | + public function body($recompile = false) { |
|
197 | + if ($recompile || (null === $this->compiled_body)) { |
|
150 | 198 | $body[] = "--{$this->uid}"; |
151 | 199 | $body[] = "Content-Type: {$this->contentType}"; |
152 | 200 | $body[] = "Content-Transfer-Encoding: quoted-printable"; |
@@ -154,10 +202,12 @@ discard block |
||
154 | 202 | $body[] = quoted_printable_encode($this->message); |
155 | 203 | $body[] = ''; |
156 | 204 | |
157 | - if (!empty($this->attachments)) foreach ((array)$this->attachments as $file) { |
|
205 | + if (!empty($this->attachments)) { |
|
206 | + foreach ((array)$this->attachments as $file) { |
|
158 | 207 | |
159 | 208 | if (is_string($file)) { |
160 | 209 | $name = basename($file); |
210 | + } |
|
161 | 211 | $data = file_get_contents($file); |
162 | 212 | } else { |
163 | 213 | $name = isset($file['name']) ? $file['name'] : 'untitled'; |
@@ -180,7 +230,7 @@ discard block |
||
180 | 230 | return \Filter::with( 'core.email.source.body', $this->compiled_body); |
181 | 231 | } |
182 | 232 | |
183 | - public function build(){ |
|
233 | + public function build() { |
|
184 | 234 | return \Filter::with( 'core.email.source', $this->head() . "\r\n" . $this->body() ); |
185 | 235 | } |
186 | 236 |