-
app/Jobs/SendNewsletterSubscriptionEmail.php
Open in GitHubuse App\Mail\Newsletter; use App\Models\Post; use Mail; class SendNewsletterSubscriptionEmail { protected $email; public function __construct($email) { $this->email = $email; } public function handle(): void { $posts = Post::lastMonth()->get(); $email = $this->email; Mail::to($this->email)->send(new Newsletter($posts, $email)); } }
-
app/Mail/Newsletter.php
Open in GitHubuse Illuminate\Mail\Mailable; class Newsletter extends Mailable { protected $posts; protected $email; public function __construct($posts, $email) { $this->posts = $posts; $this->email = $email; } public function build(): Newsletter { return $this->from('hello@app.com', config('app.name', 'Laravel')) ->subject(__('newsletter.email.subject')) ->view('emails.newsletter') ->with([ 'posts' => $this->posts, 'email' => $this->email ]); } }
-
app/Jobs/PrepareNewsletterSubscriptionEmail.php
Open in GitHubuse App\Models\NewsletterSubscription; class PrepareNewsletterSubscriptionEmail { public function handle(): void { $newsletterSubscriptions = NewsletterSubscription::all(); $newsletterSubscriptions->each(fn ($newsletterSubscription) => SendNewsletterSubscriptionEmail::dispatch($newsletterSubscription->email)); PrepareNewsletterSubscriptionEmail::dispatch()->delay(carbon('next month')); } }
-
readme.md
Open in GitHub// Starting job for newsletter : $ artisan tinker > PrepareNewsletterSubscriptionEmail::dispatch(); //