Have you seen this? http://laravel.com/docs/5.0/artisan#scheduling-artisan-commands
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.
- How can this be done
- Do i need to use Crons or anything?
Best and easiest way please if someone can advise on this!
Thanks in advance.
Yes, not sure how that will help in this scenario though :-)
@lstables you create a command that checks the database and then sends the email.
yes, i have create command and how to checks with database and then send? please guide
@mstnorris ok not sure how though I would do this! :( commands are pretty new to me.
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.
Of course....Yes I'll go watch these...
I think this is the one https://laracasts.com/lessons/laracasts-crons-and-notifications
Currently watching this https://laracasts.com/series/intermediate-laravel/episodes/1 as I'm building within L5 not 4
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.
Yeah thanks, here we go!!!
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.
Just as I'm doing now @Muzammil and within the fire method adding all logic, so I'm on track it seems....
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!
What date column are you checking?
Is it an instance of Carbon?
If it is then you can do
$date->subHour();
It's just called 'date' with a timestamp type.
Then add the following to your model.
protected $dates = [
'date'
];
I get...
InvalidArgumentException in Carbon.php line 392:
Data missing
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.
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
Where is the code where you are trying to subtract an hour from date?
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');
});
}
}
If you
public function sendTo($user, $todo)
{
if (! empty($todo->date)) {
dd($todo->date)
...
}
}
What do you get?
I get 2015-05-26 14:40:00
Try
public function sendTo($user, $todo)
{
if (! empty($todo->date)) {
...
$date = Carbon::parse($todo->date)->subHour();
...
}
}
Still getting Data missing error
Edited, try now.
Nope, still the same :(
By removing protected $dates = [ 'date' ]; worked.
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
}
}
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.
Please or to participate in this conversation.