邮寄foreach构造

时间:2015-01-31 17:13:35

标签: php email

我是php新手,所以如果我说的话没有用,请不要讨厌我。我有一个带有测验表单的php页面,在提交时自我刷新并发布错误的答案等。现在我想添加功能将此结果发送到我的电子邮件。我设法让它发送邮件,但我无法获得整个数据。这是检查和打印问题和答案以及(尝试)邮寄它的原因:

if (isset($_POST['answers'])){
    $Answers = $_POST['answers']; 
    foreach ($Questions as $QuestionNo => $Value){              
        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){  
            echo $Value['Question'].'<br />';
            echo 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]].'<br />';
            echo 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];
        } 
        else {}        
    }
    $email = $_POST['userEmail'];
    $mailTo = 'intrelis@gmail.com';
    $mailFrom = 'From: <mail@mail.com>'; 
    $msgSubject = 'English level test from' . $email;
    $msgBody = ;

    mail ($mailTo, $msgSubject, $msgBody, $xHeaders);    
}

我想要在发送到我邮件的页面上打印相同的信息,但我不知道该怎么做。我在$ msgBody中放了什么?我尝试了不同的想法,但邮件的正文总是空的......

3 个答案:

答案 0 :(得分:1)

您应该将答案存储在var

//Define variable on top outside loop
$your_answer = '';
$correct_answer = '';

...
$your_answer .= 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]];

$correct_answer .= 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];

...
//After loop
echo $your_answer.'<br />';
echo $correct_answer;

并在邮件中将其连接到正文

$msgBody = $your_answer . "\n" . $correct_answer ."\n";

答案 1 :(得分:1)

使用字符串连接将每个问题和答案添加到循环中的邮件正文中。

if (isset($_POST['answers'])){
    $msgBody = '';
    $Answers = $_POST['answers']; 
    foreach ($Questions as $QuestionNo => $Value){              
        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){  
            echo $Value['Question'].'<br />';
            echo 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]].'<br />';
            echo 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];
            $msgBody .= "Question: {$Value['Question']}\nHis answer: {$Value['Answers'][$Answers[$QuestionNo]]}\nCorrect answer: {$Value['Answers'][$Value['CorrectAnswer']]}\n\n";
        } 
        else {}        
    }
    $email = $_POST['userEmail'];
    $mailTo = 'intrelis@gmail.com';
    $mailFrom = 'From: <mail@mail.com>'; 
    $msgSubject = 'English level test from' . $email;

    mail ($mailTo, $msgSubject, $msgBody, $xHeaders);    
}

答案 2 :(得分:1)

实际上我没看到你在哪里分配变量'$ msgBody'。以下是如何完成此操作的示例:

<?php

function sendMail($msgBody)
{

$email = $_POST['userEmail'];
$mailTo = 'intrelis@gmail.com';
$mailFrom = 'From: <mail@mail.com>';
$msgSubject = 'English level test from' . $email;

mail($mailTo, $msgSubject, $msgBody, $xHeaders);

}

if (isset($_POST['answers'])) {
$Answers = $_POST['answers'];
$output = "";
foreach ($Questions as $QuestionNo => $Value) {
    if ($Answers[$QuestionNo] != $Value['CorrectAnswer']) {

        $newOutput = $Value['Question'] . '<br />';
        $newOutput .= 'Your answer: ' . $Value['Answers'][$Answers[$QuestionNo]] . '<br />';
        $newOutput .= 'Correct answer: ' . $Value['Answers'][$Value['CorrectAnswer']];

        echo $newOutput;
        $output .= $newOutput;

    } else {
    }
}
sendMail($output);
}