44
55namespace Stackkit \LaravelDatabaseEmails ;
66
7+ use Closure ;
78use Exception ;
89use Carbon \Carbon ;
10+ use Illuminate \Database \Eloquent \Builder ;
911use Illuminate \Database \Eloquent \Model ;
12+ use Illuminate \Database \Eloquent \Prunable ;
1013
1114/**
1215 * @property $id
1518 * @property $from
1619 * @property $cc
1720 * @property $bcc
21+ * @property $reply_to
1822 * @property $subject
1923 * @property $view
2024 * @property $variables
3337class Email extends Model
3438{
3539 use HasEncryptedAttributes;
40+ use Prunable;
3641
3742 /**
3843 * The table in which the e-mails are stored.
@@ -48,6 +53,8 @@ class Email extends Model
4853 */
4954 protected $ guarded = [];
5055
56+ public static ?Closure $ pruneQuery = null ;
57+
5158 /**
5259 * Compose a new e-mail.
5360 *
@@ -190,6 +197,26 @@ public function getBccAttribute()
190197 return $ this ->bcc ;
191198 }
192199
200+ /**
201+ * Get the e-mail reply-to addresses.
202+ *
203+ * @return array|string
204+ */
205+ public function getReplyTo ()
206+ {
207+ return $ this ->reply_to ;
208+ }
209+
210+ /**
211+ * Get the e-mail reply-to addresses.
212+ *
213+ * @return array|string
214+ */
215+ public function getReplyToAttribute ()
216+ {
217+ return $ this ->reply_to ;
218+ }
219+
193220 /**
194221 * Get the e-mail subject.
195222 *
@@ -388,6 +415,16 @@ public function hasBcc(): bool
388415 return strlen ($ this ->getRawDatabaseValue ('bcc ' )) > 0 ;
389416 }
390417
418+ /**
419+ * Determine if the e-mail should sent with reply-to.
420+ *
421+ * @return bool
422+ */
423+ public function hasReplyTo (): bool
424+ {
425+ return strlen ($ this ->getRawDatabaseValue ('reply_to ' ) ?: '' ) > 0 ;
426+ }
427+
391428 /**
392429 * Determine if the e-mail is scheduled to be sent later.
393430 *
@@ -520,4 +557,25 @@ public function getRawDatabaseValue(string $key = null, $default = null)
520557
521558 return $ this ->getOriginal ($ key , $ default );
522559 }
560+
561+ /**
562+ * @param Closure $closure
563+ * @return void
564+ */
565+ public static function pruneWhen (Closure $ closure )
566+ {
567+ static ::$ pruneQuery = $ closure ;
568+ }
569+
570+ /**
571+ * @return Builder
572+ */
573+ public function prunable ()
574+ {
575+ if (static ::$ pruneQuery ) {
576+ return (static ::$ pruneQuery )($ this );
577+ }
578+
579+ return $ this ->where ('created_at ' , '< ' , now ()->subMonths (6 ));
580+ }
523581}
0 commit comments