How do I email multiple people with Phpmailer?

Asked

Viewed 1,563 times

2

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

$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 didn’t work, it only sends to 1.

OBS: No var_dump($e) he brings me all the bank results.

2 answers

0

Put inside a repeat loop, where the control clause ( condition to finish) 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 requirement is only happening once, otherwise it will give error and only send an email:

require("../class.phpmailer.php");
  • but I am already done in my while it is doing as long as there is email that would be the $e = $aux['mail']

  • Okay, I did the same thing once and the problem was in require("../class.phpmailer.php"), check this out..

0


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

Behold:

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

So at each loop it is deleting all addresses, ie only the last will be in the address list, to solve the problem use a array, to avoid duplicities should look similar to this (it seems 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 the trim is therefore necessary for the in_array or array_unique spaces at the beginning and at the end make different strings, for example:

[email protected]

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

 [email protected] 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.