当我尝试使用来自foreach循环的一些数据仅发送一封电子邮件时,我遇到了一个小问题。当我在foreach中回显数据时,所有代码都可以工作,但是这样做后,代码会发送大量电子邮件。
当我将负责发送邮件的代码放在此foreach之外时,仅发送一封电子邮件。忽略foreach中的条件,我回显了按键,它们为我带来了最后一个按键。
我想发送一封电子邮件,其中包含我在foreach中编写的两个条件:如果要从atendimento发送所有数据,则将atendimento 1596649的所有数据发送给一个收件人,并将1596652的数据发送给其他收件人。
我正在使用Windows 10,MySQL 5,PHP 5和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;
答案 0 :(得分:0)
更改您的SELECT
查询,以便仅获取这两个收件人的行。
$sql2 = 'SELECT atendimento FROM dfp_dados WHERE atendimento IN (1596649, 1596652)';
或者您可以更改foreach
循环,以免向其他人发送邮件。
if($value2['atendimento'] == "1596649"){
$atendmail = 'email';
} elseif($value2['atendimento'] == "1596652"){
$atendmail = 'email2';
} else {
continue; // skip the rest of this loop iteration
}