Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ShirshaDebnath's avatar

Trying to get property 'email' of non-object

When I dd($user->email ) it is showing the necessary data but when I am Mail::to($user->email) it is giving me error

0 likes
42 replies
Martal's avatar

@shirshadebnath

What does normalizeRecipient function do? Don't you forget to return from that function? To use "return" keyword?

tisuchi's avatar

@shirshadebnath

Can you share your full BlogPublished.php code? Don't post image.

Use 3 tilde(`) sign before and after your code.

ShirshaDebnath's avatar

tried but still the same error , the error has been showing in the Mail\Mailable,php file on line:584

ShirshaDebnath's avatar

@tisuchi

i just made them back to protected

<?php

namespace App\Mail;

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

class BlogPublished extends Mailable implements ShouldQueue{
    use Queueable, SerializesModels;

    protected $blog;
    protected $user;

    public function __construct(Blog $blog, User $user)
    {
        $this->blog = $blog;
        $this->user = $user;
    }

    public function build()
    {
        $this->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'))
        ->subject('A new Blog has been published')
        ->view('emails.newblog')
        ->with([
            'user' => $this->user,
            'blog' => $this->blog,
        ]);

        return $this;
    }
}
tisuchi's avatar

@shirshadebnath Honestly I cannot any issue so far. Can you clear your cache and try.

If still same, can you try this in your store method in the controller-

$users = User::get();

foreach($users as $user){
    echo $user->email . "<br/>";
}

dd('');

I just want to see that what's returning there.

ShirshaDebnath's avatar

it is returning this:

public function store(Request $request){
        //validate
        $rules = [
            'title' => ['required', 'min:10', 'max:160'],
            'content' => ['required', 'min:102'],
        ];

        $this->validate($request, $rules);
        $input = $request->all();
        //meta-stuff
        $input['slug'] =uniqid() . Str::slug($request->title, '-');
        $input['meta_title'] = Str::limit($request->title, 55);
        $input['meta_description'] = Str::limit($request->content, 155);
        //image-upload
        if ($file = $request->file('featured_image')) {
            $name = uniqid() . $file->getClientOriginalName();
            $name = strtolower(str_replace(' ', '-', $name));
            $file->move(public_path('/image/featured_image/'), $name);    
            $input['featured_image'] = $name;
        }

        //$blog = Blog::create($input);
        $blogByUser = $request->user()->blogs()->create($input);
        //sync with categories
        if ($request->category_id) {
            $blogByUser->category()->sync($request->category_id);
        }
        //mail
        $users = User::all();
        foreach($users as $user){
            echo $user->email . "<br/>";
        }
        
        dd('');

        Session::flash('blog_created_message', 'Blog Created!');

        return redirect('/blogs');
    }

Martal's avatar

@shirshadebnath

Couldn't you add dd($address, $name, $property) on 578th line of Mailable.php and try to send mail again?

tisuchi's avatar

@shirshadebnath

Seems good to me. Can I see your email environment variables? Do you set up everything properly?

Martal's avatar

@shirshadebnath I see, that first $recipient is correct. But what about others? Please, change dd($recipient) to dump($recipient).

ShirshaDebnath's avatar

@martal

i did dump($recipient) but first it showed the output for a second and then redirected to error page and gave me the same error. therefore to show the output i got i dd($recipient)

thanks for patience till now

Martal's avatar

@shirshadebnath

Oh. But you can log it. Try logger('RECIPIENT', [$recipient]); and explore storage/logs/laravel.log

ShirshaDebnath's avatar

[2019-12-14 08:21:56] local.DEBUG: RECIPIENT [{"stdClass":{"email":"kirito.sd307@gmail.com"}}] [2019-12-14 08:21:56] local.DEBUG: RECIPIENT [null] [2019-12-14 08:21:56] local.ERROR: Trying to get property 'email' of non-object {"userId":1,"exception":"[object] (ErrorException(code: 0): Trying to get property 'email' of non-object at E:\dev\Laravel_projects\soporific_mind\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:585) [stacktrace]

Next

Please or to participate in this conversation.