22
33namespace Buildcode \LaravelDatabaseEmails ;
44
5- use Illuminate \Contracts \Encryption \DecryptException ;
65use Illuminate \Database \Eloquent \Model ;
6+ use Illuminate \Mail \Message ;
77use Illuminate \Support \Facades \Event ;
88use Illuminate \Support \Facades \Mail ;
99use Carbon \Carbon ;
1010use Exception ;
1111
12+ /**
13+ * @property $id
14+ * @property $label
15+ * @property $recipient
16+ * @property $cc
17+ * @property $bcc
18+ * @property $subject
19+ * @property $view
20+ * @property $variables
21+ * @property $body
22+ * @property $attempts
23+ * @property $sending
24+ * @property $failed
25+ * @property $error
26+ * @property $encrypted
27+ * @property $scheduled_at
28+ * @property $sent_at
29+ * @property $delivered_at
30+ */
1231class Email extends Model
1332{
33+ /**
34+ * Make sure all encrypted attributes are decrypted.
35+ */
36+ use HasEncryptedAttributes;
37+
1438 /**
1539 * The table in which the e-mails are stored.
1640 *
@@ -62,7 +86,7 @@ public function getLabel()
6286 */
6387 public function getRecipient ()
6488 {
65- return $ this ->getEmailProperty ( ' recipient ' ) ;
89+ return $ this ->recipient ;
6690 }
6791
6892 /**
@@ -72,12 +96,6 @@ public function getRecipient()
7296 */
7397 public function getCc ()
7498 {
75- if ($ this ->exists ) {
76- $ cc = $ this ->getEmailProperty ('cc ' );
77-
78- return json_decode ($ cc , 1 );
79- }
80-
8199 return $ this ->cc ;
82100 }
83101
@@ -88,12 +106,6 @@ public function getCc()
88106 */
89107 public function getBcc ()
90108 {
91- if ($ this ->exists ) {
92- $ bcc = $ this ->getEmailProperty ('bcc ' );
93-
94- return json_decode ($ bcc , 1 );
95- }
96-
97109 return $ this ->bcc ;
98110 }
99111
@@ -104,7 +116,7 @@ public function getBcc()
104116 */
105117 public function getSubject ()
106118 {
107- return $ this ->getEmailProperty ( ' subject ' ) ;
119+ return $ this ->subject ;
108120 }
109121
110122 /**
@@ -125,16 +137,6 @@ public function getView()
125137 */
126138 public function getVariables ()
127139 {
128- if ($ this ->exists ) {
129- $ var = $ this ->getEmailProperty ('variables ' );
130-
131- return json_decode ($ var , 1 );
132- }
133-
134- if (is_string ($ this ->variables )) {
135- return json_decode ($ this ->variables , 1 );
136- }
137-
138140 return $ this ->variables ;
139141 }
140142
@@ -145,7 +147,7 @@ public function getVariables()
145147 */
146148 public function getBody ()
147149 {
148- return $ this ->getEmailProperty ( ' body ' ) ;
150+ return $ this ->body ;
149151 }
150152
151153 /**
@@ -251,7 +253,7 @@ public function isScheduled()
251253 */
252254 public function isEncrypted ()
253255 {
254- return !!$ this ->encrypted ;
256+ return !!$ this ->getOriginal ( ' encrypted ' ) ;
255257 }
256258
257259 /**
@@ -274,25 +276,6 @@ public function hasFailed()
274276 return $ this ->failed == 1 ;
275277 }
276278
277- /**
278- * Get a decrypted property.
279- *
280- * @param string $property
281- * @return mixed
282- */
283- private function getEmailProperty ($ property )
284- {
285- if ($ this ->exists && $ this ->isEncrypted ()) {
286- try {
287- return decrypt ($ this ->{$ property });
288- } catch (DecryptException $ e ) {
289- return '' ;
290- }
291- }
292-
293- return $ this ->{$ property };
294- }
295-
296279 /**
297280 * Mark the e-mail as sending.
298281 *
@@ -318,6 +301,8 @@ public function markAsSent()
318301 $ this ->update ([
319302 'sending ' => 0 ,
320303 'sent_at ' => $ now ,
304+ 'failed ' => 0 ,
305+ 'error ' => '' ,
321306 ]);
322307 }
323308
@@ -352,8 +337,8 @@ public function send()
352337 if (app ()->runningUnitTests ()) {
353338 Event::dispatch ('before.send ' );
354339 }
355-
356- Mail::send ([], [], function ($ message ) {
340+
341+ Mail::send ([], [], function (Message $ message ) {
357342 $ message ->to ($ this ->getRecipient ())
358343 ->cc ($ this ->hasCc () ? $ this ->getCc () : [])
359344 ->bcc ($ this ->hasBcc () ? $ this ->getBcc () : [])
0 commit comments