PHP发送邮件表单,工作 - 但在页面刷新时发送空邮件

时间:2014-06-11 13:10:17

标签: php

我是一名ASP.NET用户,但是想要学习PHP,我已经发了一个PHP邮件发送表单,它来自网上的一些教程,谷歌搜索。

我有一个......没有两个问题。

  1. 当我加载页面时,它会向我发送一封空邮件,为什么?
  2. 我建议您在代码中使用或编辑某些内容吗?
  3. 我的工作代码就是这个

    <?php 
    if (isset($_REQUEST['submitted'])) {
    //START of validation 
        // Initialize error array.
        $errors = array();
        // Check for a proper name
        if (!empty($_REQUEST['navn'])) {
            $navn = $_REQUEST['navn'];
            $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
            if (preg_match($pattern,$navn)){ $navn = $_REQUEST['navn'];}
            else{ $errors[] = 'Dit navn kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
        } 
        else {$errors[] = 'Venligst indtast dit navn.';}
    
        //Check for a valid phone number
        if (!empty($_REQUEST['telefon'])) {
            $telefon = $_REQUEST['telefon'];
            $pattern = "/^[0-9\_]{7,20}/";
            if (preg_match($pattern,$telefon)){ $telefon = $_REQUEST['telefon'];}
            else{ $errors[] = 'Dit telefon nummer kan kun være tal.';}
        } 
        else {$errors[] = 'Venligst indtast dit telefon nummer.';}
    
        // Check for a proper info
        if (!empty($_REQUEST['beskrivelse'])) {
            $beskrivelse = $_REQUEST['beskrivelse'];
            $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
            if (preg_match($pattern,$beskrivelse)){ $beskrivelse = $_REQUEST['beskrivelse'];}
            else{ $errors[] = 'Din beskrivelse kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
        } 
        else {$errors[] = 'Venligst indtast en beskrivelse.';}
    
        // Check for a proper value
        if (!empty($_REQUEST['mvalue'])) {
            $mvalue = $_REQUEST['mvalue'];
            $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
            if (preg_match($pattern,$mvalue)){ $mvalue = $_REQUEST['mvalue'];}
            else{ $errors[] = 'Feltet mængde kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
        } 
        else {$errors[] = 'Venligst indtast en mængde.';}
    
        // Check for a proper comment
        if (!empty($_REQUEST['kommentar'])) {
            $kommentar = $_REQUEST['kommentar'];
            $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
            if (preg_match($pattern,$kommentar)){ $kommentar = $_REQUEST['kommentar'];}
            else{ $errors[] = 'Din kommentar kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
        } 
        else {$errors[] = 'Venligst indtast en kommentar.';}
     }
    //End of validation 
    
    //START Send mail
        if (empty($errors)) { 
            $to = "my@mail.dk"; 
    
            $message = "<!DOCTYPE HTML>";
            $message .= "<html><head></head><body>";
            $message .= "<table>";
            $message .= "<tr><td colspan='2'>" . $navn . " har sendt denne foresp&oslash;rgsel.</td></tr>";
            $message .= "<tr><td>Telefon nr.:</td><td>" . $telefon . "</td></tr>";
            $message .= "<tr><td>Beskrivelse:</td><td>" . $beskrivelse . "</td></tr>";
            $message .= "<tr><td>M&aelig;ngde:</td><td>" . $mvalue . "</td></tr>";
            $message .= "<tr><td>Kommentar:</td><td>" . $kommentar . "</td></tr>";
            $message .= "</table></body></html>";
            $message .= trim(stripslashes($message));
    
            $subject = "Mosegården Forespørgsel fra " . $navn . ".";
    
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
            $headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
            $headers .= "From: Mosegården Hjemmeside - Forespørgsel" . "\r\n";
    
            mail($to, $subject, $message, $headers);
        }
    //End of Send mail
    ?>
    
    <!DOCTYPE html>
    <html lang="da">
        <head>
            <meta charset="utf-8" />
            <title></title>
            <style>
            input[type=text] {
                padding:5px; border:2px solid #ccc; 
                -webkit-border-radius: 5px;
                border-radius: 5px;
            }
            input[type=text]:focus {
                border-color:#333; 
            }
            input[type=submit] {
                padding:5px 15px; 
                background:#ccc; 
                border:0 none;
                cursor:pointer;
                -webkit-border-radius: 5px;
                border-radius: 5px; 
            }
            textarea{
                padding:5px; border:2px solid #ccc; 
                -webkit-border-radius: 5px;
                border-radius: 5px;
            }
            </style>
        </head>
        <body>
            <h1>Foresp&oslash;rgsel</h1> 
            <div id='emailerror'>
                    <?php 
                    //Print Errors
                    if (isset($_REQUEST['submitted'])) {
                    // Print any error messages. 
                        if (!empty($errors)) { 
                            echo '<hr /><h3>Der skete følgende:</h3><ul>'; 
                            // Print each error. 
                            foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
                            echo '</ul><h1>Mail ikke send! pga. følgende fejl.</h1><hr />';
                        } else {
                                echo '<hr /><h1 style="color:#00ff00;">Mail Send!</h1><hr />'; 
                        }
                    }
                    //End of errors array
                    ?>
            </div>
            <form method="post" action="">
                <div> 
                    <div class="input_label user"> 
                        <label>Navn:</label>
                    </div> 
                    <input type="text" name="navn" value="<?php echo $navn; ?>" />
                </div> 
                <br />
                <div> 
                    <div class="input_label user"> 
                        <label>Telefon nr.:</label>
                    </div> 
                    <input type="text" name="telefon" value="<?php echo $telefon; ?>" />
                </div> 
                <br />
                <div> 
                    <div class="input_label user"> 
                        <label>Beskrivelse:</label>
                    </div> 
                    <textarea name="beskrivelse"><?php echo $beskrivelse; ?></textarea>
                </div> 
                <br />
                <div> 
                    <div class="input_label user"> 
                        <label>M&aelig;ngde:</label>
                    </div> 
                    <input type="text" name="mvalue" value="<?php echo $mvalue; ?>" />
                </div> 
                <br />
                <div> 
                    <div class="input_label user"> 
                        <label>Kommentar:</label>
                    </div> 
                    <textarea name="kommentar"><?php echo $kommentar; ?></textarea>
                </div> 
                <br />
                <input name="submitted" type="submit" value=" Send " />
            </form>
        </body>
    </html> 
    

1 个答案:

答案 0 :(得分:0)

试试这个。我将结束括号从第2行的if语句移动到发送邮件例程的底部。见第78行的评论

<?php 
if (isset($_REQUEST['submitted'])) {
//START of validation 
    // Initialize error array.
    $errors = array();
    // Check for a proper name
    if (!empty($_REQUEST['navn'])) {
        $navn = $_REQUEST['navn'];
        $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
        if (preg_match($pattern,$navn)){ $navn = $_REQUEST['navn'];}
        else{ $errors[] = 'Dit navn kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
    } 
    else {$errors[] = 'Venligst indtast dit navn.';}

    //Check for a valid phone number
    if (!empty($_REQUEST['telefon'])) {
        $telefon = $_REQUEST['telefon'];
        $pattern = "/^[0-9\_]{7,20}/";
        if (preg_match($pattern,$telefon)){ $telefon = $_REQUEST['telefon'];}
        else{ $errors[] = 'Dit telefon nummer kan kun være tal.';}
    } 
    else {$errors[] = 'Venligst indtast dit telefon nummer.';}

    // Check for a proper info
    if (!empty($_REQUEST['beskrivelse'])) {
        $beskrivelse = $_REQUEST['beskrivelse'];
        $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
        if (preg_match($pattern,$beskrivelse)){ $beskrivelse = $_REQUEST['beskrivelse'];}
        else{ $errors[] = 'Din beskrivelse kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
    } 
    else {$errors[] = 'Venligst indtast en beskrivelse.';}

    // Check for a proper value
    if (!empty($_REQUEST['mvalue'])) {
        $mvalue = $_REQUEST['mvalue'];
        $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
        if (preg_match($pattern,$mvalue)){ $mvalue = $_REQUEST['mvalue'];}
        else{ $errors[] = 'Feltet mængde kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
    } 
    else {$errors[] = 'Venligst indtast en mængde.';}

    // Check for a proper comment
    if (!empty($_REQUEST['kommentar'])) {
        $kommentar = $_REQUEST['kommentar'];
        $pattern = "/^[a-zA-Z0-9æøåÆØÅ\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
        if (preg_match($pattern,$kommentar)){ $kommentar = $_REQUEST['kommentar'];}
        else{ $errors[] = 'Din kommentar kan kun indholde _, 1-9, A-Z or a-z 2-20 long.';}
    } 
    else {$errors[] = 'Venligst indtast en kommentar.';}

//End of validation 

//START Send mail
    if (empty($errors)) { 
        $to = "my@mail.dk"; 

        $message = "<!DOCTYPE HTML>";
        $message .= "<html><head></head><body>";
        $message .= "<table>";
        $message .= "<tr><td colspan='2'>" . $navn . " har sendt denne foresp&oslash;rgsel.</td></tr>";
        $message .= "<tr><td>Telefon nr.:</td><td>" . $telefon . "</td></tr>";
        $message .= "<tr><td>Beskrivelse:</td><td>" . $beskrivelse . "</td></tr>";
        $message .= "<tr><td>M&aelig;ngde:</td><td>" . $mvalue . "</td></tr>";
        $message .= "<tr><td>Kommentar:</td><td>" . $kommentar . "</td></tr>";
        $message .= "</table></body></html>";
        $message .= trim(stripslashes($message));

        $subject = "Mosegården Forespørgsel fra " . $navn . ".";

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
        $headers .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
        $headers .= "From: Mosegården Hjemmeside - Forespørgsel" . "\r\n";

        mail($to, $subject, $message, $headers);
    }
//End of Send mail
 } // <-- moved bracket to here to close if statement on line 2
?>

<!DOCTYPE html>
<html lang="da">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style>
        input[type=text] {
            padding:5px; border:2px solid #ccc; 
            -webkit-border-radius: 5px;
            border-radius: 5px;
        }
        input[type=text]:focus {
            border-color:#333; 
        }
        input[type=submit] {
            padding:5px 15px; 
            background:#ccc; 
            border:0 none;
            cursor:pointer;
            -webkit-border-radius: 5px;
            border-radius: 5px; 
        }
        textarea{
            padding:5px; border:2px solid #ccc; 
            -webkit-border-radius: 5px;
            border-radius: 5px;
        }
        </style>
    </head>
    <body>
        <h1>Foresp&oslash;rgsel</h1> 
        <div id='emailerror'>
                <?php 
                //Print Errors
                if (isset($_REQUEST['submitted'])) {
                // Print any error messages. 
                    if (!empty($errors)) { 
                        echo '<hr /><h3>Der skete følgende:</h3><ul>'; 
                        // Print each error. 
                        foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
                        echo '</ul><h1>Mail ikke send! pga. følgende fejl.</h1><hr />';
                    } else {
                            echo '<hr /><h1 style="color:#00ff00;">Mail Send!</h1><hr />'; 
                    }
                }
                //End of errors array
                ?>
        </div>
        <form method="post" action="">
            <div> 
                <div class="input_label user"> 
                    <label>Navn:</label>
                </div> 
                <input type="text" name="navn" value="<?php echo $navn; ?>" />
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>Telefon nr.:</label>
                </div> 
                <input type="text" name="telefon" value="<?php echo $telefon; ?>" />
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>Beskrivelse:</label>
                </div> 
                <textarea name="beskrivelse"><?php echo $beskrivelse; ?></textarea>
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>M&aelig;ngde:</label>
                </div> 
                <input type="text" name="mvalue" value="<?php echo $mvalue; ?>" />
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>Kommentar:</label>
                </div> 
                <textarea name="kommentar"><?php echo $kommentar; ?></textarea>
            </div> 
            <br />
            <input name="submitted" type="submit" value=" Send " />
        </form>
    </body>
</html>