Laravel Version:5.3 PHP Version: 7.0 Database Driver & Version: MySQL 5.7 Description:
I'm running into this problem when trying to set the sender (not to) and I end up getting an error that looks like this
Symfony\Component\Debug\Exception\FatalThrowableError: [] operator not supported for strings in C:\wamp\www\enterprise\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:384
even though I'm entering an email string It is conflicting with the system email address that I have setup in mail.php.
Nowhere else in the system do I get errors when trying to set email to send from a different email address. I'm trying to send it from the authorized user email
So, in my mailable class, I'm using
public function build()
{
return $this->from(Auth::user()->email)
} ->view('mailbox.sendmail');
But in my mail.php
'from' => [
'address' => '[email protected]',
'name' => 'Swell Systems',
],
What is the solution?
This is my Mailable -- UserEmail class. I'm using it to send queued user emails. I have a few dependencies I'm getting from a $request
<?php
namespace App\Mail;
use Auth;
use DB;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserEmail extends Mailable
{
use Queueable, SerializesModels;
public $from, $subject, $content;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($from, $subject, $content)
{
//
$this->from = $from;
$this->subject = $subject;
$this->content = $content;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from($this->from)
->view('mailbox.sendmail')
->subject('Subject:' . $this->subject)
->with(['body' => $this->content])
}
I call it from a controller
namespace App\Http\Controllers;
use App\Mail\UserEmail;
use Auth;
use Mail;
$from = Auth::user()->email;
$subject = $request->input( 'subject' );
$content = $request->input( 'content' );
Mail::to($request->to)->later($when, (new UserEmail($from, $subject, $content))->onQueue('emails'));
This is the exception given it throws in more detail
Symfony\Component\Debug\Exception\FatalThrowableError: [] operator not supported for strings in C:\wamp\www\enterprise\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:384
Stack trace
1 . C:\wamp\www\enterprise\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php(312): Illuminate\Mail\Mailable->setAddress('nmorgan@designl...', NULL, 'from') 2 . C:\wamp\www\enterprise\app\Mail\UserEmail.php(51): Illuminate\Mail\Mailable->from('nmorgan@designl...') --[internal function]: App\Mail\UserEmail->build() 3 . C:\wamp\www\enterprise\vendor\laravel\framework\src\Illuminate\Container\Container.php(508): call_user
If I comment out from in the class the email is sent from the system(mail.php). Otherwise, it throws the error
Would it have to do with anything in my setup? Is there something I am missing.
Mail.php
'from' => [
'address' => '[email protected]',
'name' => 'System',
],
I found this posted 9 months ago on Laravel.io forum but I don't have a message variable.
<https://laravel.io/forum/10-05-2016-mailablephp-line-382-operator-not-supported-for-strings>
I'm positive that $this->from or Auth::user()->email is definitely a string.. I dumped it and it is "[email protected]" but I removed it all together and put '[email protected]' and got the same error