在Laravel 5.1中,我发送了这样的消息。因此,用户可以发送带或不带附件的消息。
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()));
}
});
这种方法不适用于Mailable。这样做我得到错误
syntax error, unexpected 'if' (T_IF)
它只是这样工作,这很复制代码。
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() ));
}
}