Send multiple emails in MySQL list with PHPMailer

0

I'm trying to send some emails with PHPMailer but you always get an error message:

  

Invalid address: my @ gmail.com, other.cruz @ web.com.br

I have already checked everything I know but I still can not, here my list is being mounted and the attempt to send:

// BUSCANDO PRODUTOS
$RelEmail = "SELECT cadUsuario.Email,
                    cadPessoa.Nome AS NomeTransp
            FROM cadUsuario
            INNER JOIN cadPessoa ON (cadUsuario.IdPessoa = cadPessoa.IdPessoa)
            WHERE (cadPessoa.Papel = 'Transportadora')
            AND (cadPessoa.Ativo = 1)";
$stm = $conn->prepare($RelEmail);
// $stm->bindValue(1, $IdPessoa, PDO::PARAM_INT);
$stm->execute();    
$RelEmail = $stm->fetchAll(PDO::FETCH_OBJ); 
// CONTAGEM DE REGISTROS RETORNADOS
$ContReg = count($RelEmail); 
// FECHANDO A CONSULTA
$stm->closeCursor(); 

foreach($RelEmail as $RegTransportadoras) {
    if ($ListaEmails == "") {
        $ListaEmails = $RegTransportadoras->Email;
    } else {
        $ListaEmails = $ListaEmails.",".$RegTransportadoras->Email;
    }
}

// Inclui o arquivo class.phpmailer.php localizado na pasta class
require_once("phpmailer/class.phpmailer.php");
date_default_timezone_set("Brazil/East");

$mail = new PHPMailer(true);

$mail->IsSMTP(); 

try {
     $mail->Host = 'smtp.seudominio.com.br'; 
     $mail->SMTPAuth   = true;  
     $mail->Port       = 587; 
     $mail->CharSet = "UTF-8";
     $mail->Username = '[email protected]'; 
     $mail->Password = 'secreta'; 
     $mail->SMTPSecure = "tls";
     $mail->SMTPDebug = 2;
     $mail->SetLanguage("br");

     $mail->SetFrom('[email protected]', 'Nome'); 
     $mail->AddReplyTo('[email protected]', 'Nome'); 
     $mail->Subject = 'Assunto';
     $mail->AddAddress($ListaEmails, 'Teste');
     $mail->AddCC($ListaEmails, 'Destinatario'); // Copia
     //$mail->AddBCC('[email protected]', 'Destinatario2''); // Cópia Oculta
     //$mail->AddAttachment('images/phpmailer.gif');      // Adicionar um anexo

     $mail->MsgHTML('corpo do email'); 
     $mail->Send();
     echo "Mensagem enviada com sucesso\n";


    }catch (phpmailerException $e) {
      echo $e->errorMessage(); //Mensagem de erro costumizada do PHPMailer
}
    
asked by anonymous 30.10.2018 / 17:29

1 answer

1

The problem occurs in the AddAdress method, you need to set one email at a time, as in the example below.

$mail->AddAddress('[email protected]', 'First Name');
$mail->AddAddress('[email protected]', 'Second Name');
$mail->AddAddress('[email protected]', 'Third Name');

In your case, it is best to go through the $RelEmail array in a foreach by calling this method for each element of the array.

    
30.10.2018 / 17:38