Email 使用zf2从数据库向多个收件人发送邮件

Email 使用zf2从数据库向多个收件人发送邮件,email,controller,zend-framework2,Email,Controller,Zend Framework2,这是我第一个使用Zend Framework 2的应用程序。我想向从数据库检索的收件人发送电子邮件,但每次我的代码都显示错误。这是我的代码: $user = new Container('user'); $db = $this->getServiceLocator()->get('db1'); if (!$user->offsetExists('id')) { $idconnected = '0'; } else { $idconnect

这是我第一个使用Zend Framework 2的应用程序。我想向从数据库检索的收件人发送电子邮件,但每次我的代码都显示错误。这是我的代码:

$user = new Container('user');
$db = $this->getServiceLocator()->get('db1');

if (!$user->offsetExists('id')) {
    $idconnected = '0';           
} else {
    $idconnected = $user->offsetGet('id');
    $mail = $db->query("SELECT email FROM user WHERE id =" . $idconnected)->execute()->current();

    $message = new Message();
    $message->addTo($mail, 'eee@web.com')
            ->addFrom('xxxx@gmail.com')
            ->setSubject('Invitation à l’événement : Soirée Latino');
    $message->addCc('xxxx@hotmail.com')
            ->addBcc("xxxx@hotmail.com");

    // Setup SMTP transport using LOGIN authentication
    $transport = new SmtpTransport();
    $options = new SmtpOptions(array(
        'host' => 'smtp.gmail.com',
        'connection_class' => 'login',
        'connection_config' => array(
            'ssl' => 'tls',
            'username' => 'xxxx@gmail.com',
            'password' => '*********'
        ),
        'port' => 587,
    ));
    $html = new MimePart('<b>Invitation for the event: Latin Night, orgonized by Mr. Jony Cornillon <i>Date : 06/04/2015</i></b>');
    $html->type = "text/html";
    $body = new MimeMessage();
    $body->addPart($html);
    //$body->setParts(array($html));

    $message->setBody($body);

    $transport->setOptions($options);
    $transport->send($message);
}

我尝试过使用此代码,现在它运行良好:

    foreach ($mail as $recip) {
    $message->addTo($recip);
    $message->addTo('samehmay@hotmail.com', 'eee@web.com')
                ->addFrom('maysameh0@gmail.com')
                ->setSubject('Invitation à l’événement : Soirée Latino');
    }
    foreach ($mail as $recip) {
    $message->addTo($recip);
    $message->addTo('samehmay@hotmail.com', 'eee@web.com')
                ->addFrom('maysameh0@gmail.com')
                ->setSubject('Invitation à l’événement : Soirée Latino');
    }