Support the ongoing development of Laravel.io →
posted 8 years ago
Mail

Hello,

I have a contact form, a contact mailable, and a contact mail view and a contact controller... in the contact controller I have the to email as a variable because I want to receive the emails and the from email will be the visitors of the page. However in the to method of the Mail class I got this error:

FatalThrowableError in Mailable.php line 382: [] operator not supported for strings

Here is the code

ContactController

<?php

namespace App\Http\Controllers\pages;

use Mail;
use Toastr;
use App\Mail\Contact;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\ContactFormRequest;
use App\Http\Controllers\Controller;


class ContactController extends Controller
{
    public function store(ContactFormRequest $request)
    {
        $to = 'myemail@email.com';
        Toastr::success('Gracias por ponerse en contacto con nosotros'); 
        Mail::to($to)->send(new Contact($request->email, $request->subject, $request->message));
        return back();
    }
}

Contact Mailable

<?php

namespace App\Mail;

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

class Contact extends Mailable
{
    use Queueable, SerializesModels;


    public $from;
    public $subject;
    public $message;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->from)
                    ->view('emails.contact');
    }
}

Contact View

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  Buenas

  Ha recibidio un correo de: {{ $from }}
  Asunto: {{ $subject }}
  Mensaje: {{ $message }}  
</body>
</html>
Last updated 2 years ago.
0

Solved, you cant use a message variable because the mail classes use it.

nnaemeka liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.