2017-05-04 26 views
1

我想發送一封電子郵件,如果它有cc,它將與cc一起發送。在發送電子郵件的同時收發郵件給我的例外與Laravel

public $content; 
public $subject; 
public $filename; 
public $user_id; 
public $cc; 

public function __construct($content,$subject,$filename,$user_id,$cc = null) 
{ 

    $this->content = $content; 
    $this->subject = $subject; 
    $this->filename = $filename; 
    $this->user_id = $user_id; 
    $this->cc = $cc; 
} 

/** 
* Build the message. 
* 
* @return $this 
*/ 
public function build() 
{ 
    if($this->cc){ 
    return $this->from("[email protected]") 
     ->subject($this->subject) 
     ->cc($this->cc) 
     ->attach(storage_path() . "/app/public/files/" . $this->user_id . "/" . $this->filename) 
     ->view('emails.sent'); 
    }else{ 
     return $this->from("[email protected]") 
      ->subject($this->subject) 
      ->attach(storage_path() . "/app/public/files/" . $this->user_id. "/" .$this->filename) 
      ->view('emails.sent'); 
    } 
} 

該控制器工作得十分完美之前,我公司推出的CC邏輯,但我加入了,如果別的構建功能後,Laravel送我這個錯誤:

ErrorException in Mailable.php line 241: Invalid argument supplied for foreach()

這是本功能:

protected function buildRecipients($message) 
    { 
     foreach (['to', 'cc', 'bcc', 'replyTo'] as $type) { 
      foreach ($this->{$type} as $recipient) { 
       $message->{$type}($recipient['address'], $recipient['name']); 
      } 
     } 

     return $this; 
    } 

我已經嘗試過和沒有添加$ cc時調用一個函數,它總是給我同樣的錯誤。正如我所說,這之前我介紹瞭如果其他。

我已經通過創建2個不同的郵件控制器解決了這個問題,但我想知道爲什麼這個解決方案不起作用。在此先感謝

+0

什麼是您的PHP版本?因爲你使用的短陣列語法只適用於5.4以上的 – Amarnasan

+1

,所以在函數buildRecipients的第二個foreach循環之前,先檢查一下(isset($ this - > {$ type})etc等)。因爲我沒有在你的粘貼代碼中看到任何'to'或'replyTo'屬性。 – JoshulSharma

+0

@Amarnasan 7.0.4 – prgrm

回答

0

我實際上將變量$ cc重命名爲$ mycc,並且它再次工作。

由於類擴展Mailable,並且Mailable已經有一個名爲$cc的變量,它應該是一個數組,我的$cc = null重寫了這個變量。

0

ErrorException in Mailable.php line 241: Invalid argument supplied for foreach()

這意味着傳遞給foreach的參數不是數組。根據你的代碼,這意味着'to','cc','bcc','replyTo'屬性之一是空的或者不是數組。我建議在foreach之前添加is_array檢查。

謝謝。

相關問題