簡體   English   中英

使用PHP發送只有一封帶有不同收件人的電子郵件,並使用PHP維護整個代碼

[英]Send only one e-mail with different recipients in foreach using PHP and maintaining the whole code

當我嘗試使用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;

更改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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM