I'm facing a little issue when I'm trying to send only one e-mail using some datas from a foreach loop. When I echo the data inside foreach, all code works, but making this, the code sends a lot of e-mails.
When I put the code responsible for sending mail outside this foreach, only one e-mail is sent. Ignoring my conditions in the foreach, I echo the keys, and they bring me the last key.
I want to send one e-mail containing the two conditions I wrote in the foreach: if from atendimento I want to send all data from atendimento 1596649 to one recipient and 1596652 data to other recipient.
I'm using Windows 10, MySQL 5, PHP 5 and apache 2.
$sql = 'SELECT * FROM dfp_dados WHERE atendimento IS NOT NULL';
$result = $pdo->query($sql);
$resultado = $result->fetchAll(PDO::FETCH_ASSOC);
$sql2 = 'SELECT atendimento FROM dfp_dados WHERE atendimento IS NOT NULL';
$result2 = $pdo->query($sql2);
$resultado2 = $result2->fetchAll(PDO::FETCH_ASSOC);
$atendmail = '';
$check = false;
foreach ($resultado2 as $key2 => $value2) {
if($value2['atendimento'] == "1596649"){
$atendmail = 'email';
} elseif($value2['atendimento'] == "1596652"){
$atendmail = 'email2';
}
$conteudo = '<table>' .
'<tr>' .
'<th>Order Id</th>' .
'<th>Line Item Id</th>' .
'<th>Campanha</th>' .
'<th>Formato</th>' .
'<th>Data de Início</th>' .
'<th>Data de Término</th>' .
'<th>Total de Dias</th>' .
'<th>Dias Veiculados</th>' .
'<th>Impressões Programadas</th>' .
'<th>Impressões Projetadas</th>' .
'<th>Impressões Entregues</th>' .
'<th>Impressões Faltantes</th>' .
'<th>Cliques</th>' .
'<th>CTR</th>' .
'<th>Resultado Final</th>' .
'<th>Under/Over</th>'.
'</tr>';
foreach ($resultado as $key => $value) {
$array = array_merge_recursive($value, $value2);
if($array['atendimento'][0] == $array['atendimento'][1]){
$conteudo .= '<tr>'.
'<td>'.$value['orderId'].'</td>'.
'<td>'.$value['lineItemId'].'</td>'.
'<td>'.$value['campanha'].'</td>'.
'<td>'.$value['formato'].'</td>'.
'<td>'.$value['dataInicio'].'</td>'.
'<td>'.$value['dataFim'].'</td>'.
'<td>'.$value['totalDias'].'</td>'.
'<td>'.$value['diasVeiculados'].'</td>'.
'<td>'.$value['impressoesProgramadas'].'</td>'.
'<td>'.$value['impressoesProjetadas'].'</td>'.
'<td>'.$value['impressoesEntregues'].'</td>'.
'<td>'.$value['impressoesFaltantes'].'</td>'.
'<td>'.$value['cliques'].'</td>'.
'<td>'.$value['ctr'].'</td>'.
'<td>'.$value['resultadoFinal'].'</td>'.
'<td>'.$value['underOver'].'</td>'.
'</tr>';
}
}
$conteudo .= '</table>';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'myUsername';
$mail->Password = 'myPass';
$mail->setFrom('opec@webedia-group.com');
$mail->ClearAddresses();
$mail->ClearAttachments();
$mail->addAddress($atendmail);
$mail->Subject = 'Suas Campanhas no DFP';
$mail->isHTML(true);
$mail->Body = $conteudo;
if (!$mail->send()) {
echo 'Erro no envio do email: ' . $mail->ErrorInfo;
} else {
echo 'Envio OK!';
}
}
echo $conteudo;