doushi3454 2019-04-24 19:24
浏览 45

使用PHP发送只有一封带有不同收件人的电子邮件,并使用PHP维护整个代码

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;
  • 写回答

1条回答 默认 最新

  • dqq9695 2019-04-24 19:47
    关注

    Change your SELECT query so you only fetch the rows for those two recipients.

    $sql2 = 'SELECT atendimento FROM dfp_dados WHERE atendimento IN (1596649, 1596652)';
    

    Or you could change the foreach loop so you don't send mails for anyone else.

    if($value2['atendimento'] == "1596649"){
        $atendmail = 'email';
    } elseif($value2['atendimento'] == "1596652"){
        $atendmail = 'email2';
    } else {
        continue; // skip the rest of this loop iteration
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接