开发者

HTML/PHP form not sending email

I have a form which does everything right except send the input values to my email, what am I doing wrong? Ps: not using local server, so that's not it.

EDIT: I'm not getting any email whatsoever.

  • Tried changing the if(isset($_POST['enviar'])) { part but still not working.

  • Tried the chatty echos. the only if statement that isn't behaving properly is stripslashes. It stops at the else statement.

The form snippet:

<div id="contact-wrapper">

    <?php if(isset($hasError)) { //If errors are found ?>
        <p class="error">Please check if you entered valid information.</p>
    <?php } ?>

    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
        <p><strong>Email sent with success!</strong></p>
        <p>Thank you for using our contact form <strong><?php echo $name;?></strong>, we will contact you soon.</p>
    <?php } ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
        <div>
            <label for="name"><strong>Name:</strong></label>
            <input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
        </div>

        <div>
            <label for="email"><strong>E-mail:</strong></label>
            <input type="text" size="50" name="email" id="email" value="" class="required email" />
        </div>

        <div>
            <label for="subject"><strong>Subject:</strong></label>
            <input type="text" size="50" name="subject" id="subject" value="" class="required" />
        </div>

        <div>
            <label for="message"><strong>Message:</strong></label>
            <textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
        </div>
        <input type="submit" value="enviar" name="submit" id="submit" />
    </form>
    </div>
开发者_如何学JAVA

and the PHP:

<?php
//If the form is submitted
if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
    } else {
        $name = trim($_POST['contactname']);
    }

    //Check to make sure that the subject field is not empty
    if(trim($_POST['subject']) == '') {
        $hasError = true;
    } else {
        $subject = trim($_POST['subject']);
    }

    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    //Check to make sure comments were entered
    if(trim($_POST['message']) == '') {
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['message']));
        } else {
            $comments = trim($_POST['message']);
        }
    }

    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = 'myemail@email.com'; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>


The ereg() family of functions are deprecated. use the preg_...() equivalents instead. They work almost exactly the same, except requiring delimiters around the match patterns.

As well, don't use PHP_SELF in your form. That value is raw user-supplied data and can be trivially subverted for an XSS attack.

Checking for a particular form field to see if a POST occured is somewhat unreliable - you might change the field's name later on and your check will fail. However, this

if ($_SERVER['REQUEST_METHOD'] == 'POST) { ... }

will always work, no matter how many or few fields are in the form, as long as the form was actually POSTed.

As for the actual problem, I'm assuming the mail is getting sent out, or you'd have complained about that. That means your variables aren't being populated properly. Instead of just sending the mail, echo out the various variables as they're built, something like:

echo 'Checking name';
if ($_POST['name'] .....) {
     echo 'name is blank';
} else {
     $name = ...;
     echo "Found name=$name";
}

Basically have your code become extremely "chatty" and tell you what it's doing at each stage.


@dafz: Change

if(isset($_POST['submit'])) {

to

if(isset($_POST['enviar'])) {

@Marc B deserves another up-vote for his answer as well.

Edit

You can try the following update.

if(!isset($hasError)) {
    $siteAddress = 'validaddress@yourdomain.com'; //Put admin@ or info@ your domain here
    $emailTo = 'myemail@email.com'; //Put your own email address here
    $body = "Name: $name \r\nEmail: $email \r\nSubject: $subject \r\nComments: $comments \r\n";

    $headers  = 'To: ' . $name . ' <' . $emailTo . '>' . "\r\n";
    $headers .= 'From: My Site <' . $siteAddress . '>' . "\r\n";
    $headers .= 'Reply-To: ' . $email . "\r\n";

    if (mail($emailTo, $subject, $body, $headers)) {
        $emailSent = true;
    } else {
        $emailSent = false;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜