在提交时从html表单发送带有PHP的电子邮件

时间:2016-02-17 22:36:08

标签: 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 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.';  } ?>

我的桌子旁边还有<?php echo $result; ?>但是如何在任何人点击提交之前停止显示该消息。

Live view

2 个答案:

答案 0 :(得分:0)

首先,我看到您在尝试获取远程IP的行中缺少变量名称。 而不只是[REMOTE_ADDR],请尝试$_SERVER['REMOTE_ADDR']

如果在此修复后无效,请发布一些错误消息,以便更方便地为您提供帮助。

答案 1 :(得分:-1)

你错过了输入代码,所以我试着制作一个,希望这有帮助。

首先,如果标签太早,你就会关闭。 其次,未定义变量$ 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;
    }
} ?>