I have a laravel project at hand. However, I did not write this project. They gave me a bug to solve. It shows the problem in this file /app/Http/Controllers/Crons/EmailCronJobController.php
but as far as I understand, it shows here because sending mail is used here. I have not used such services of Laravel before, so I do not know much. I'm putting the screenshot of the problem and the content of the file below, but if there is something different I need to share for you to understand better, I can share it with you.
EmailCronJobController.php
namespace App\Http\Controllers\Crons;
use App\Http\Controllers\Controller;
use App\Mail\Listener\FirstContentListen;
use App\Mail\Listener\NothingListenedFor3Days;
use App\RawListeningData;
use App\User;
use App\UserPayment;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class EmailCronJobController extends Controller
{
public function __invoke()
{
$this->listeners();
}
private function listeners(){
$this->listenersFirstContentListen();
$this->listenersNothingListenedFor3Days();
}
private function listenersNothingListenedFor3Days(){
$users = User::where([
['role_id','=', 4],
['created_at', '>=', Carbon::now()->subDay(7)->toDateTimeString()],
['created_at', '<', Carbon::now()->subDay(3)->toDateTimeString()]
])->get();
$users = $users->filter(function ($user){
return $user->getFirstListenedContent()==null;
});
/*
foreach ($users as $user){
$email_array = explode('@',$user->email);
$email = $email_array[0].'@etrexio.com';
$user->update(['email'=>$email]);
}*/
foreach ($users as $user){
if($user->getCustomField('nothing_listened_3_days_email_send_date')==null){
Mail::to($user)->send(new NothingListenedFor3Days($user));
$user->setCustomField('nothing_listened_3_days_email_send_date',date('Y-m-d H:i:s'));
}
}
}
private function testEmail(){
$active_user_payments = UserPayment::select('user_id')->where('exprire_date', '>=', Carbon::now()->toDateTimeString())->get();
$active_users_ids = $active_user_payments->map(function ($payments){
return $payments->user_id;
});
$users = User::whereIn('id',$active_users_ids)->get();
dd($users);
}
private function listenersFirstContentListen(){
$last_24_h_listening = RawListeningData::select('user_id')->where('created_at', '>=', Carbon::now()->subDay()->toDateTimeString())->get();
$last_24_h_listening_users = $last_24_h_listening->map(function ($listening){
return $listening->user_id;
})->unique();
$before_24_h_listening = RawListeningData::select('user_id')->whereIn('user_id',$last_24_h_listening_users)->where('created_at', '<', Carbon::now()->subDay()->toDateTimeString())->get();
$before_24_h_listening_users = $before_24_h_listening->map(function ($listening){
return $listening->user_id;
})->unique();
$result = collect($last_24_h_listening_users)->diff(collect($before_24_h_listening_users));
$users = User::whereIn('id',$result)->get();
foreach ($users as $user){
if($user->getCustomField('first_content_listen_email_send_date')==null){
$content = $user->getFirstListenedContent();
if($content!=null){
Mail::to($user)->send(new FirstContentListen($user,$content));
$user->setCustomField('first_content_listen_email_send_date',date('Y-m-d H:i:s'));
}
}
}
}
}
NothingListenedFor3Days.php
namespace App\Mail\Listener;
use App\Course;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NothingListenedFor3Days extends Mailable
{
use Queueable, SerializesModels;
public $listener;
public $random_contents;
public $fromEmail = 'listener@omnicourse.io';
public $fromName = 'Omnicourse';
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(User $listener)
{
$this->listener = $listener;
$this->random_contents = Course::all()->random(10);
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from($this->fromEmail,$this->fromName)
->view('email.listener.nothing_listened_for_3_days')
->text('email.listener.nothing_listened_for_3_days_plain')
->subject("Let's move ahead together");
}
}