简体   繁体   中英

send mail form, but the mail is not send

Normally I code in ASP.NET, so the PHP world is a little new for me. When I fill out the form and press "send" I don't get a mail.

Can someone see whats wrong here ?

The Validation is working fine, but if you can see some code I don't need, then please say it.

<?php

/************************
* Variables 
*************************/

$cc = "";
$bcc = "";
$headermail = 'xxx@xyz.com';
$toaddress = "xxx@xyz.com";
$to = "my@mail.dk";
$name = "Thomas Petersen";
$email = "my@mail.dk";

if($_SERVER["REQUEST_METHOD"] == "POST")
    { 
    if(strlen($_POST['companyname']) < 1  || strlen($_POST['navn']) < 1  || strlen($_POST['beskrivelse']) < 1  || strlen($_POST['kommentar']) < 1 || validateEmail($email) == FALSE)
        {

        if(empty($_POST['navn'])){
            $emailerror .= '<li>Venligst indtast dit navn</li>';
        }

        else if(empty($_POST['telefon'])){
            $emailerror .= '<li>Venligst indtast dit telefon nummer</li>';
        }

        else if(!is_numeric($_POST['telefon'])){
            $emailerror .= '<li>OPMÆRKSOM! Telefon nummer, kan kun indholde tal-cifre.</li>';
        }

        else if(empty($_POST['beskrivelse'])){
            $emailerror .= '<li>Venligst indtast en beskrivelse</li>';
        }

        else if(empty($_POST['mvalue'])){
            $emailerror .= '<li>Venligst indtast ordre mængde</li>';
        }

        else if(empty($_POST['kommentar'])){
            $emailerror .= '<li>Venligst indtast en kommentar</li>';
        }
    }

    else
    {

        $emailerror .= "<h1 style='color:#00ff00;'>Mail Send!</h1>";

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

        $message = trim(stripslashes($message));

        $e_subject = 'Moseg&aring;rden Foresp&oslash;rgsel fra ' . $_POST['navn'] . '.';

        $headers = "Fra: " . $_POST['navn'] . PHP_EOL;
        $headers .= "Reply-To: $email" . PHP_EOL;
        $headers .= "MIME-Version: 1.0" . PHP_EOL;
        $headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
        $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
        $headers .= 'To: Thomas <my@mail.dk>' . PHP_EOL;
        $headers .= 'From: Salg af hus my@mail.dk>' . PHP_EOL;
        $headers .= 'Cc: my@mail.dk' . PHP_EOL;
        $headers .= 'Bcc: my@mail.dk' . PHP_EOL;

        mail($toaddress, $e_subject, $message, $headers);
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <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'>
            <ul>
                <p><?php echo $emailerror; ?></p>
            </ul>
        </div>
        <form method="post" action="">
            <div> 
                <div class="input_label user"> 
                    <label>Navn:</label>
                </div> 
                <input type="text" name="navn" />
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>Telefon nr.:</label>
                </div> 
                <input type='text' name="telefon" />
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>Beskrivelse:</label>
                </div> 
                <textarea name="beskrivelse"></textarea>
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>M&aelig;ngde:</label>
                </div> 
                <input type="text" name="mvalue" />
            </div> 
            <br />
            <div> 
                <div class="input_label user"> 
                    <label>Kommentar:</label>
                </div> 
                <textarea name="kommentar"></textarea>
            </div> 
            <br />
            <input type="submit" value=" Send " />
        </form>
    </body>
</html> 

EDIT

This code is working fine without the Validation, to send mail.

<?php
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $name = "Thomas Petersen";
        $email = "my@mail.dk";
        $message = $_POST['navn'] . " har sendt denne foresp&oslash;rgsel." . \r\n . "Telefon nr.: ". $_POST['telefon'] . "\r\n" . "Beskrivelse: ". $_POST['beskrivelse'] . "\r\n" . "M&aelig;ngde: ". $_POST['mvalue'] . "\r\n" . "Kommentar: ". $_POST['kommentar'] . "\r\n";
        $to = "thomas@pahm.dk";
        $subject = "Moseg&aring;rden Foresp&oslash;rgsel fra " . $_POST['navn'];
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'To: Thomas <my@mail.dk>' . "\r\n";
        $headers .= 'From: Salg af hus my@mail.dk>' . "\r\n";
        $headers .= 'Cc: my@mail.dk' . "\r\n";
        $headers .= 'Bcc: my@mail.dk' . "\r\n";
        if (!mail($to, $subject, $message, $headers)) {
            echo "<h1 style='color:rgb(0,0,255);'>Mail ikke send!</h1>";
        } else {
            echo "<h1 style='color:#00ff00;'>Mail Send!</h1>";
        }
    }
?>

Like I pointed out in the comments $_POST["companyname"] isnt set in your form. Therefore its always smaller than 1

//change this
if(strlen($_POST['companyname']) < 1  || strlen($_POST['navn']) < 1  || strlen($_POST['beskrivelse']) < 1  || strlen($_POST['kommentar']) < 1 || validateEmail($email) == FALSE)

//to this
if(strlen($_POST['navn']) < 1  || strlen($_POST['beskrivelse']) < 1  || strlen($_POST['kommentar']) < 1 || validateEmail($email) == FALSE)

i can't find an input text field with the name "companyname" in your form.

if strlen($_POST['companyname']) < 1 evaluates to true your email will not be send.

so maybe take it out of your if condition so that it looks just like this:

if(strlen($_POST['navn']) < 1  || strlen($_POST['beskrivelse']) < 1  || strlen($_POST['kommentar']) < 1 || validateEmail($email) == FALSE)

Do not use $_POST, try filter_input/filter_input_array, email send: http://swiftmailer.org/ . not strlen but isset, empty, filter_input have email validation filter. http://pl1.php.net/manual/en/function.filter-input-array.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM