Framework: Mailable bug

Created on 14 Jul 2017  路  10Comments  路  Source: laravel/framework

  • Laravel Version:5.3
  • PHP Version: 7.0
  • Database Driver & Version: MySQL 5.7

Description:

https://github.com/laravel/framework/issues/15227

I'm referencing this thread below even though it is closed. I'm running through the same problem when trying to set the the sender (not to) and I end up getting an error that looks like this

Symfony\Component\DebugException\FatalThrowableError: [] operator not supported for strings in C:\wamp\www\enterprise\vendor\laravelframeworksrc\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.

No where 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?

Most helpful comment

@nivanmorgan I've faced the same. The problem is that you are using $from variable which is used in extended Mailable class. Change it either to $senderEmail or something. And check other duplicate variables.

All 10 comments

I cannot reproduce this in 5.3.31. Can you provide more details?

My setup:
app/Mail/Foo.php

<?php

namespace App\Mail;

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

class Foo extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->from('[email protected]')
                    ->to('[email protected]')
                    ->view('foo');
    }
}

config/mail.php

//

'from' => [
    'address' => '[email protected]',
    'name' => 'Foo',
],

//

Other than that, the default Laravel 5.3 setup.

When I comment the from line in the mailable, mail comes from Foo <[email protected]>
image
With from line not commented it comes from <[email protected]> as expected:
image

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:

#0 C:\wamp\www\enterprise\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php(312): Illuminate\Mail\Mailable->setAddress('nmorgan@designl...', NULL, 'from')
#1 C:\wamp\www\enterprise\app\Mail\UserEmail.php(51): Illuminate\Mail\Mailable->from('nmorgan@designl...')
#2 [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

Sorry but 5.3 is not supported anymore, feel free to continue the discussion if you want.

Same issue in 5.4

I updated my system to Laravel 5.4 and sadly I'm getting the same exact error .. there is another comment here saying it is doing the same with him. I also referenced another issue that was never resolved. please reopen this and help us solve it. @themsaid

works now with mailgun instead of smtp.gmail.com

setting $this->from($email, $name) is still a bug in 5.5. I don't understand why

@nivanmorgan I've faced the same. The problem is that you are using $from variable which is used in extended Mailable class. Change it either to $senderEmail or something. And check other duplicate variables.

Thanks @niksynov , I was facing this issue from last 7 days and not understand whats wrong, but it was this silly mistake of duplicating original variables. Thanks, your solution worked and fixed my big problem :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainSauvaire picture RomainSauvaire  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

ghost picture ghost  路  3Comments

shopblocks picture shopblocks  路  3Comments