将表格作为电子邮件发送,仅显示第一行

时间:2018-03-18 16:23:13

标签: php html sql email laravel-5

我正在发送批量电子邮件,但是在格式化表格时,我得到表格标题,表格中只有一行数据,我尝试使用php循环显示所有行(共10行)但我我没有运气,帮助赞赏。

<?php

$db = new PDO("mysql:dbname=website4.0;host=localhost", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = ("SELECT * FROM students, criteria_table, email, buildtable ");

$statement = $db->prepare($query);
$statement->execute();

    while($row = $statement->fetch()) {
        $id = $row['studentID'];
        $name = $row['name'];
        $email = $row['email'];
        $studentID = $row['studentID'];
        $title = $row['Title'];
        $heading1 = $row['Heading1'];
        $heading2 = $row['Heading2'];
        $heading3 = $row['Heading3'];
        $heading4 = $row['Heading4'];
        $subject =$row['subject'];
        $setfrom =$row['setfrom'];
        $yourname=$row['your_name'];
        $criteria=$row['Criteria'];
        $description=$row['Description'];
        $mark=$row['Mark'];


        sendEmail($id, $email, $title, $heading1, $heading2, $heading3, 
        $heading4, $name, $studentID, $subject, $setfrom, $yourname, 
        $criteria, $description, $mark);

    }

这是我打算尝试构建表格的函数:

<?
function sendEmail($id, $email, $title, $heading1, $heading2, 
$heading3, $heading4, $name, $studentID, $subject, $setfrom, 
$yourname, $criteria, $description, $mark)
{
    $mail = new PHPMailer\PHPMailer\PHPMailer(true);

    $db = new PDO("mysql:dbname=website4.0;host=localhost", "root", "");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = ("SELECT * FROM buildtable, criteria_table");

    $statement = $db->prepare($query);
    $statement->execute();
    $values = $statement->fetchAll();


    foreach($values as $row){
        $tit = $row['Title'];
        $heading1 = $row['Heading1'];
        $heading2 = $row['Heading2'];
        $heading3 = $row['Heading3'];
        $heading4 = $row['Heading4'];
    }


    foreach($values as $row){
        $htmlversion=
        "<h1>".$tit."</h1>Name: ".$name."<br>ID: ".$id."<br><table><tr>
        <th>".$heading1."</th><th>".$heading2."</th><th>".$heading3."</th>
        <th>".$heading4."</th></tr> <tr><td>".$row['Criteria']."</td>
        <td>".$row['Description']."</td><td>".$row['Mark']."</td></tr>";
    }



    $textVersion = "Hi ".$name.",.\r\n ".
        "This is your StudentID: ".$studentID."text Version";
    // Set mailer to use SMTP
    $mail->isSMTP();                                             
    // Specify main and backup SMTP servers
    $mail->Host = ' smtp.mailtrap.io';                                  
    // Enable SMTP authentication
    $mail->SMTPAuth = true;                                
    // SMTP username
    $mail->Username = 'b74afc48436e94';                       
    // SMTP password
    $mail->Password = '90b37082eab90a';                      
    // TCP port to connect to
    $mail->Port = 25;                                   
    $mail->setFrom($setfrom, $yourname);
    // Name is optional
    $mail->addAddress($email);               
    // Add 
    //$mail->addAttachment('/var/tmp/file.tar.gz');         
    // Set email format to HTML
    $mail->isHTML(true);                                  
    $mail->Subject = $subject;
    $mail->Body    = $htmlversion;
    $mail->AltBody = $textVersion;

    if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
    } 
    else {
        echo 'Message has been sent to Username : '.$name
            .' Email: '.$email.'<br><br>';
    }
}
?>

1 个答案:

答案 0 :(得分:1)

好的,您的代码中存在一些非常大的错误:

SELECT * FROM students, criteria_table, email, buildtable

可能不是你想要的......

它会使你的桌子与笛卡尔联接(又称为A,B / 1,2,3你将A1,A2,A3,B1,B2,B3)。您需要使用ON select from T1 INNER JOIN T2 ON T1.id = T2.t1_id来指定加入条件。

接下来,在同一个数组上使用foreach两次可能不是你想要的......

试试这个以了解它是如何工作的:

$htmlversion = '';

foreach($values as $row){
    $htmlversion .=
        "<h1>".$row['Title']."</h1>Name: ".$row['name']."<br>ID: ".$row['studentID']."<br><table><tr>
        <th>".$row['Heading1']."</th><th>".$row['Heading2']."</th><th>".$row['Heading3']."</th>
        <th>".$row['Heading4']."</th></tr> <tr><td>".$row['Criteria']."</td>
        <td>".$row['Description']."</td><td>".$row['Mark']."</td></tr></table>";
}

最后,正如Kyslik所说,如果你正在使用Laravel,你可能应该避免手工完成所有这些......