【问题标题】:contact HTML / PHP form doesn't send mails联系 HTML / PHP 表单不发送邮件
【发布时间】:2013-07-19 01:32:42
【问题描述】:

我正在使用 AJAX 联系表单,但它不发送邮件。我尝试了很多东西,但我真的不知道问题是什么。

    <!-- START CONTACT FORM -->         
            <div id="contact_form" class="grid_6" style="margin:0;">
                <div class="form-success">
                    <p>Ju falemnderit, mesazhi juaj eshte derguar!</p>
                </div>

                <div class="contact-form"> 
                    <form action="contact-form/send.php" method="post" class="form">    
                        <label>Emri dhe mbiemri</label> 
                        <input class="text" type="text" name="name"> 

                        <label>E-Mail</label> 
                        <input class="text" type="text" name="email"> 

                        <!--     
                        <label>Subject</label> 
                        <input class="text" type="text" name="subject"> 
                         -->

                        <label>Koment</label> 
                        <textarea name="message" rows="8" cols="60"></textarea> 

                        <a href="javascript:;" id="submit" class="button">Dergo Email</a>

                         <div class="loading"></div> 
                    </form> 
                </div>
            </div>
            <!-- END CONTACT FORM -->

这是contact-form/send.php

<?php

//Your e-mail address goes here: 

$to = "lorentsh@hotmail.com";
//


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Include email validator
    require 'email-validator.php';
    $validator = new EmailAddressValidator();

//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

$email = strip_tags($email);

if (!$validator->check_email_address($email)) {
    $errors[count($errors)] = 'Invalid email address.'; 
}

//if the errors array is empty, send the mail
if (!$errors) {

    //sender
    $from = $name . ' <' . $email . '>';

    //Structure of the message:
    $subject = 'Message from ' . $name; 
    $message = '
    <!DOCTYPE html>
    <head></head>
    <body>
    <table>
        <tr><td>Name:</td><td>' . $name . '</td></tr>
        <tr><td>Email:</td><td>' . $email . '</td></tr>
        <tr><td>Subject:</td><td>' . $subject . '</td></tr>
        <tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    //End of the message structure


    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;   
    }

//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="../contact.html">Back</a>';
    exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}

?>

你能告诉我问题出在哪里吗?到 index.html 或 send.php。我需要你们的帮助。

【问题讨论】:

  • 他们告诉我这是 ajax 联系表,但这是我的全部,需要帮助
  • 您确定您的服务器配置正确以发送邮件吗?你可能想在你的 sendmail 函数中添加一些调试语句并检查控制是否在那里。
  • 更改 $from = $name 时会发生什么。 ' ';到 $from = $name; ?
  • 其实我对服务器一窍不通,能指导我找服务器吗?
  • 嗨洛伦特,我不明白为什么我在回答你的问题时得到一个无用的标记。我在我的服务器上使用我的建议尝试了您的代码,我还删除了您的 php 文件中的验证器,因为它不存在并且您没有向它提供您的问题,并且它有效。请在我编辑的答案中找到下面的结果截图。谢谢。

标签: php html forms contact contact-form


【解决方案1】:

问题应该出在一行

<a href="javascript:;" id="submit" class="button">Dergo Email</a>

你需要一个按钮之类的东西来提交你的表单

type="submit"

另外请确保您的托管服务支持 PHP。

您也可以参考Not receiving the mail

我已经测试过了,请看下面的截图。

附上代码。

发送.php

<?php

//Your e-mail address goes here: 

$to = "lorentsh@hotmail.com";
//


//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;


//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 

$email = strip_tags($email);


//if the errors array is empty, send the mail
if (true) {

    //sender
    $from = $name . ' <' . $email . '>';

    //Structure of the message:
    $subject = 'Message from ' . $name; 
    $message = '
    <!DOCTYPE html>
    <head></head>
    <body>
    <table>
        <tr><td>Name:</td><td>' . $name . '</td></tr>
        <tr><td>Email:</td><td>' . $email . '</td></tr>
        <tr><td>Subject:</td><td>' . $subject . '</td></tr>
        <tr><td>Message:</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    //End of the message structure


    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';

    //else if GET was used, return the boolean value so that 
    //ajax script can react accordingly
    //1 means success, 0 means failed
    } else {
        echo $result;   
    }

//if the errors array has values
} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="../contact.html">Back</a>';
    exit;
}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;
}

?>

HTML 文件

 <!-- START CONTACT FORM -->         
            <div id="contact_form" class="grid_6" style="margin:0;">
                <div class="form-success">
                    <p>Ju falemnderit, mesazhi juaj eshte derguar!</p>
                </div>

                <div class="contact-form"> 
                    <form action="contact-form/send.php" method="post" class="form">    
                        <label>Emri dhe mbiemri</label> 
                        <input class="text" type="text" name="name"> 

                        <label>E-Mail</label> 
                        <input class="text" type="text" name="email"> 

                        <!--     
                        <label>Subject</label> 
                        <input class="text" type="text" name="subject"> 
                         -->

                        <label>Koment</label> 
                        <textarea name="message" rows="8" cols="60"></textarea> 

                        <button class="submit" id="submit" type="submit">Dergo Email</button>

                         <div class="loading"></div> 
                    </form> 
                </div>
            </div>
            <!-- END CONTACT FORM -->

因此,正如我所说,这可能是因为您的免费托管服务器不支持。

您可能想自己在http://goo.gl/ynTnE 上测试它,我会在那里保留几天。

【讨论】:

  • 谢谢,但我认为代码没有问题,但我使用的是免费主机,我认为我应该有一个额外的 php 服务器来发送邮件或类似的东西,我没有有一个想法,所以如果您能提供更多帮助,我将不胜感激!再次感谢!
  • 嗨洛伦特,我不明白为什么我在这个答案上得到一个无用的标志。我在我的服务器上使用我的建议尝试了您的代码,我还删除了您的 php 文件中的验证器,因为它不存在并且您没有向它提供您的问题,并且它有效。屏幕截图将在一秒钟内完成。
  • 非常感谢是服务器的问题,再次感谢大家——
【解决方案2】:

这不是 AJAX,它只是 HTML 和 PHP。

要使其工作,您需要提交表单;只需在表单中添加这样的按钮:

<input type="submit" value="Dergo Email">

这应该替换以下行:

<a href="javascript:;" id="submit" class="button">Dergo Email</a>

【讨论】:

  • 谢谢,但我认为代码没有问题,但我使用的是免费主机,我认为我应该有一个额外的 php 服务器来发送邮件或类似的东西,我没有有一个想法,所以如果您能提供更多帮助,我将不胜感激!再次感谢!
  • 无论服务器如何,如果您没有提交按钮,您就没有提交表单。您在那里的链接根本不做任何事情 - 它只是一个什么都不做的链接。尝试用我的建议更新代码,看看会发生什么。基于此,我可以尝试为您提供更多帮助。
  • 我试过你的建议它发生了同样的情况,告诉我消息已发送但我没有收到它:S
  • 哦,我明白了。在这种情况下,是的,可能是服务器本身没有发送邮件。
  • 非常感谢是服务器的问题,再次感谢大家
猜你喜欢
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
相关资源
最近更新 更多