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

时间:2015-03-31 11:01:42

标签: email controller zend-framework2

这是我使用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);
}

这就是错误:

5.1.2 We weren't able to find the recipient domain. Please check for any
5.1.2 spelling errors, and make sure you didn't enter any spaces,      periods,
5.1.2 or other punctuation after the recipient's email address.   a13sm19808164wjx.30 - gsmtp

1 个答案:

答案 0 :(得分:0)

我尝试使用此代码,现在工作正常:

    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');
    }