Foreach只接收最后一个元素

I want a mail to be sent to all users in an array but it is only sent to the last user.

There must be something wrong with the foreach but I can't find what's causing this:

public function bulkmailAction()
{
    $users    = $this->getUserTable()->fetchAll();
    $projects = array();
    foreach ($users as $usersList)
    {
        $projects[$usersList->id] = $this->getProjectTable()->fetchJoin("projectuser", "projectuser.project=project.id", "user=$usersList->id");
    }
    $this->view->projects = $projects;
    $user = new Mailing();
    $form = new BulkMailForm($user);
    $form->get("submitbutton")->setValue("Send mail");
    $this->view->form = $form;
    $form->setInputFilter($user->getInputFilter());
    $form->setData(array_merge_recursive($this->getRequest()->getPost()->toArray(),$this->getRequest()->getFiles()->toArray()));
    if ($this->getRequest()->isPost()) {
        if ($form->isValid()) {
            foreach ($users as $usersList) {
                $formData = $form->getData();

                //redirect immediately without showing a view
                $subject = $formData["subject"];
                $message = $formData["message"];
                if ($message != strip_tags($message)) {
                    $message = \BOZA\Plugins\Functions::relToAbs($message);
                }
                $user->setSent(0);
                $user->setUser($usersList->id);
                $user->setName($usersList->name);
                $user->setSurname($usersList->surname);
                $user->setDate("0000-00-00");
                $user->setEmail($usersList->email);
                $user->setSubject($subject);
                $user->setHtml(true);
                $user->setMessage($message);
                $this->getTable("Mailing")->saveMailing($user);
                $this->view->setTerminal(true);
            }
        }
        $this->cache->success = "Mailing «".$user->toString()."» was successfully saved";
        return $this->redirect()->toRoute('admin', array('controller' => 'mailing', 'action' => 'index'));
    }
    ...

You create the view as BulkMailForm form with one Mailing but you set the user multiple times and finally to the last one.

The Mailing is sent to the user set last. This is the standard bevaior. You need to add multiple recipients and not set a single one and/or create multiple mainlings, one per each user.

As you're using object in your code that are undefined, it's hard to give more specific guidance in an answer.