简体   繁体   English

HTML电子邮件表单不发送消息

[英]HTML Email Form Not Sending Message

I have downloaded a Bootstrap website template and am attempting to make the form that sends to my email address work. 我已经下载了一个Bootstrap网站模板,我正在尝试将发送到我的电子邮件地址的表单设置为工作。 It gives the message that everything is submitted when all of the comments are filled in, but no actual message is sent. 它会在填写所有注释时提供所有内容都已提交的消息,但不会发送实际消息。

Here is the chunk of the HTML from the index: 以下是索引中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>

Here's the code from the PHP file: 这是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;            
?>

Please help! 请帮忙!

Thank you in advance 先感谢您

Your form has no method, so likely it's doing a $_GET instead of $_POST : 你的表单没有方法,很可能它正在使用$_GET而不是$_POST

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

You have no names ( name="" ) on your inputs: 您的输入中没有名称( name="" ):

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

Also, you could break your script up a bit better: 此外,您可以更好地破坏您的脚本:

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.';

Are you sure it's calling your php file? 你确定它在调用你的php文件吗? I would move the action="public_html/mail/contact_me.php on your button to the opening form tag. 我会将你的按钮上的action="public_html/mail/contact_me.php移动到开始form标签上。

You could also put some "debugging" code in various points of the php file to help troubleshoot. 您还可以在php文件的各个点放置一些“调试”代码以帮助进行故障排除。

The mail() function may not be enabled on your hosting server for example. 例如,您的托管服务器上可能未启用mail()函数。 You could put a console.log('About to send email') just before the mail line, and console.log('Mail sent') just after the mail line. 您可以在邮件行之前放置console.log('About to send email') ,然后在邮件行之后放置console.log('Mail sent') This will appear in Firebug for example and help you see if the script is working properly. 这将出现在Firebug中 ,例如,帮助您查看脚本是否正常工作。

You also said it was stating it was successfully submitted, but I don't see any code that would output a success message. 您还说它已成功提交,但我没有看到任何可以输出成功消息的代码。 Can you be more clear on how you are being notified that it was "successful"? 您是否可以更清楚地了解如何通知您“成功”?

Try doing this: 试着这样做:

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

Note: $_POST['name'] will not work if the input fields don't have name='email' 注意:如果输入字段没有name='email'$_POST['name']将不起作用

Put the destination in form tag. 将目标放在表单标记中。

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

I believe above code is correct and hope you are trying to submit your form through a javascript(jquery) ajax POST request. 我相信上面的代码是正确的,并希望您尝试通过javascript(jquery)ajax POST请求提交表单。 if yes, You can add an alert/console in the ajax success block, if you are getting it in the success block properly and not received the mail in given email ID, its related to your mail server(local or live sever). 如果是,您可以在ajax成功块中添加警报/控制台,如果您正确地在成功块中获取它并且没有收到给定电子邮件ID中的邮件,它与您的邮件服务器(本地或活动服务器)相关。

  1. Above scenario is correct, try to use a mail ID with the same domain like, for example, if you are hosted your site as a www.exmaple.com try to use yourname@example.com 以上方案是正确的,尝试使用具有相同域的邮件ID,例如,如果您作为www.exmaple.com托管您的网站,请尝试使用yourname@example.com
  2. If you are still looking to send a mail to Gmail or Yahoo, check with your hosting administrator, if they can figure out what is preventing the form from sending. 如果您仍希望向Gmail或Yahoo发送邮件,请与您的托管管理员联系,了解他们是否可以找出阻止该表单发送的内容。 Sample ajax request below. 请在下面提供ajax请求示例。

    $.ajax({ url: "./mail/contact_me.php", type: "POST", data: { name: name, phone: phone, email: email, message: message }, cache: false, success: function() { alert('Success'); }, error: function() { alert('Failed'); }, complete: function() { } }); $ .ajax({url:“。/ mail / contact_me.php”,输入:“POST”,数据:{name:name,phone:phone,email:email,message:message},cache:false,success:function (){alert('Success');},error:function(){alert('Failed');},complete:function(){}});

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

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