脚本应发送包含表单提交值的电子邮件,但不会发送任何电子邮件

时间:2011-04-04 21:08:06

标签: php html email forms

所以我有一个带有PHP的HTML表单,但是在提交之后我没有收到任何电子邮件。我找不到问题!你能救我吗?

  • 尝试将if(isset($ _ POST ['submit'])){更改为'enviar',但仍无效。

  • 尝试放置一些回声来查看它无法正常工作的地方。在else语句中停止的唯一语句是stripslashes。

表格摘要:

<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>

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

3 个答案:

答案 0 :(得分:5)

在收到任何电子邮件之前,您必须确保该电子邮件已发送

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

放置在适当的位置时,此代码会输出什么?

答案 1 :(得分:0)

您是否尝试过一个简单的php页面来测试电子邮件(例如固定内容),我认为网络服务器已经配置了电子邮件并准备好转发您的地址?

答案 2 :(得分:-1)

如果这是WordPress联系表格,对于初学者:

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

应该是:

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

请参阅:wp_mail codex

现在您必须检查您的电子邮件设置以及WordPress电子邮件设置。