简体   繁体   English

如何在Wordpress页面上发送带有联系表的电子邮件

[英]How to send email with contact form on Wordpress page

I am trying to get a simple contact form to work with my wordpress site but cannot get it to work. 我正在尝试使用一个简单的联系表来与我的wordpress网站一起工作,但无法使其正常工作。 I dont want to use any plugin, I just want to do it using PHP. 我不想使用任何插件,我只想使用PHP即可。 So I put this file in root dir of my wordpress installation on my hostgator hosted site 所以我将此文件放在hostgator托管站点上wordpress安装的根目录中

www.example.com/sendmail.php www.example.com/sendmail.php

<?php
if(isset($_POST['submit'])){
$to = "myemail@yahoo.com";
$from= $_POST['email'];
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$message= $_POST['message'];
$subject = "Request email";
$headers = "From:" .$from;

mail($to,$subject,$message,$headers);
}
?>

This is the contact-us form on www.example.com/contact-us 这是www.example.com/contact-us上的联系我们表格

    <form action="http://www.example.com/sendmail.php" method="post">
                        <fieldset>
                            <div class="form-group">
                                <div class="col-md-12">
                                    <input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-12">
                                    <input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-12">
                                    <input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-12">
                                    <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-12">
                                    <textarea class="form-control" id="message" name="message" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7"></textarea>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-12 text-center">
<!--                                    <button type="submit" class="btn btn-primary btn-lg">Submit</button>-->
                                        <input type="submit" name="submit" class="btn btn-primary btn-lg"  value="Submit">
 </div>
                            </div>
                        </fieldset>
                    </form>

If i try to visit this page www.example.com/sendmail.php, it gives 200 success ok message. 如果我尝试访问此页面www.example.com/sendmail.php,它将显示200条成功ok消息。 However if I try to fill the form and then it sends the email, i am not able to recieve it. 但是,如果我尝试填写表格然后发送电子邮件,我将无法收到。

Is there anything I am missing or I need to check ? 有什么我想念的东西还是需要检查?

You are getting these variables $fname= $_POST['fname']; $lname= $_POST['lname']; 您正在获取这些变量$fname= $_POST['fname']; $lname= $_POST['lname']; $fname= $_POST['fname']; $lname= $_POST['lname']; on sendmail.php sendmail.php

But your html form input fields does not have name for fname and lname . 但是您的html表单输入字段没有fnamelname名称。

So use the below one : 因此,请使用以下之一:

 <div class="form-group">
                                <div class="col-md-12">
                                    <input id="fname" name="fname" type="text" placeholder="First Name" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-12">
                                    <input id="lname" name="lname" type="text" placeholder="Last Name" class="form-control">
                                </div>
                            </div>

Instead of : 代替 :

 <div class="form-group">
                                <div class="col-md-12">
                                    <input id="fname" name="name" type="text" placeholder="First Name" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-12">
                                    <input id="lname" name="name" type="text" placeholder="Last Name" class="form-control">
                                </div>
                            </div>

Hope it helps you. 希望对您有帮助。

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

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