0

I am trying to send the data from a Contact form to email, but I am getting the following error htmlspecialchars() expects parameter 1 to be string, object given. Given below is my code. This is from a blog I found trying to get this done. I followed all the steps mentioned there.

ContactController.php

 public function index() {
        return view('front.contact');
    }

    public function storeForm(Request $request) {
        $this->validate($request, [
           'name' => 'required',
           'email' => 'required|email',
           'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
           'message' => 'required'
        ]);

        contact::create($request->all());

        \Mail::send('email', array(
            'name'=> $request->get('name'),
            'phone'=> $request->get('phone'),
            'email'=> $request->get('email'),
            'message'=> $request->get('message')
        ), function ($message) use ($request) {
            $message->from($request->email);
            $message->to('olindo.testing@gmail.com')->subject('Hello Admin | New Appointment');
        });

        return back()->with('success', 'Thank you for contacting us. We will get back to you soon.');

email.blade.php Contact.blade.php

<form action="" method="post" action="{{ route('contact.save') }}">
   @csrf
   <div class="kt-contact-form margin-top-60 form-group">
   <div id="message-box-conact"></div>
       <h3 class="title">Get in touch</h3>
       <p>
           <input id="name" type="text" placeholder="Your Name" name="name">
       </p>
       <p>
           <input id="email" type="text" placeholder="Your Email" name="email">
       </p>
       <p>
           <input id="phone" type="text" placeholder="Your Phone Number" name="phone">
       </p>
       <p>
       <textarea id="message" placeholder="Write msg here" name="message"></textarea>
       </p>
       <input type="submit" name="send" value="SEND REQUEST" class="button">
   </div>
</form>

model

class contact extends Model
{
    use HasFactory;

    public $fillable = [
        'name',
        'email',
        'phone',
        'message',
    ];
}
  • Change `$message` variable to another variable. Laravel automatically makes the `$message` variable available to all of your email templates – STA Aug 31 '21 at 15:03
  • @sta it did get rid of the error, I still can't get any email from the contact form. – ivanantonov Aug 31 '21 at 17:42
  • request->get() can return mixed content https://laravel.com/api/8.x/Illuminate/Http/Request.html change it to $request->name, $request->message etc. direct property of request. – Cameron Sep 01 '21 at 00:40

0 Answers0