Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 Mailer Trying to get property of non-object

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.

like image 349
Bogart Avatar asked Jul 19 '15 13:07

Bogart


1 Answers

I suggest the problem is here:

 $message->to(**$to->email**)->subject($subject)->from($from);

The $to is a string of an email address judging by this:

 $to = env('MAIL_NOREPLY', 'SUPPORT');

So simply get rid of the ->email bit as a string does not have attributes/properties, for example:

 $message->to($to)->subject($subject)->from($from);
like image 150
NaN Avatar answered Oct 04 '22 04:10

NaN