【问题标题】:HTML Email Form Not Sending MessageHTML 电子邮件表单未发送消息
【发布时间】:2015-02-11 20:07:09
【问题描述】:

我已经下载了一个 Bootstrap 网站模板,并正在尝试使发送到我的电子邮件地址的表单正常工作。当所有的 cmets 都填满时,它给出的消息是所有内容都已提交,但没有发送实际消息。

这是索引中的 HTML 块:

<section id="contact">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2>Contact Us</h2>
                <hr class="star-primary">
            </div>
        </div>
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2">
                <!-- To configure the contact form email address, go to mail/contact_me.php and update the email address in the PHP file on line 19. -->
                <!-- The form should work on most web servers, but if the form is not working you may need to configure your web server differently. -->
                <form name="sentMessage" id="contactForm" novalidate>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Name</label>
                            <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Email Address</label>
                            <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Phone Number</label>
                            <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Message</label>
                            <textarea rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                    <div class="row">
                        <div class="form-group col-xs-12">
                            <button type="submit" class="btn btn-success btn-lg" action="public_html/mail/contact_me.php">Send</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

这是 PHP 文件中的代码:

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'tri.developmentstudios@gmail.com'; // Add your email address inbetween the '' replacing            
yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the        
details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@tri-dev.com\n"; // This is the email address the generated message will     
be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

请帮忙!

提前谢谢你

【问题讨论】:

  • 据我所知,您的表单输入没有名称。
  • 另外,您正在检查$_POST,但您可能正在检查$_GET
  • 我假设他正在尝试使用 jquery ajax 提交并忘记在此处粘贴该代码 sn-p,如果是,则不需要 name 和 action 属性。

标签: php html twitter-bootstrap html-email


【解决方案1】:

您的表单没有方法,因此很可能它正在执行$_GET 而不是$_POST:

<form name="sentMessage" id="contactForm" novalidate method="post" action="public_html/mail/contact_me.php">

您的输入中没有姓名 (name=""):

<input name="email" type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">

另外,你可以更好地分解你的脚本:

function Validate()
    {
        // Check for empty fields
        if(empty($_POST['name'])            ||
                empty($_POST['email'])      ||
                empty($_POST['phone'])      ||
                empty($_POST['message'])    ||
                !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {

                return false;
           }
        else
            return true;
    }

function SendEmail($to = 'tri.developmentstudios@gmail.com')
    {
        if(Validate() == true) {
                $name           =   $_POST['name'];
                $email_address  =   $_POST['email'];
                $phone          =   $_POST['phone'];
                $message        =   $_POST['message'];

                // Create the email and send the message
                $email_subject  =   "Website Contact Form:  $name";
                $email_body     =   "You have received a new message from your website contact form.\n\n"."Here are the        
                details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
                $headers        =   "From: noreply@tri-dev.com\n";
                $headers        .= "Reply-To: $email_address";
                // Send true on successful send.
                // Send false if failed
                return (mail($to,$email_subject,$email_body,$headers))? true: false;
            }
        else
            // Invalid inputs
            return 'err';
    }

    // Apply function(s). You will get true, false, or err
    $send   =   SendEmail();

    // On return, you can echo any result
    if($send == 'err')
        echo 'Invalid Fields.';
    elseif($send == false)
        echo 'An Error Occurred.';
    else
        echo 'Email Sent Successfully.';

【讨论】:

  • 我也在尝试使用相同的表单并面临相同的问题,我想在这里提到一件事,我正在尝试使用 jquery ajax 提交,因此方法和操作属性在我的情况下无效。有人可以帮忙吗?
【解决方案2】:

您确定它正在调用您的 php 文件吗?我会将按钮上的 action="public_html/mail/contact_me.php 移动到开头的 form 标记。

您还可以在 php 文件的各个点放置一些“调试”代码以帮助排除故障。

例如,您的托管服务器上可能未启用mail() 功能。您可以将console.log('About to send email') 放在邮件行之前,将console.log('Mail sent') 放在邮件行之后。例如,这将出现在 Firebug 中,并帮助您查看脚本是否正常工作。

您还说它表示已成功提交,但我没有看到任何会输出成功消息的代码。您能否更清楚地了解您是如何收到“成功”通知的?

【讨论】:

    【解决方案3】:

    将目的地放在表单标签中。

    <form action='contact_me.php'>
    <input type='submit'>
    

    【讨论】:

      【解决方案4】:

      我相信上面的代码是正确的,希望您尝试通过 javascript(jquery) ajax POST 请求提交您的表单。如果是,您可以在 ajax 成功块中添加警报/控制台,如果您在成功块中正确获取它并且没有收到给定电子邮件 ID 中的邮件,则它与您的邮件服务器(本地或实时服务器)有关。

      1. 上述情况是正确的,请尝试使用具有相同域的邮件 ID,例如,如果您将站点托管为 www.exmaple.com,请尝试使用 yourname@example.com
      2. 如果您仍想向 Gmail 或 Yahoo 发送邮件,请与您的托管管理员联系,看看他们是否可以找出阻止发送表单的原因。 下面是示例 ajax 请求。

        $.ajax({ url: "./mail/contact_me.php", 类型:“发布”, 数据: { 姓名:姓名, 电话:电话, 电子邮件:电子邮件, 消息:消息 }, 缓存:假, 成功:函数(){ 警报(“成功”); }, 错误:函数(){ 警报(“失败”); }, 完成:函数(){ } });

      【讨论】:

        【解决方案5】:

        尝试这样做:

        <form name="sentMessage" id="contactForm" method='post' action='public_html/mail/contact_me.php' novalidate><form>
        

        注意:如果输入字段没有name='email',$_POST['name'] 将不起作用

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-22
          • 2017-03-31
          • 2016-07-02
          • 1970-01-01
          • 2016-03-10
          相关资源
          最近更新 更多