当用户访问我的网站时,为什么会收到电子邮件通知?

时间:2014-02-02 19:17:07

标签: php html forms

我正在开发一个网站并注意到在测试中,我会在转到URL时收到电子邮件ping。基本上返回的是空白通知。

enter image description here

我怀疑这与我在动作领域输入的内容有关?

<form id="frmContact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

我也在html声明标签正上方输入了php脚本......

<?php

$to = "17antonio.ortiz@gmail.com"; 
$from = $_REQUEST['email']; 
$name = $_REQUEST['name']; 
$headers = "From: $from"; 
$subject = "You have a message sent from your site"; 

$fields = array(); 
$fields{"name"} = "name"; 
$fields{"email"} = "email"; 
$fields{"comments"} = "comments";

$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$send = mail($to, $subject, $body, $headers);
?>


<!DOCTYPE html>

任何见解都将受到赞赏!

P.S。反正有没有让电子邮件中的文字刷新? :)看起来有点搞笑!

3 个答案:

答案 0 :(得分:3)

因为每次请求页面时都会执行脚本。你想要做的是设置某种条件。例如,您应该检查是否对此页面发出了POST请求,然后才执行PHP代码(这是通过POST提交表单数据)。查看示例here

答案 1 :(得分:1)

每次加载页面时都会执行php代码。你可能想要的只是在表单发布后才运行php代码:

if ($_SERVER['REQUEST_METHOD'] == "POST")
{
  // paste all your php code to send the email here
}

答案 2 :(得分:0)

您应首先检查发布的表格或您。您可以通过选中是否设置任何表单字段来检查它

<?php
if(isset($_REQUEST['email']))
{
    $to = "17antonio.ortiz@gmail.com"; 
    $from = $_REQUEST['email']; 
    $name = $_REQUEST['name']; 
    $headers = "From: $from"; 
    $subject = "You have a message sent from your site"; 
    $fields = array(); 
    $fields{"name"} = "name"; 
    $fields{"email"} = "email"; 
    $fields{"comments"} = "comments";

    $body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){   $body .=                 sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

    $send = mail($to, $subject, $body, $headers);
}
?>