我想發送一封電子郵件,如果它有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個不同的郵件控制器解決了這個問題,但我想知道爲什麼這個解決方案不起作用。在此先感謝
什麼是您的PHP版本?因爲你使用的短陣列語法只適用於5.4以上的 – Amarnasan
,所以在函數buildRecipients的第二個foreach循環之前,先檢查一下(isset($ this - > {$ type})etc等)。因爲我沒有在你的粘貼代碼中看到任何'to'或'replyTo'屬性。 – JoshulSharma
@Amarnasan 7.0.4 – prgrm