Support the ongoing development of Laravel.io →
Mail Forms Database

my Controller

<?php

namespace App\Http\Controllers;

use App\Task;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Twilio\Rest\Client;
use App\Notifications\CustomEmail;
use Mail;
use App\Mail\ContactMail;

class EmailController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tasks = Task::paginate(15);
        return view('send.mail',compact('tasks',$tasks));
    }

    public function sendmail(Request $get)
    {
        $validatedData = $get->validate([
            'tasks' => 'required|array',
            "subject" => "required",
            "message" => "required",
        ]);
    
        $tasks = $validatedData["tasks"];
        
        foreach ($tasks as $task) {
            $mail = ($task->mail);
            $subject = $get->subject;
            $message = $get->message;

            Mail::to($mail)->send(new ContactMail($subject, $message) );
    }
        
    }
}

ContactMail.php

<?php

namespace App\Mail;

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

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;

    public $sub;
    public $mes;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $e_subject = $this->sub;
        $e_message = $this->mes;

        return $this->view('email.contactmail', compact("e_message"))->subject("$e_subject");
    }
}

Form

@extends('layouts.index')

@section('content')
            @if (session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
            @endif
            @if ($errors->any())
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif
                <div class="col">
                    <div class="card">
                        <div class="card-body">
                            <form method="POST" action="{{url('send/email')}}">
                                @csrf
                                <label><h3>Notify Contractors via EMail</h3></label>
                                <div class="form-group">
                                {{Form::label('subject', 'Subject')}}
                                    {{Form::text('subject', '', ['class' => 'form-control', 'placeholder' => 'Subject'])}}
                                </div>
                                <div class="form-group">
                                <label>Select Address</label>
                                    <select name="tasks[]" multiple class="form-control" >
                                        @foreach ($tasks as $task)
                                        <option>{{$task->mail}}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>Message</label>
                                    <textarea name="message" type="text" class="form-control" rows="3"></textarea>
                                </div>
                                <button type="submit" class="btn btn-primary">Send Notification</button>
                            </form>
                        </div>
                    </div>
                </div>

@endsection
Last updated 3 years ago.
0

What seems to be the problem ?

0

I get this error when i try to send the mail


ErrorException thrown with message "Trying to get property 'mail' of non-object"

Stacktrace:
#56 ErrorException in C:\xamppNew\htdocs\laravelapps\procurement\app\Http\Controllers\EmailController.php:37
#55 Illuminate\Foundation\Bootstrap\HandleExceptions:handleError in C:\xamppNew\htdocs\laravelapps\procurement\app\Http\Controllers\EmailController.php:37
#54 App\Http\Controllers\EmailController:sendmail in C:\xamppNew\htdocs\laravelapps\procurement\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
...
0
Solution

What happens when you do dump($task) inside the foreach does it have an email property (eg is it an object or an array)? or should you be calling it as $task['mail'] ?

0

Thanks...

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.