Pass a variable into a data array to send email

Posted 1 year ago by almost_pitt

Hi guys,

I'm trying to put a contact form into a site where users can post. I would like the email to be sent to the posters.

I can show the poster's email address on the page using blade,

{{$posts->user_email}}

However, when I try to put this into my sendMail function, it cannot read find the email address.

public function sendEmail(Request $request)
{
    $posts = Post::all();
    $postersemail = $posts->user_email;

    $this->validate($request, [
        'email' => 'required|email',
        'subject' => 'min:3',
        'message' => 'min:10'
        ]);

    $data = array(
        'email' => $request->email,
        'subject' => $request->subject,
        'bodyMessage' => $request->message,
        'postersemail' => $postersemail
        );

    Mail::send('emails.contact', $data, function($message) use ($data){
        $message->from($data['email']);
        $message->to( $data['postersemail'] );
        $message->subject($data['subject']);
    });

    return redirect()->route('home');
}

When I do this, I get the following error:

(1/1) Exception Property [user_email] does not exist on this collection instance.

Any idea why this function cannot find the user_email, but blade can?

Thanks!

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

Reply to

Use Markdown with GitHub-flavored code blocks.