8

So i'm trying to code a simple website form. But it has this htmlspecialchars error.

I've tried to make {{ $message }} but it didn't work. has the same error.

this is my controller :

<?php
namespace App\Http\Controllers;
use Mail;
use Illuminate\Http\Request;
class ContactMessageController extends Controller
{
public function create()
    {
        return view('form');
    }

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'address' => 'required',
    ]);

    Mail::send('emails.contact-message', [
        'message' => $request->message
    ], function($mail) use($request) {
        $mail->from($request->email, $request->name);

        $mail->to('john@example.com')->subject('Contact message');
    });

        return redirect()->back()->with('flash_message', 'thanks');
    }
}

and this is my blade

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Customer Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" integrity="sha384-PDle/QlgIONtM1aqA2Qemk5gPOE7wFq8+Em+G/hmo5Iq0CCmYZLv3fVRDJ4MMwEA" crossorigin="anonymous">
<style>
    .invalid-feedback {
        display: block;
    }
</style>
</head>
<body>
<div class ="container">
    <h1>Customer Form</h1>
        @if (Session::has('flash_message'))
            <div class="alert alert-success">{{ Session::get('flash_message') }}</div>
        @endif
    <form method="post" action="{{ route('contact.store') }}">
    {{ csrf_field() }}
        <div class="form-group">
            <label>Full Name : </label>
            <input type="text" class="form-control" name="name">
            @if ($errors->has('name'))
                <small class="form-text invalid-feedback">{{ $errors->first('name') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Email : </label>
            <input type="text" class="form-control" name="email">
            @if ($errors->has('email'))
                <small class="form-text invalid-feedback">{{ $errors->first('email') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Address : </label>
            <textarea name="address" class ="form-control"></textarea>
            @if ($errors->has('address'))
                <small class="form-text invalid-feedback">{{ $errors->first('address') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Message : </label>
            <textarea name="message" class ="form-control"></textarea>
            @if ($errors->has('message'))
                <small class="form-text invalid-feedback">{{ $errors->first('message') }}</small>
            @endif
        </div>

        <button class="btn btn-primary">Submit</button>


    </form>
</div>

</body>
</html>

and this is my contact-message.blade.php

{{ $message }}

also i've tried {{dd($message)}}

but it didnt work.

please help.

calvinerico
  • 121
  • 1
  • 2
  • 6

3 Answers3

30

Just change the array key from message to messages in your controller like below:

$data = array(
        'messages' => $request->message
        );

and also in the blade print it as {{$messages}}

A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a message variable in your view payload.

Check the note in this link: http://laravel.com/docs/5.0/mail#basic-usage

Md Riadul Islam
  • 1,273
  • 11
  • 25
  • Welcome @calvinerico . As you are new, just to let you know that if any answer works for you then you can accept it and accepting a perfect answer will add value to your profile. See details from here: https://stackoverflow.com/help/someone-answers – Md Riadul Islam Feb 13 '19 at 03:31
  • 1
    Thanks! This applies to Laravel 5.8 and this entry about the `$message` was removed from the documentation. Not sure why... – delroh Oct 28 '19 at 21:25
  • 1
    They should somehow say these kinds of stuff in the debug page. I searched everywhere and tried many methods to send data. Oh my god. – Akshay K Nair Sep 04 '20 at 07:07
  • 1
    Thanks for this, I was banging my head against the screen until I found this post and realised some reserved word BS was going on. Sigh. Thanks! – Snouto Oct 10 '21 at 02:53
1

also I fetch the same problem Just change it message to messages it works for me.

<?php

namespace App\Mail;

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

class ContactUserIncomingMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    public $name;
    public $email;
    public $phone;
    public $subject;
    public $message;

    public function __construct($name,$email,$phone,$subject,$message)
    {
        $this->name = $name;
        $this->email = $email;
        $this->phone = $phone;
        $this->message = $message;
        $this->subject = $subject;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $name = $this->name;
        $email = $this->email;
        $phone = $this->phone;
        $messages = $this->message;
        $subject = $this->subject;
        $subject = 'Website Customer Query';
        return $this->view('Mail.contact-user-incomeing-mail',compact('name','email','phone','messages','subject'))->subject($subject);
    }
}
Mithun Rana
  • 1,334
  • 1
  • 9
  • 10
-1

You should try this:

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'address' => 'required',
    ]);

    $fromName = $request->name;
      $subject = "MailSent";
      $data = array(
        'message' => $request->message
        );

      $fromEmail = $request->email;

      $toName = 'test';
      $toEmail = 'john@example.com';


      Mail::send('emails.contact-message', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){ 

        $message->from($fromEmail, $fromName);
        $message->to($toEmail, $toName);
        $message->subject($subject);
      });

        return redirect()->back()->with('flash_message', 'thanks');
    }
}
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57