Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 31-03-2015 14:25:47

smay
Membre
Date d'inscription: 31-03-2015
Messages: 12

Send mail to multiple recipients from data base using zf2

Hello, I'm trying to send an e-mail to multiple e-mail address in my database. Here is my current code. It is only working when I specify a single e-mail address, however, I need to have them query my database and send the e-mail to each e-mail address. Where am I going wrong here?

Code:

[lang=php]
$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);
}

And this is the error:

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

Any help please. Thanks.

Hors ligne

 

#2 31-03-2015 14:43:09

flobrflo
Membre
Lieu: Marseille
Date d'inscription: 26-04-2013
Messages: 376

Re: Send mail to multiple recipients from data base using zf2

hi,
i try a test with your request, this is my result :

Code:

[lang=php]
$mails = array(
    0 => array('email' => 'blabla.com'),
    1 => array('email' => 'test.fr'),
);

so check your's wink

Hors ligne

 

#3 31-03-2015 14:52:25

smay
Membre
Date d'inscription: 31-03-2015
Messages: 12

Re: Send mail to multiple recipients from data base using zf2

Thanks flobrflo, i find the solution and this is the code:

Code:

[lang=php]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');
}

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages