-2
    baseos | u554-docm7xa6j209@gsydm1027.siteground.biz:/home/customer/www/lostwillregister.com.au$ cd /home/customer/www/lostwillregister.com.au; /usr/local/php74/bin/php-cli artisan marketing:startCampaign -v
    
       Symfony\Component\Debug\Exception\FatalThrowableError  : Call to a member function isEmpty() on array
    
      at /home/customer/www/lostwillregister.com.au/app/Http/Controllers/SendLostwillEmailsController.php:34
        30|             $lostwill = LostWill::select('id', 'lost_first_name', 'lost_last_name', 'lost_dod', 'user_id')->whereDate('created_at', $yesterdayDate)->get();
        31| 
        32|             if (!$lostwill->isEmpty()) {
        33|                 $listOfEmails = $this->getEmailList($lostwill->pluck('user_id'));
      > 34|                 if (!$listOfEmails['emails']->isEmpty()) {
        35|                     $newMailSetting->sendMail();
        36|                     $listChunkEmail = array_chunk($listOfEmails['emails'], 550, false);
        37|                     $i = 0;
        38|                     $countSentMail = 0;
    
      Exception trace:
    
     
  1   App\Http\Controllers\SendLostwillEmailsController::getAllLostWill()
      /home/customer/www/lostwillregister.com.au/app/Http/Controllers/SendLostwillEmailsController.php:20

  2   App\Http\Controllers\SendLostwillEmailsController::runCommand()
      /home/customer/www/lostwillregister.com.au/app/Console/Commands/SendMarketingEmailCron.php:44

  3   App\Console\Commands\SendMarketingEmailCron::handle()
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32

  4   call_user_func_array([])
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:32

  5   Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:90

  6   Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure))
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:34

  7   Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), [])
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Container/Container.php:576

  8   Illuminate\Container\Container::call()
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Console/Command.php:183

  9   Illuminate\Console\Command::execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      /home/customer/www/lostwillregister.com.au/vendor/symfony/console/Command/Command.php:255

  10  Symfony\Component\Console\Command\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Console/Command.php:170

  11  Illuminate\Console\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      /home/customer/www/lostwillregister.com.au/vendor/symfony/console/Application.php:1009

  12  Symfony\Component\Console\Application::doRunCommand(Object(App\Console\Commands\SendMarketingEmailCron), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      /home/customer/www/lostwillregister.com.au/vendor/symfony/console/Application.php:273

  13  Symfony\Component\Console\Application::doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      /home/customer/www/lostwillregister.com.au/vendor/symfony/console/Application.php:149

  14  Symfony\Component\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Console/Application.php:90

  15  Illuminate\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      /home/customer/www/lostwillregister.com.au/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:133

  16  Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
      /home/customer/www/lostwillregister.com.au/artisan:37

I get the above error in the class SendLostwillEmailsController at line 20 and line 44 and I belive its why my mail is not getting send. Can anyone please help me what those erro means?

My problem is the mail is not getting send and I am using getAllLostWill() function to send mail and the error seems something wrong with the method. I am new to it so can't figure out whats wrong, so why you think its not sending mail?

<?php

namespace App\Http\Controllers;

use App\LostWill;
use App\MailAction\ChangeMailSettings;
use App\Notifications\SendLostwillMarketingEmail;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use DB;
use Exception;
use Illuminate\Support\Facades\Log;
use Notification;

class SendLostwillEmailsController extends Controller
{

    public function runCommand()
    {
        return $this->getAllLostWill();
    }

    protected function getAllLostWill()
    {
        try {
            $status = false;
            $newMailSetting = new ChangeMailSettings();

            $getToday = Carbon::now()->format('Y-m-d');
            $getLast7day = Carbon::now()->subDays(7)->format('Y-m-d');
            $lostwill = LostWill::select('id', 'lost_first_name', 'lost_last_name', 'lost_dod', 'user_id')->whereBetween('created_at', [$getLast7day, $getToday])->get();

            if (!$lostwill->isEmpty()) {
                $listOfEmails = $this->getEmailList($lostwill->pluck('user_id'));

                $newMailSetting->sendMail();
                $listChunkEmail = array_chunk($listOfEmails['emails'], 550, false);
                $i = 0;
                $countSentMail = 0;
                while ($i < count($listChunkEmail)) {
                    Notification::route('mail', 'info@lostwillregister.com.au')->notify(new SendLostwillMarketingEmail($listChunkEmail[$i], $lostwill));
                    $countSentMail = ($countSentMail + count($listChunkEmail[$i]));
                    $i++;
                }
                //$listChunkEmail[$i]
                Log::channel('marketing_mail')->info('total email sent:' . $countSentMail);
                $newMailSetting->setDefault();
                $status = true;
            } else {
                $status = false;
                Log::channel('marketing_mail')->info("lost will not registers for yesterday's date");
            }
            return $status;
        } catch (Exception $e) {
            return false;
            Log::channel('marketing_mail')->info($e->getMessage());
        }
    }

    protected function getEmailList($except_email)
    {
        $emailList = [];

        $getAllLawyers = DB::table('marketing_lawyers')->where('isSubscribe', '1')->select('email')->get();
        $allUsers = User::select('email')->where('isSubscribe', '1')->whereNotIn('id', $except_email)->get();

        $emailList['emails'] = array_merge($allUsers->pluck('email')->toArray(), $getAllLawyers->pluck('email')->toArray());

        return $emailList;
    }

    protected function unsubscribeEmails(Request $req)
    {
        $email = trim($req->input('email'));
        try {

            $list1 = DB::table('marketing_lawyers')->where('email', $email)->first();
            $list2 = User::where('email', $email)->first();

            if ($list1) {
                DB::table('marketing_lawyers')
                    ->where('email', $email)
                    ->update(['isSubscribe' => false]);
            }

            if ($list2) {
                User::where('email', $email)
                    ->update(['isSubscribe' => false]);
            }

            Log::channel('marketing_mail')->info('unsubscribed email: ' . $email);
            return view('mail.unsubscribe.unsubscribe_view_page');
        } catch (Exception $e) {
            Log::channel('marketing_mail')->info('unsubscribed Error for email: ' . $email);
            return view('mail.unsubscribe.unsubscribe_view_page');
        }
    }

    protected function testing($newMailSetting, $lostwill)
    {
        $newMailSetting->sendMail();
        Notification::route('mail', 'palash.patel@computit.com.au')->notify(new SendLostwillMarketingEmail(['palash.patel@computit.com.au', 'hm@computit.com.au'], $lostwill));
        $newMailSetting->setDefault();
        dd(true);
    }
}

ChangeMailSettings

<?php

namespace App\MailAction;

use Mail;
use Swift_SmtpTransport;
use Swift_Mailer;

class ChangeMailSettings
{

    protected $backup;

    // store default mail settings
    protected function getCurretnMailSettings(){
        $this->backup = Mail::getSwiftMailer();
    }

    // set new email configuration for sendGrid
    //set secont email settings into env file and use
    protected function sendGridMailConfig(){    

        $transport = new Swift_SmtpTransport(env('MAIL_HOST_2'), env('MAIL_PORT_2'), env('MAIL_ENCRYPTION_2'));
        $transport->setUsername(env('MAIL_USERNAME_2'));
        $transport->setPassword(env('MAIL_PASSWORD_2'));        
        $new_mail_config = new Swift_Mailer($transport);
        Mail::setSwiftMailer($new_mail_config);
    }

    //create function to send mail and use into another controller
    public function sendMail(){
        $this->getCurretnMailSettings();
        $this->sendGridMailConfig();
    }
    
    //after sending mail set it to default configuration
    public function SetDefault(){
        Mail::setSwiftMailer($this->backup);
    }

}
miken32
  • 42,008
  • 16
  • 111
  • 154
user022yufjb
  • 157
  • 1
  • 3
  • 11

3 Answers3

0

The error message clearly says that you are calling method isEmpty() on an array at line 34 (which is of course not allowed):

Symfony\Component\Debug\Exception\FatalThrowableError : Call to a member function isEmpty() on array

  at /home/customer/www/lostwillregister.com.au/app/Http/Controllers/SendLostwillEmailsController.php:34
  30|             $lostwill = LostWill::select('id', 'lost_first_name', 'lost_last_name', 'lost_dod', 'user_id')->whereDate('created_at', $yesterdayDate)->get();
  31| 
  32|             if (!$lostwill->isEmpty()) {
  33|                 $listOfEmails = $this->getEmailList($lostwill->pluck('user_id'));
> 34|                 if (!$listOfEmails['emails']->isEmpty()) {
  35|                     $newMailSetting->sendMail();
  36|                     $listChunkEmail = array_chunk($listOfEmails['emails'], 550, false);

To test if an array contains elements, you can use count():

if(count($listOfEmails['emails']) > 0)
Olivier
  • 13,283
  • 1
  • 8
  • 24
  • That would still throw a notice/warning if `$listOfEmails['emails']` were not set or not an array – miken32 Jun 27 '23 at 18:46
0

You need to replace

if (!$listOfEmails['emails']->isEmpty()) {

by

if (!empty($listOfEmails['emails'])) {
Seb33300
  • 7,464
  • 2
  • 40
  • 57
-1

Try replacing !$lostwill->isEmpty() with !empty($lostwill)

Xander-Nova
  • 129
  • 2