Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP在提交电子邮件时获取html表单_Php_Html_Forms_Mailing - Fatal编程技术网

使用PHP在提交电子邮件时获取html表单

使用PHP在提交电子邮件时获取html表单,php,html,forms,mailing,Php,Html,Forms,Mailing,我有一个html表单,用户将提交他们的联系信息,以便我们与他们联系。我希望所有这些都能在index.html上直播,但我并不反对在文件夹中创建另一个文件 <form class="row" method="post" action="contact.php"> <div class="col-md-12"> <div class="form-group"> <input type="text" class=

我有一个html表单,用户将提交他们的联系信息,以便我们与他们联系。我希望所有这些都能在
index.html
上直播,但我并不反对在文件夹中创建另一个文件

<form class="row" method="post" action="contact.php">
    <div class="col-md-12">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Your Full Name">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Phone Number*">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Email Address*">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Company Name*">
        </div>
        <div class="form-group">
            <textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
        </div>
    </div>

    <div class="buttn_outer">
        <button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left;">Submit</button>
    </div>
</form>

提交
我已经在服务器上安装了php。查看我发现的其他示例,我尝试使用php脚本,如下所示

<?php

if($_POST["submit"]) {
    $recipient = "me@me.com";
    $subject = "Some Message subject";
    $sender = $_POST["sender"];
    $senderEmail = $_POST["senderEmail"];
    $message = $_POST["message"];

    $mailBody = "Name: ".$sender."\nEmail: ".$senderEmail."\n\n".$message;

    mail($recipient, $subject, $mailBody, "From: ".$sender." <".$senderemail.">");
    $thankYou = "<p>Thank you! Your message has been sent.</p>";
}
?>

到目前为止,它会发送到成功消息,但是我从来没有收到任何东西发送到我的电子邮件。
我对php非常陌生,欢迎提供任何帮助/建议。

您的输入字段都没有名称属性

<input type="text" class="form-control" placeholder="Email Address*">
<input type="text" class="form-control" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>

应该是

<input type="text" class="form-control" name="senderEmail" placeholder="Email Address*">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" name="message" placeholder="Questions / comments"></textarea>

基本上,输入字段的name属性应该与
$\u POST
键匹配

因此,
将被处理为
$\u POST['name']


另外,它看起来像是把
$recipient
$senderEmail
变量混在一起了。PHP指定第一个参数是收件人。我理解
me@me.com
显然不是真正的电子邮件。但是,收件人应该是用户在表单中提交的电子邮件。确保邮件功能在不提交表单的情况下通过测试来工作,如果可以,则尝试使其与表单变量一起工作。

其他方面,我会加入

($_POST[“提交”])
应改为

!空($\u POST[“submit”])
-或-
isset($\u POST[“submit”])

永远不要假设变量已设置-在检查是否已设置(或您自己初始化)之前,不要对其运行任何测试或使用其值

根据您的PHP配置,该行可能会失败。因此,如果您将contact.php页面移动到另一台服务器,那么它可能会因错误而崩溃

正如Lance所提到的,要传递回服务器的每个表单字段都需要一个name属性。这是将其传递到服务器时将其链接到
$\u POST
数组的内容

此外,您还可以使用用户电子邮件作为发件人地址。但是,当接收服务器发现您的电子邮件不是来自该用户的域电子邮件服务器时,您的电子邮件可能会被标记为垃圾邮件。您必须使用服务器的电子邮件设置,以便在尝试“欺骗”它时将其验证为合法的发件人地址(也称为不使用服务器的默认地址)

如果要覆盖默认的发件人地址(或回复地址),则需要在标题字段中执行此操作(您可以通过谷歌搜索此字段以查看选项)


如果希望在HTML中包含HTML标记或元素,还需要在mail函数的headers参数中指明这一点。对于简单的提醒电子邮件,如您正在做的事情-我只使用纯文本和回车(如您使用的
\n
),而不使用

之类的项目。尝试一下这可能会帮到您

index.html  

 <form class="row" method="post" action="contact.php">
        <div class="col-md-12">
            <div class="form-group">
                <input type="text" class="form-control" name="sender" placeholder="Your Full Name">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="phno" placeholder="Phone Number*">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="email" placeholder="Email Address*">
            </div>
            <div class="form-group">
                <input type="text" class="form-control"  name="cmp" placeholder="Company Name*">
            </div>
            <div class="form-group">
                <textarea class="form-control lg_textarea" name="cmt" placeholder="Questions / comments"></textarea>
            </div>
        </div>

        <div class="buttn_outer">
            <button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left";>Submit</button>
        </div>
    </form>

    contact.php

    <?php
        $to='me@me.com';
        $header = "From: info@".$_SERVER["SERVER_NAME"]."\n";
        $header .= "Content-Type: text/html; charset=iso-8859-1\n";

        $sender=$_POST['sender'];
        $email=$_POST['email'];
        $cmt=$_POST['cmt'];
        $cmp=$_POST['cmp'];
        $phno=$_POST['phno'];

        $subject='Message Subject';

        $body='<table width="90%" border="0">
        <tr>
        <td><b>Sender:</b></td> <td>'.$sender.'</td>
        </tr>
        <tr>
        <td><b>Email:</b></td> <td>'.$email.'</td>
        </tr>
        <tr>
        <td><b>Phone No:</b></td> <td>'.$phno.'</td>
        </tr>
        <tr>
        <td><b>Copmany Name:</b></td> <td>'.$cmp.'</td>
        </tr>
        <tr>
        <td><b>Comment:</b></td> <td>'.$cmt.'</td>
        </tr>
        <tr></table>';



        mail($to,$subject,$body,$header);
       header('location:index.html');



    ?>
index.html
提交
contact.php

它在洛卡霍斯特吗?你不能从locahost发送电子邮件。如果localhost设置了smtp中继,当然可以。我不清楚他是否设置了smtp中继。除了确定这是php问题外,你是否验证过你可以直接发送测试电子邮件-邮件(“abc@abc.com“测试”、“身体”)?@airtech没错。html文件和php文件根本不匹配。所有输入字段都没有name属性。。