0

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.

enter image description here

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");
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
iguner
  • 51
  • 8
  • What have you tried to resolve the problem? Did you check whether the mail address is a valid one? – Nico Haase Sep 01 '22 at 08:46
  • @NicoHaase Yes, the e-mail (listener@omnicourse.io) address is valid. – iguner Sep 01 '22 at 08:50
  • Are you sure you are looking for the proper part of your code? Is the recipients address also valid? – Nico Haase Sep 01 '22 at 08:59
  • @NicoHaase Yes, the recipient address is also valid. – iguner Sep 01 '22 at 09:04
  • Then, what have you tried to resolve the problem? Why not step through the execution of your code to see which input triggers that problem? – Nico Haase Sep 01 '22 at 09:05
  • @NicoHaase Obviously, the problem is not something that happens all the time. In other words, once or twice a day, sometimes sentry.io never drops. So I don't fully understand what triggered the problem. – iguner Sep 01 '22 at 09:10
  • Then please check this further. I would assume that any of the recipients' addresses is invalid – Nico Haase Sep 01 '22 at 09:14
  • @NicoHaase Ok, I'll check again in detail. Besides, is there anything else you can suggest to me? – iguner Sep 01 '22 at 09:19
  • Would `Mail::to($user)` work? I would expect you'd need something like `Mail::to($user->email)` or something – apokryfos Sep 01 '22 at 09:23
  • Currently not. Debugging the problem and checking which input triggers the problem should be the first case. Afterwards, you need to check for invalid inputs and reject the mail sending process for them – Nico Haase Sep 01 '22 at 09:24
  • @apokryfos Should I change the EmailCronJobController.php file as you say? – iguner Sep 01 '22 at 09:34
  • I do apologise, my post was incorrect – migsAV Sep 01 '22 at 09:38
  • It couldn't hurt to try I guess – apokryfos Sep 01 '22 at 09:50
  • @migsAV It's okay mate. If there's anything you think will help me figure it out, you can share it with me. – iguner Sep 01 '22 at 13:47

1 Answers1

0

I think it's probably this line:

            Mail::to($user)>send(newFirstContentListen($user,$content));

$user in your case it's an object not email. Try to debug it or dd() at several points to see where it get stucks.

Also might want to check this out

Address in mailbox given [] does not comply with RFC 2822, 3.6.2. when email is in a variable

Kevin
  • 1,152
  • 5
  • 13
  • That won't explain why this works sometimes, and fails at other times – Nico Haase Sep 01 '22 at 12:14
  • It does explain. It's wrapped inside an if condition and it will not always enter inside there – Kevin Sep 01 '22 at 12:41
  • So, some mails get sent out properly by this line, and some are not? – Nico Haase Sep 01 '22 at 12:58
  • No, incorrect. In other uses cases the email is formatted properly and won't throw error. In the other case, when the condition is true, it will go there and the $user will be an object not email – Kevin Sep 01 '22 at 13:01
  • Feel free to add all clarification to your question by editing it – Nico Haase Sep 01 '22 at 13:19
  • @workservice I was in a meeting, I couldn't answer you. What exactly should I do now? Frankly, I'm a little confused. What should I do or try as a solution or is there anything I should pass on so that I can assist you? – iguner Sep 01 '22 at 13:45
  • @NicoHaase What do you think about this situation? – iguner Sep 01 '22 at 13:46
  • Why not check for the root cause of the error, like recommended some hours ago? – Nico Haase Sep 01 '22 at 13:50
  • @NicoHaase I have already checked the sender e-mail address and the recipient e-mail address, there is no problem with it. Other than that, I don't know exactly where to check as I don't know what the root cause of the error is. If you tell me to check that place, I will take care of it right away. I know I'm boring you because I'm new to this stuff. But I'm trying to understand too, :) – iguner Sep 01 '22 at 14:04
  • @iguner you should pass the user email in that case, not only the user object and it will be okay – Kevin Sep 01 '22 at 14:10
  • @workservice like this: `Mail::to($user->email)->send(new NothingListenedFor3Days($user));` is it true? – iguner Sep 01 '22 at 14:23
  • Yep you can try that. Check both Mail::to() Any place that should store email and you are not passing it, replace it with an email. You could also replace it with email string just for test purposes and then replace them back with actual code. Debug is your friend – Kevin Sep 01 '22 at 14:25
  • @workservice OK, I'll try. Thank you very much for your help, but if I fail, I will write to you again here, please do not be angry with me. – iguner Sep 01 '22 at 14:31
  • all good, happy to help! – Kevin Sep 01 '22 at 14:38