简体   繁体   English

HTML / PHP表单未发送电子邮件

[英]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. 尝试更改if(isset($ _ POST ['enviar'])){部分,但仍然无法正常工作。

  • Tried the chatty echos. 尝试了健谈的回声。 the only if statement that isn't behaving properly is stripslashes. 行为不正确的唯一if语句是反斜杠。 It stops at the else statement. 它在else语句处停止。

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:

<?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. ereg()系列函数已弃用。 use the preg_...() equivalents instead. 请使用preg_...()等效项。 They work almost exactly the same, except requiring delimiters around the match patterns. 它们的工作原理几乎完全相同,只是在匹配模式周围需要定界符。

As well, don't use PHP_SELF in your form. 同样,不要在表单中使用PHP_SELF That value is raw user-supplied data and can be trivially subverted for an XSS attack. 该值是用户提供的原始数据,可以轻易地将其颠覆以进行XSS攻击。

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. 检查特定表单字段以查看是否发生了POST是不可靠的-您稍后可能会更改该字段的名称,并且检查将失败。 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 @dafz:更改

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

to

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

@Marc B deserves another up-vote for his answer as well. @Marc B也应该得到他的回答。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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