Project "Address in mailbox given [] does not comply with RFC 2822, 3.6.2." gives a fault

Viewed 23

I have a Laravel backend project. This project is currently running in Laravel 8. But sometimes it gives Address in mailbox given [] does not comply with RFC 2822, 3.6.2. error. But as I said, it only gives this error sometimes because when I follow on sentry.io, I only see it two or three times a week. When I look at sentry.io, it shows the error in App\Http\Controllers\Crons\EmailCronJobController::listenersNothingListenedFor3Days here.

Here is the block:

    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'));
            }
        }
    }

EmailCronJobController.php

<?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");
    }
}

What could be the solution to this, I searched a lot, but I couldn't find anything clear.

0 Answers
Related