簡體   English   中英

HTML / PHP表單未發送電子郵件

[英]HTML/PHP form not sending email

我有一個表格,除了將輸入值發送到我的電子郵件之外,其他所有操作都正確,我在做什么錯呢? 附言:不使用本地服務器,事實並非如此。

編輯 :我什么也沒收到。

  • 嘗試更改if(isset($ _ POST ['enviar'])){部分,但仍然無法正常工作。

  • 嘗試了健談的回聲。 行為不正確的唯一if語句是反斜杠。 它在else語句處停止。

表單片段:

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

ereg()系列函數已棄用。 請使用preg_...()等效項。 它們的工作原理幾乎完全相同,只是在匹配模式周圍需要定界符。

同樣,不要在表單中使用PHP_SELF 該值是用戶提供的原始數據,可以輕易地將其顛覆以進行XSS攻擊。

檢查特定表單字段以查看是否發生了POST是不可靠的-您稍后可能會更改該字段的名稱,並且檢查將失敗。 但是這個

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

只要表單實際上已過帳,無論表單中有多少個字段,它都將始終有效。

至於實際的問題,我假設郵件已發送出去,或者您可能對此有所抱怨。 這意味着您的變量未正確填充。 不僅僅是發送郵件,而是在構建它們時回顯各種變量,例如:

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

基本上,您的代碼變得非常“閑談”,並告訴您每個階段的工作情況。

@dafz:更改

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

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

@Marc B也應該得到他的回答。

編輯

您可以嘗試以下更新。

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