How to send email to several people with PHPMailer?

2

I would like to know how to make the "to:" in the email correspond to the field in the table, that is, that for example I have two emails, one email is [email protected] and the other email from the bank is [email protected] , I would like to know how do I send the email to the respective ones, because I can not, I can only send to one, I would like it to be my message para:[email protected] only this and without showing the two emails that were sent as do that?

$res = mysqli_query($mysqli, "SELECT * FROM newsletter");
    while($aux = mysqli_fetch_assoc($res)){ 

    $e = $aux['mail'];
    $mail->ClearAddresses($e);
    $mail->AddAddress($e);

    var_dump($e);
    }    
                        //se enviou com sucesso salvo este envio
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Mensagem enviada!';
        return true;
    }

This is the code snippet I did, but it did not work, it only sends 1.

NOTE: In%% of% it brings me right all the results of the bank.

    
asked by anonymous 25.12.2015 / 05:25

2 answers

0

The method ClearAddresses cleans all addresses, so I believe that in case you were trying to use it to clear repeated addresses, but according to the documentation it has no parameter:

See:

public function clearAddresses()
{
    foreach ($this->to as $to) {
        unset($this->all_recipients[strtolower($to[0])]);
    }
    $this->to = array();
}

So every loop is deleting all addresses, ie only the last will be in the address list, to solve the problem use a array , to avoid duplication should look similar to this (looks like your goal):

  • With array_unique

    $todosEnderecos = array();
    
    $res = mysqli_query($mysqli, "SELECT * FROM newsletter");
    while ($aux = mysqli_fetch_assoc($res)) {
        $todosEnderecos[] = trim($aux['mail']);
    }
    
    $todosEnderecos = array_unique($todosEnderecos);
    
    foreach($todosEnderecos as $value) {
        $mail->AddAddress($value);//Adiciona os endereços
    }
    
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Mensagem enviada!';
        return true;
    }
    
  • With in_array :

    $todosEnderecos = array();
    
    $res = mysqli_query($mysqli, "SELECT * FROM newsletter");
    while ($aux = mysqli_fetch_assoc($res)) {
        if (in_array($aux['mail'], $todosEnderecos) === false) { //Verifica se o endereço já existe
            $todosEnderecos[] = $aux['mail'];
            $mail->AddAddress($aux['mail']);//Adiciona os endereços
        }
    }
    
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Mensagem enviada!';
        return true;
    }
    
  

Note that trim is required because for in_array or array_unique spaces at the beginning and end make different strings, for example:

[email protected]

is different from (has a space at the beginning):

 [email protected] 
    
30.12.2015 / 03:24
0

Put inside a loop of repetition, where the control clause (condition to end) would be the amount of records in the bank, ie the amount of email's registered in the bank.

Or you can check if the phpmailer class request is only happening once, otherwise it will give error and will only send to an email:

require("../class.phpmailer.php");
    
25.12.2015 / 12:15