如何发送多封电子邮件?

时间:2017-01-09 08:41:51

标签: php email nette

我试图通过nette邮件程序向选定的用户发送电子邮件,但总的来说只有InvalidStateException

public function contactsEditorFormSucceeded($form, $values)
{
        try {
            $recipients = $values->recipients;
            $mail = new Message;                      
            $mail->setFrom($values->email)
                ->setSubject($values->subject)
                ->setBody($values->message);
                foreach ($recipients as $recipient) {
                    $mail->addTo($recipient);
                }
    $mailer = new SendmailMailer;
        $mailer->send($mail);
    $this->flashMessage('Done.', self::MSG_SUCCESS);
        $this->redirect('this');
        } catch (InvalidStateException $ex) {
        $this->flashMessage('Error', self::MSG_ERROR);
        }
}    

我使用foreach来获取多个addTo(),但它不会发送邮件。

1 个答案:

答案 0 :(得分:0)

邮件不能包含多个收件人。有必要创建一个循环并创建关于有多少收件人

的消息
public function contactsEditorFormSucceeded($form, $values)
{
    try {
        $recipients = $values->recipients;

        foreach ($recipients as $recipient) {
            $mail = new Message;                      

            $mail->setFrom($values->email)
                ->setSubject($values->subject)
                ->setBody($values->message)
                ->addTo($recipient);

            $mailer = new SendmailMailer;
            $mailer->send($mail);
        }

        $this->flashMessage('Done.', self::MSG_SUCCESS);
        $this->redirect('this');

    } catch (InvalidStateException $ex) {
        $this->flashMessage('Error', self::MSG_ERROR);
    }
}