How do I loop to send my emails?

Asked

Viewed 366 times

0

I’m trying to get the code to take the emails I’m going to put on a list. txt or in a form (while not assembling the database) and make a loop to send all emails. I happen to be getting this error message and don’t know how to fix the problem.

<?PHP

require_once('class.phpmailer.php');

$nm = $_GET['nome'];
$sb = $_GET['sobrenome'];

$address = "[email protected]";
$address = "[email protected]";

for($loop=0; $loop<4; $loop++){

  $mail = new PHPMailer();
  $body = file_get_contents('a.html');


  $mail->AddReplyTo("[email protected]","EREA SSA");
  $mail->SetFrom('[email protected]', 'EREA SSA');


  // Nome e Sobrenome pegos através do GET "site.com/email.php?nome=André&sobrenome=Leal"
  $mail->AddAddress($address[$loop], utf8_decode("$nm[$loop] $sb[$loop]"));
  $mail->Subject = "Teste !";
  $mail->MsgHTML($body);

  //$mail->AddAttachment(""); // Arquivos para anexo

  if(!$mail->Send()) {
    echo "Erro: " . $mail->ErrorInfo . "<br/>";
  } else {
    echo "Mensagem enviada !";
  }
  $mail->clearAllRecipients(); // Limpa o campo de para quem o email será enviado e o nome

}

?>

1 answer

1

Well, the solution has already been found, so below I leave the solution of the problem to those who want to use to study.

<?PHP

require_once('class.phpmailer.php');

$nm = $_GET['nome'];
$sb = $_GET['sobrenome'];

$address[] = "[email protected]";
$address[] = "[email protected]";
$address[] = "[email protected]";
$address[] = "[email protected]";

foreach($address as $addr){

  $mail = new PHPMailer();
  $body = file_get_contents('a.html');

  $mail->AddReplyTo("[email protected]","Opa Epa");
  $mail->SetFrom('[email protected]', 'Opa Epa');

  $mail->AddAddress($addr, utf8_decode("$nm[$loop] $sb[$loop]"));
  $mail->Subject = "Teste !";
  $mail->MsgHTML($body);

  //$mail->AddAttachment(""); // Arquivos para anexo

  if(!$mail->Send()) {
    echo "Erro: " . $mail->ErrorInfo . "<br/>";
  } else {
    echo "Mensagem enviada !<br/>";
  }
  $mail->clearAllRecipients(); // Limpa o campo de para quem o email será enviado e o nome

}

?>

Browser other questions tagged

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