HashtagForgotName
HashtagForgotName

Reputation: 671

laravel mail how to properly implement into the system

So I am trying to implement a command that notifies all users that are subscribes to an event with command that does an check every day. I was reading Laravel mail docs 7.x so there example is about order system where they send the mail with this peace of code

foreach (['[email protected]', '[email protected]'] as $recipient) {
    Mail::to($recipient)->send(new OrderShipped($order));
}

what as it looks takes the email of of the loop and then send an email toward that adress. So I made a mail class php artisan make:mail NotifyUserOfEvents and where I made this code

<?php

namespace App\Mail;

use App\Event;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class NotifyUserOfEvents extends Mailable
{
use Queueable, SerializesModels;

protected $event;

public function __construct(Event $event)
{
    $this->event = $event;
}

public function build()
{
    return $this->view('mails.NotifyUserOfEvents')
        ->with([
            'name' => $this->event->name,
            'date' => $this->event->settings->start_date,
        ]);
}
}

but when I try to call this class with this function

<?php

namespace App\Console\Commands;

use App\Event;
use App\RegistrationEvents;
use App\User;
use Carbon\Carbon; 
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class NotifyUsersForEvents extends Command
{
protected $signature = 'NotifyUsersForEvents';

protected $description = 'Notify the user for the event. test run with -> php artisan schedule:run';

public function __construct()
{
    parent::__construct();
}

public function handle()
{
    Log::debug('this works every minute');

    $events = Event::query()
        ->with('settings')
        ->has('settings')
        ->get();

    foreach ($events as $event) {
        $week = Carbon::now()->addWeek();
        $sixDays = $week->copy()->subDay();
        if (Carbon::create($event->settings->date_start)->between($week, $sixDays)) {
            $subscriptions = RegistrationEvents::query()
                ->where('event_id', $event->id)
                ->get();
            foreach ($subscriptions as $subscription) {
                var_dump($subscription->user_id);
                $user = User::findOrFail($subscription->user_id);

                Mail::to($user->email)->send($event);
                var_dump($user->email);
            }
        }
    }

  }
}

it returns this error: Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given, called in so do I need to change the way I call the mail class or do I need to add something to the Event Model?

also the event.php

use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Event extends Model implements Mailable
{
use SoftDeletes;

public function settings(){

    return $this->hasOne('App\EventSettings', 'event_id');

}
}

Upvotes: 0

Views: 621

Answers (4)

nandhini ram
nandhini ram

Reputation: 119

$user   = User::find($id);
$email  = $user->email;
if(Helper::isValidEmail($email))
                    {
                        Mail::send('emails.applicant_reference', 
$emailParameters, function($message) use ($email, $name, $subject){
                            $message->to($email, $name)
                            ->subject($subject);
                        });

Upvotes: 0

Alberto
Alberto

Reputation: 12939

Referring to this line:

Mail::to($user->email)->send(new Event($event));

you are creating a new Event passing to the constructor another Event... you probably never define a constructor that accept as first parameter an Event...
But despite that, what's the sense of doing this? To Mail::send you have to pass a Mailable, not an event, and i'm pretty sure you don't need a new event, so i believe you would want to do something like this:

use App\Mail\NotifyUserOfEvents; // or whatever namespace you have to the mail
Mail::to($user->email)->send(new NotifyUserOfEvents($event));

Upvotes: 1

nandhini ram
nandhini ram

Reputation: 119

$applicantName = null; $subject = " Application received for ".$applicantName; $emailParameters = ["applicantName" => $applicantName, "proposerName" => $proposerName, "seconderName" => $seconderName]; try {

                                Mail::send('emails.application', $emailParameters, function($message) use ($applicantName, $subject){
                                    $message->to(['[email protected]','[email protected]'], " Test Email Function ")
                                    ->subject($subject);
                                });


                        } catch (Exception $ex){ Log::error("UserController".$ex->getMessage());

                    }

Upvotes: -1

Ivanilov
Ivanilov

Reputation: 46

You must pass to send function an object of your NotifyUserOfEvents, not an Event object.

Try this:

Mail::to($user->email)->send(new NotifyUserOfEvents($event));

Upvotes: 2

Related Questions