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

Getting ErrorException in Mailer.php line 33: Trying to get property of non-object

my Mailer.php:

<?php

namespace App\Mailers;

use Illuminate\Contracts\Mail\Mailer as Mail;

abstract class Mailer
{
    /**
     * @var Mail
     */
    protected $mail;

    /**
     * @param Mail $mail
     */
    public function __construct(Mail $mail)
    {
        $this->mail = $mail;
    }

    /**
     * @param $to
     * @param $subject
     * @param $from
     * @param $view
     * @param null $data
     */
    public function mailTo($to, $subject, $from, $view, $data = null)
    {
        $this->mail->send($view, $data, function($message) use ($to, $from, $subject)
        {
            $message->to($to->email)->subject($subject)->from($from);
        });
    }
}

My SiteMailer.php that extends my Mailer.php abstract class

<?php

namespace App\Mailers;

class SiteMailer extends Mailer
{
    /**
     * @param $data
     */
    public function sendEmailMessageToSupport($data)
    {
        $from = env('MAIL_NOREPLY', 'SUPPORT');
        $to = env('MAIL_NOREPLY', 'SUPPORT');
        $subject = 'Activate Your Account';
        $view = 'auth.emails.support';

        $this->mailTo($to, $subject, $from, $view, $data);
    }
}

And my SupportController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Mailers\SiteMailer;
use App\Http\Controllers\Controller;
use App\Http\Requests\SupportRequest;

class SupportController extends Controller
{
    public function create()
    {
        return view('pages.support');
    }

    public function store(SupportRequest $request, SiteMailer $mail)
    {
        $mail->sendEmailMessageToSupport($request->all());

        return redirect()->back()->with('alert-success', 'Thanks for contacting us!');
    }
}

My SupportRequest FormRequest

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class SupportRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'message_content' => 'required',
        ];
    }
}

And then the email view

<p>
    A prospective customer named {{ $name }} <small>{{ $email }}</small>
    has submitted an inquiry through Our Site.
</p>

<p>
    {{ $message_content }}
</p>

Can't find where the problem is.

Last updated 3 years ago.
0

on that line you use $to->email but your passing a value from env(). So $to isn't an object.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

BebzLoveKo bebzloveko Joined 19 Jul 2015

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.