Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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
提交时从html表单用PHP发送电子邮件_Php_Html_Email_Phpmailer - Fatal编程技术网

提交时从html表单用PHP发送电子邮件

提交时从html表单用PHP发送电子邮件,php,html,email,phpmailer,Php,Html,Email,Phpmailer,当我测试这段代码时总是失败,有人能帮我吗 <?php if(isset($_POST['submit'])){ $to = "<<<___myEmail___>>>"; $from = $_POST['email']; $name = $_POST['name']; $subject = "Contact Form: LewisDerbyshire.co.uk"; $subject2 = "Copy of your form su

当我测试这段代码时总是失败,有人能帮我吗

  <?php if(isset($_POST['submit'])){
  $to = "<<<___myEmail___>>>";
  $from = $_POST['email'];
  $name = $_POST['name'];
  $subject = "Contact Form: LewisDerbyshire.co.uk";
  $subject2 = "Copy of your form submission : LewisDerbyshire.co.uk";
  $message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
  $IP = "Senders IP :" . [REMOTE_ADDR];

  $headers = "From:" . $from;
  $headers2 = "From:" . $to;

  mail($to,$subject,$message,$IP,$headers);
  mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
  echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.";
  if ($sent) {
       $result = 'Thank you,' . $name . 'Your message has been sent.';
   }   } else {
   $result = 'Sorry' . $name . ', there was a problem.';  } ?>

此外,我的表格旁边有
,但如何在任何人单击提交之前阻止它显示消息


首先,我发现您在尝试获取远程IP的行中缺少变量名。 请尝试
$\u服务器['REMOTE\u ADDR']
,而不只是
[REMOTE\u ADDR]


如果此修复后它不起作用,请发布一些错误消息,以便更容易地帮助您。

您错过了输入代码,因此我尝试创建一个,希望这有帮助

首先,如果标记太早,则关闭。 其次,未定义变量$sent。 第三,我的$sent变量还不清楚

    <form method="POST">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="message" />
<input type="submit" name="submit" />
</form>


<?php if (isset($_POST['submit'])) {
    $to = "Your mail";
    $from = $_POST['email'];
    $name = $_POST['name'];
    $subject = "Contact Form: LewisDerbyshire.co.uk";
    $subject2 = "Copy of your form submission : LewisDerbyshire.co.uk";
    $message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
    $IP = "Senders IP :" . $_SERVER["REMOTE_ADDR"];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;

    $sent = mail($to, $subject, $message, $IP, $headers);
    mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $name . ", I will contact you shortly.<br/>";
    if ($sent) {
        $result = 'Thank you,' . $name . ' Your message has been sent.';
        echo $result;
    } else {
        $result = 'Sorry' . $name . ', there was a problem.';
            echo $result;
    }
} ?>


它仍然不存在,错误——PHP解析错误:语法错误,意外“;”在第10行的t/index.php中——第10行代表IPSorry,这是我的错。您必须将REMOTE\u ADDR放在如下引号中:
$\u SERVER['REMOTE\u ADDR']
@MrPixelDream请编辑您的答案以包含提示的引号。完成!当我现在提交它时,我没有收到电子邮件,虽然也没有消息显示有问题,或者你的电子邮件以$sentWell的价格发送,我将其更改为测试你的服务器配置是否发送邮件。我已经这样做了,它没有,它确实这样做了。我以前做过,只是没有代码作为诊断,尝试回显变量,以检查它们是否已填充。这是一个很好的示例,说明了为什么不应直接使用
mail()
函数。您正在伪造发件人地址,因此SPF检查将失败,并且此代码易受头和消息注入攻击以及XSS攻击的攻击。使用图书馆。