Andi Xhafa
Andi Xhafa

Reputation: 11

Sending auto email in laravel

I want to create a procedure which will sent auto email base on a condition.

On my table I have a expire_date column and want to sent auto email to everyone to remind them that their account will expire next week.
I am using laravel and mysql.

Thank You

Upvotes: 0

Views: 2176

Answers (2)

phaberest
phaberest

Reputation: 3220

You may use laravel's Mail facade.

Something like

$ten_days_from_now = \Carbon\Carbon::now()->addDay(10);
$users = User::where('expire_date', '<', $ten_days_from_now)->get();

foreach ($users as $user) {
    Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
        $m->from('[email protected]', 'Your Application');

        $m->to($user->email, $user->name)->subject('Your Reminder!');
    });
}

You can put something like this into a command and execute it with a cron.

Upvotes: 0

Sherif
Sherif

Reputation: 1537

setup a daily scheduled task that checks users expirations within a week.

in app/Console/kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command('notify:expirations')
              ->daily();
}

and make a new artisan command that checks for expirations.

don't forget to set cron job on the server to run php artisan schedule:run every minute

Upvotes: 1

Related Questions