dongping1689 2017-05-04 08:53
浏览 6
已采纳

在发送电子邮件时建立收件人让我与Laravel例外

I am trying to send an email and if it has cc, it will be sent with cc.

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("my@email.io")
        ->subject($this->subject)
        ->cc($this->cc)
        ->attach(storage_path() . "/app/public/files/" . $this->user_id . "/" . $this->filename)
        ->view('emails.sent');
    }else{
        return $this->from("my@email.io")
            ->subject($this->subject)
            ->attach(storage_path() . "/app/public/files/" . $this->user_id. "/" .$this->filename)
            ->view('emails.sent');
    }
}

This controller worked flawlessly before I introduced the cc logic, but after I added the if else to the build function, Laravel sends me this error:

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

Which is this function:

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

        return $this;
    }

I have tried both with and without adding $cc when calling a function, and it always gives me the same error. As I said this worked before I introduced the if else.

I already solved this by creating 2 different Mail controllers, but I would like to know why this solution doesn't work. Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongzhuang5741 2017-05-04 09:28
    关注

    I actually renamed the variable $cc to $mycc and it works again.

    Since the class extended Mailable, and Mailable already has a variable called $cc, which is supposed to be an array, my $cc = null overwrote the variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集