9

I have to update from 5.1 to 5.4

This was code for mail with`5.1

Mail::queue('emails.welcome_client', compact('user', 'userPassword'), function ($message) use ($user, $adminEmails) {
    $message->subject('Welcome to Enterprise Solutions!');

    $message->from(env('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'));
    $message->to($user->email);
    foreach($adminEmails as $adminEmail) {
        $message->bcc($adminEmail);
    }
});

I have to change from Laravel 5.1 to 5.4 so I create object mail

here it is

<?php

namespace App\Mail;

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

class ClientMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $user;
    // protected $content;

    public function __construct($user)
    {
        $this->content = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from(('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'))
            ->subject('Welcome to Enterprise Solutions!')
            ->view('emails.welcome_client');
    }
}

and in controller I do this

Mail::to($user->email)
    ->bcc($adminEmail)
    ->queue(new ClientMail($adminEmails)); 

when I try to run I get this error: Undefined $adminEmail. How I can fix this problem?

Dees Oomens
  • 4,554
  • 3
  • 29
  • 61
flower
  • 989
  • 4
  • 16
  • 34

1 Answers1

7

Try this one:

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

class ClientMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->from(('MAIL_FROM_ADDRESS'), env('MAIL_FROM_NAME'))
            ->subject('Welcome to Enterprise Solutions!')
            ->view('emails.welcome_client');

        return $this;
    }
}

And in Controller call: Mail::to($user->email)->bcc($adminEmails)->queue(new ClientMail());

smbdevin
  • 172
  • 1
  • 5
  • I got Undefined variable: user – flower Jul 29 '17 at 21:08
  • @flower change $user->email to string email like your_email@domain.com – azwar_akbar Feb 19 '20 at 06:36
  • ```Mail::to()->bcc()```, both require User object and collection of Objects respectively as Laravel extracts name and email itself. It should've been ```Mail::to($user)->bcc($admins)->queue(new ClientMail());``` – GoharSahi Oct 23 '20 at 21:00