开发者

Script should send an email containing form submission values, but no email is getting sent

So I have a HTML form with some PHP but I'm not getting any email whatsoever after submitting it. I can't find the problem! can you help me out?

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

  • tried putting some echos to see where it's not working. the only statement that is stopping at the else statement is stripslashes.

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>

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;
    }
}
?>


Before getting any email, you have to be sure the email is being sent.

$sent = mail($emailTo, $subject, $body, $headers);
var_dump($sent);

What does this code output when placed in the appropriate place?


Have you tried a simple php page to do a test Email (eg fixed content) and I assume the webserver has email configured and is prepared to relay for your address?


If this is WordPress contact form, for starters:

mail($emailTo, $subject, $body, $headers);

should be:

wp_mail($emailTo, $subject, $body, $headers);

See: wp_mail codex

Now you have to check your e-mail setup, as well as WordPress e-mail setup too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜