Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Send email 1 hour before....

Hi all,

I'm looking for a way/method that I can grab the timestamp from the database then say if one hour before this timestamp, send the email.

  1. How can this be done
  2. Do i need to use Crons or anything?

Best and easiest way please if someone can advise on this!

Thanks in advance.

0 likes
37 replies
theUnforgiven's avatar

Yes, not sure how that will help in this scenario though :-)

fahdshaykh's avatar

yes, i have create command and how to checks with database and then send? please guide

mstnorris's avatar

Jeffrey has a bunch of lessons on the subject. I think he even covers exactly what you're after in one of these. Not sure which one though.

mstnorris's avatar

That shouldn't make too much difference, obviously certain things may have changed, but you will get the idea and it will get you started.

Muzammil's avatar

Firstly create a custom command and then set that command in your cron tab (in hosting server)

To create command

$ php artisan make:console sendEmails --command=sendEmails

then go to app/Console/Commands/sendEmails.php and add your logic to pull the entries from db and sending emails etc in fire() method.

and now go to app/Console/Kernel.php and register this sendEmails command in schedule() method :

$schedule->command('sendEmails');

you can verify and test the command by $ php artisan sendEmails.

if everything goes well , set this command to run at specific interval of time as per your need in cron tab. Thats it.

1 like
theUnforgiven's avatar

Just as I'm doing now @Muzammil and within the fire method adding all logic, so I'm on track it seems....

1 like
theUnforgiven's avatar

Mmmm, cant seem to get the logic to check the date from the database then subtract 1 hour to send 1 hour before the date and time from the database. Maybe just having a brain freeze day!

mstnorris's avatar

What date column are you checking?
Is it an instance of Carbon?

If it is then you can do

$date->subHour();
mstnorris's avatar

Then add the following to your model.

protected $dates = [
    'date'
];
theUnforgiven's avatar

I get...

InvalidArgumentException in Carbon.php line 392:
Data missing
mstnorris's avatar

Can you post your model class here and the code where you are manipulating the date field. It looks like you need more data when parsing the data for the Carbon instance.

1 like
theUnforgiven's avatar

Model:

<?php namespace CRM;

use Illuminate\Database\Eloquent\Model;

class Todo extends Model {

    /**
     * Guarded props
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * Table
     * @var string
     */
    public $table = 'todos';

    /**
     * @return array
     */
    protected $dates = [
        'date'
    ];

    /**
     * Save Done Attribute to boolean
     * @param $value
     * @return bool
     */
    public function getDoneAttribute($value)
    {
        return (boolean) $value;
    }

    /**
     * Belongs to a user
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('CRM\User');
    }

}

Then the date column has dates in like 2015-05-26 14:40:00

mstnorris's avatar

Where is the code where you are trying to subtract an hour from date?

theUnforgiven's avatar
public function sendTo($user, $todo)
    {
        if (! empty($todo->date)) {

            $data = array(
                'name'  => $user->name,
                'email' => $user->email,
                'title' => $todo->title,
                'date'  => $todo->date
            );
            $date = $todo->date->subHours(1);
            // Send the email
            Mail::later($date, 'emails/todoNotification', $data, function ($message) use ($data) {
                $message->to($data['email'])
                    ->from('noreply@example.com')
                    ->subject('Todo Task Notification');
            });

        }

    }
mstnorris's avatar

If you

public function sendTo($user, $todo)
{
    if (! empty($todo->date)) {
        dd($todo->date)
        ...
    }
}

What do you get?

mstnorris's avatar

Try

public function sendTo($user, $todo)
{
    if (! empty($todo->date)) {

        ...

        $date = Carbon::parse($todo->date)->subHour();
        
        ...

    }
}
mstnorris's avatar
public function sendTo($user, $todo)
{
    if (! empty($todo->date)) {

        dd($todo->date); // 1. try this first, tell me what you get

        $date = Carbon::createFromFormat('Y-m-d H:i:s', $todo->date); // 2. try this

        dd($date); // 3. try dumping the $date now

    }
}
mstnorris's avatar

So there was something else wrong in your code, as if $date was an instance of Carbon in the beginning then ->subHour() would have worked. Glad it is sorted now though.

Next

Please or to participate in this conversation.