laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.2k stars 10.89k forks source link

if clauses not working in mailable #15820

Closed schel4ok closed 7 years ago

schel4ok commented 7 years ago

in Laravel 5.1 I sent message like this. So user could send message with or without attachment.

                    Mail::send('emails.contacts', $mailarray, function($message) use ($mailarray) {
                        $message->from($mailarray['email'], $mailarray['name'] );
                        $message->to('admin@site.ru', 'admin');
                        $message->setCc($mailarray['email'], $mailarray['name']);
                        $message->replyTo($mailarray['email'], $mailarray['name'] );
                        $message->subject('Письмо со страницы Контакты');

                        if ( isset($mailarray['attachment']) ) 
                        {
                            $message->attach($mailarray['attachment']->getRealPath(), array(
                                'as'    => $mailarray['attachment']->getClientOriginalName(), 
                                'mime'  => $mailarray['attachment']->getMimeType()));
                        }
                    });

This approach is not working with Mailable. Doing this I get error

syntax error, unexpected 'if' (T_IF)

It is working only like this, which is duplicating code a lot.

    public function build()
    {
        if (is_null($this->file) )  {
            return $this->subject('Обратный звонок с сайта')
                        ->view('emails.callback')
                        ->cc($this->input['email'], $this->input['name'])
                        ->replyTo($this->input['email'], $this->input['name']);
        }
        else {
            return $this->subject('Обратный звонок с сайта')
                        ->view('emails.callback')
                        ->cc($this->input['email'], $this->input['name'])
                        ->replyTo($this->input['email'], $this->input['name'])
                        ->attach($this->file, array('as' => $this->file->getClientOriginalName(), 'mime' => $this->file->getClientMimeType() ));
        }
    }

Steps To Reproduce:

GrahamCampbell commented 7 years ago

syntax error, unexpected 'if' (T_IF)

That indicates your code has a syntax error. Nothing to do with us. Please ask on the forums if you can't work it out.