SQL查询组特定列值为每个用户发送一封电子邮件

Posted

技术标签:

【中文标题】SQL查询组特定列值为每个用户发送一封电子邮件【英文标题】:SQL query group specific column value to send single e-mail for each user 【发布时间】:2018-11-15 16:03:39 【问题描述】:

我真的需要这方面的帮助,我尝试了几个小时让我的代码正常工作,但我做不到。

我将放置一些代码和输出,以便轻松了解我需要什么以及我得到什么。

正如您在输出中看到的那样,有些用户拥有不止一张卡,而我需要的是,为每个带有卡的用户发送一封电子邮件,例如:

Email #1
To: test1@gmail.com
Text: 
Card 1

Email #2
To: test2@gmail.com
Text: 
Card 2
Card 3

Email #3
To: test3@gmail.com
Text: 
Card 1
Card 2
Card 4

我应该像上面那样发送电子邮件,但我将在下面发布的代码没有这样做,它发送电子邮件但它发送的第一封电子邮件发送到 test1@gmail.comtest2@gmail.com 和第二封电子邮件发送到test3@gmai.com,它为每张卡发送一封电子邮件,而不是一封包含所有客户卡的电子邮件

if (!$stm)

    echo "";

else

    $last_email = null;
    while ($rows = $stm->fetch())
    
        $CPersonUId = isset($rows['PersonUId']) ? $rows['PersonUId'] : NULL;
        $CType = isset($rows['CardType']) ? $rows['CardType'] : NULL;
        $CNr = isset($rows['CardNr']) ? $rows['CardNr'] : NULL;
        $CValFrom = isset($rows['ValidFrom']) ? $rows['ValidFrom'] : NULL;
        $CValUntil = isset($rows['ValidUntil']) ? $rows['ValidUntil'] : NULL;
        $CLTCCTime = isset($rows['LastTccTime']) ? $rows['LastTccTime'] : NULL;
        $CLTPEmail = isset($rows['Email']) ? $rows['Email'] : NULL;

        if ($CValUntil != NULL)
        
            $time1 = strtotime($CValUntil);
            $mytime1 = date("d/m/Y", $time1);
            $CValUntil = $mytime1;
        else
            $mytime1 = $CValUntil;
        

        if($CType == 6)
            $CType = 'Type 1';
        elseif($CType == 5)
            $CType = 'Type 2';
        elseif($CType == 1)
            $CType = 'Type 3';
        

    $message = '<html><body>';
        $message .= "<p>Cartão nº<b>".$CNr."</b> válido até <b>".$mytime1." Tipo: ".$CType."</b></p>";

    $message .= '</body></html>';

    $mail->IsSMTP();
    $mail->SMTPDebug = 0;
    $mail->Host = "test-localhost";
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl'; 
    $mail->Port = "465";
    $mail->Username = "admin@gmail.com";
    $mail->Password = "passxxxxx";
    $mail->SetFrom('admin@gmail.com', 'admin');
    $mail->addReplyTo("admin@gmail.com");
    $mail->Subject    = "Subject";
    $mail->AltBody    = "Alt";
    $mail->MsgHTML($message);


        if ( $last_email == null ) 
        $last_email = $CLTPEmail;
        
        if ( $CLTPEmail != $last_email ) 
            //echo $last_email . "</br>";
            $mail->addAddress($last_email);
             if(!$mail->send()) 
        echo "Mailer Error: " . $mail->ErrorInfo;
     else 
        echo "Message sent!";
    
            $last_email = $CLTPEmail;
        
    
    echo $last_email."</br>";

【问题讨论】:

听起来你只需要Card 列的逗号分隔结果。 Comma separated results in SQL 的可能重复项? Didn't you ask something similar already?. @FunkFortyNiner 我问过它,但关于我的查询,我一直在尝试根据我在该问题上获得的帮助发送电子邮件,但无法正常工作,但他们回答了我的问题问这个问题,因为现在问题有点不同,我提出了一个新问题。 @Larnu,不幸的是,我无法理解如何在我的查询中使用它,但我会继续尝试 @Larnu 顺便说一句,我认为这不是我需要的,因为我不需要发送用逗号分隔的卡片,我需要为每个用户在同一个电子邮件中发送它们。 【参考方案1】:
<?php

function sendCards( $cards, $email ) 
            if( empty( $cards ) || ! $email ) return;
            $message = '<html><body>';
            $message .= implode('', $cards);
            $message .= '</body></html>';

            $mail->IsSMTP();
            $mail->SMTPDebug = 0;
            $mail->Host = "test-localhost";
            $mail->SMTPAuth = true;
            $mail->SMTPSecure = 'ssl'; 
            $mail->Port = "465";
            $mail->Username = "admin@gmail.com";
            $mail->Password = "passxxxxx";
            $mail->SetFrom('admin@gmail.com', 'admin');
            $mail->addReplyTo("admin@gmail.com");
            $mail->Subject    = "Subject";
            $mail->AltBody    = "Alt";
            $mail->MsgHTML($message);
            $mail->addAddress($email);
            if(!$mail->send()) 
              echo "Mailer Error: " . $mail->ErrorInfo;
             else 
              echo "Message sent!";
            
            echo $last_email."</br>";


if (!$stm) 
    echo ""; 
 else 
    $last_email = null; 
    $cards = [];
    while ($rows = $stm->fetch())
    
        $CLTPEmail = isset($rows['Email']) ? $rows['Email'] : NULL;
        if( ! $CLTPEmail ) continue;
        if( $last_email !== $CLTPEmail ) 
            sendCards( $cards, $last_email );
            $cards = [];
            $last_email = $CLTPEmail;
        

        $CPersonUId = isset($rows['PersonUId']) ? $rows['PersonUId'] : NULL;
        $CType = isset($rows['CardType']) ? $rows['CardType'] : NULL;
        $CNr = isset($rows['CardNr']) ? $rows['CardNr'] : NULL;
        $CValFrom = isset($rows['ValidFrom']) ? $rows['ValidFrom'] : NULL;
        $CValUntil = isset($rows['ValidUntil']) ? $rows['ValidUntil'] : NULL;
        $CLTCCTime = isset($rows['LastTccTime']) ? $rows['LastTccTime'] : NULL;

        if ($CValUntil != NULL)
        
            $time1 = strtotime($CValUntil);
            $mytime1 = date("d/m/Y", $time1);
            $CValUntil = $mytime1;
        else
            $mytime1 = $CValUntil;
        

        if($CType == 6)
            $CType = 'Type 1';
        elseif($CType == 5)
            $CType = 'Type 2';
        elseif($CType == 1)
            $CType = 'Type 3';
        
        $cards [] = "<p>Cartão nº<b>".$CNr."</b> válido até <b>".$mytime1." Tipo: ".$CType."</b></p>";
    

    if( ! empty( $cards )) 
        sendCards( $cards, $last_email )
    

【讨论】:

我在sendCards( $cards, $last_email ) 之后收到以下错误Parse error: syntax error, unexpected '$cards' 就在这里$cards = []; 我做了一些修复。再试一次。 给我这个错误:Parse error: syntax error, unexpected '$cards' (T_VARIABLE) in... 错误在sendCards( $cards, $last_email )之后的同一行 sendCards( $cards, $last_email )之后添加;

以上是关于SQL查询组特定列值为每个用户发送一封电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

SQL 查询以查找具有相同列值的多行

查询用户名并返回密码的 SQL [重复]

如何使用 SQL Server 通过电子邮件发送包含查询结果的 Excel 工作簿

将当地时间转换为UTC时间

PL/SQL 在游标中引用另一个游标?

每周向用户发送电子邮件