tomasosho
4 months ago
549
4
General

I am trying to send mail automatically from my controller

Posted 4 months ago by tomasosho

will the code execute?

My Controller

public function mail()
    {
        $alert = Items::whereColumn('number', '<=', 'threshhold')->get();
        $notification = Maintenance::whereBetween('service_date', [today(), today()->addDays(5)])->get();
        
        if ($alert->count() >= 1 or $notification->count() >= 1) {
            foreach ($alert as $a){
                $alerts = $a->title .'for'. $a->title .'has'. $a->number .'left, which is below the Threshold of'. $a->threshhold .'Please restock quickly';
            }
            foreach ($notification as $n){
                $notifications = $n->machine_title. $n->description .'is due for servicing on'. $n->service_date. 'Thank You!';
            }

            Mail::to('email address')->send(new SendMailable($alerts, $notifications));
        }
    }

SendMailable

<?php

namespace App\Mail;

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

class SendMailable extends Mailable
{
    use Queueable, SerializesModels;
    public $name;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($name)
    {
        $this->notifications = $notifications;
        $this->notifications = $notifications;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('/');
    }
}

Please sign in or create an account to participate in this conversation.