Reputation: 1340
I have a form in which I get to, from, subject and message as input. On submit, it sends an email to the specified client (to).
I created a Mailable and in controller doing something like
Mail::to($request['to'])->send(new SendMail($request['from'], $request['subject'], $request['message']));
Here is my Mailable _constructor
public function __construct($from, $sub, $msg)
{
$this->from = $from;
$this->sub = $sub;
$this->msg = $msg;
}
Here is the build method
public function build()
{
return $this->from($address = $this->from, $name='Company Support')
->subject($this->sub)
->view('emails.sendmail');
}
In my log file I am getting an error like
[] operator not supported for strings {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): [] operator not supported for strings at C:\xampp\htdocs\shopvel_\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:582) ....
I can't figure out where I am going wrong. I had tried many solutions but it didn't work. For example, removing from in build method then it shows
local.ERROR: Illegal string offset 'address' {"exception":"[object] (ErrorException(code: 0): Illegal string offset 'address' at C:\xampp\htdocs\shopvel_\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:318)
EDIT 1: Here is my email view
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Mail</title>
</head>
<body>
{{ $msg }}
</body>
</html>
EDIT 2: When I pass two variables in the constructor
method it is working fine like
Mail::to($request['to'])->send(new SendMail($request['from'], $request()->all()));
Is there any limit for passing the variable in the constructor method??
(I don't think so since constructor can take any number of variables)
Upvotes: 3
Views: 1287
Reputation: 71
The $from parameter in your constructor is clashing with (and overwriting) the $from public property in the Mailable class.
public function __construct($from, $sub, $msg)
Change it to something like this...
public function __construct($fromAddress, $sub, $msg)
and it will work fine
Upvotes: 5