0

I´m trying to send an email confirmation when a contact form is submitted. So, I have a controller to do it and all it works fine. The only problem is due to a textarea field which is making problems to me. This is the output problem:

[previous exception] [object] (TypeError(code: 0): htmlspecialchars(): Argument #1 ($string) must be of type string, Illuminate\\Mail\\Message given at D:\\Proyectos\\PÁGINA PERSONAL\\profesional_website\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php:118)

So, I'm debugging to check variable type and I checked that it was a string. I tried to parse such as strval($message) and concatenating such as trim($message." ") but it didn't work.

I attach controller code:

class EmailController extends Controller
{
    function sendEmail (Request $request) {
        Log::debug(gettype($request->description));
        $formData = array(
            'name' => $request->name,
            'last_names' => $request->last_names,
            'mail'=> $request->email,
            'message'=> $request->description,
            'phone' => $request->phone
        );
        Mail::to($formData['mail'])->send(new NotifyMail($formData));
     
        if (Mail::failures()) {
            $response['success'] = false;
            $response['message'] = 'Ha ocurrido un error';
            return $response;
        }else{
            $response['success'] = true;
            $response['message'] = 'Mensaje enviado correctamente';
             return $response;
        }
    }
}

And the blade view:

<div style="background-color:#ffcc66;text-align:center;padding:30px 0">
    <h1 style="color: #fff;">¡Bienvenido a alexdevs, {{$name}}!</h1>
    <p style="color: #fff;">Si este es tu problema:</p>
    <p style="color: #fff;padding:20px 50px">{{$message}}</p>
    <p style="color: #fff">¡Encontraremos una solución perfecta!</p>
    <p style="color: #fff">Me pondré en contacto contigo lo antes posible a través de tu correo: {{$mail}}</p>
    <h3 style="color: #fff;">¡Muchas gracias!</h3>
    <img src="{{asset('img/negative_rounded_brand.png')}}" alt="alexdevs_logo"
        style="height:50px;width:50px;margin-top:30px">
</div>

And this is the Mailable class:

class NotifyMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.client', $this->data);
    }
}
alexdevs
  • 5
  • 3
  • Don't use `$message` variable, change this variable to any other name, this will solve the problem. Laravel automatically makes the `$message` variable available to all of your email templates – STA Feb 05 '22 at 17:25
  • I can't belive @sta haha it worked ! Are there any reserved variable with that name to emails in laravel? – alexdevs Feb 05 '22 at 17:27
  • There are no any reserved variable for email template. – STA Feb 05 '22 at 17:28
  • So, why doesn't work with that name? – alexdevs Feb 05 '22 at 17:30
  • 1
    Does this answer your question? [Laravel mail - no message body](https://stackoverflow.com/questions/65004606/laravel-mail-no-message-body) – STA Feb 05 '22 at 17:30
  • https://laravel.com/docs/8.x/mail#inline-attachments – STA Feb 05 '22 at 17:31

0 Answers0