Send multiple mailing lists from Mysql with Phpmailer

Asked

Viewed 393 times

0

I’m trying to send some emails with Phpmailer but always get an error message:

Invalid address: [email protected], [email protected]

I’ve checked everything I know but still can not, here my list is being assembled 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
}
  • You should not give the hosting credentials, username and Pw when you put online be careful with that

1 answer

1


The problem occurs in the method AddAdress, 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 the ideal is to go through the array $RelEmail in a foreach calling this method for each element of the array.

Browser other questions tagged

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