为什么PHP的mail()会向我发送数十封空白电子邮件?

时间:2014-02-25 02:02:06

标签: php wordpress email wordpress-theming

我在WordPress主题中使用PHP的mail()函数将表单提交发送到我的电子邮件。

但是,mail()已自动向我发送了至少20封空白电子邮件,
在最后五分钟内,未提交。

有人能否阐明我为创造这种情况所做的工作?
这是我(functions.php文件)

的代码
function send_my_form(){

  $form = array();
  $form['fstname']  = $_POST['fstname'];
  $form['lstname']  = $_POST['lstname'];
  $form['email']    = $_POST['email'];
  $form['message']  = $_POST['message'];

  $send_to  = 'fakeemail@gmail.com';
  $subject  = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
  $return   = "-f" . $send_to;

  $message  = "First Name: " . $form['fstname'] . "\r\n";
  $message .= "Last Name: " . $form['lstname'] . "\r\n";
  $message .= "Email: " . $form['email'] . "\r\n";
  $message .= "Message: " . $form['message'] . "\r\n";

  $headers  = 'MIME-Version: 1.0' . '\r\n';
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
  $headers .= 'From: ' . $send_to . '\r\n';
  $headers .= 'Reply-To: ' . $form['email'] . '\r\n';
  $headers .= 'Return-Path: ' . $send_to . '\r\n';
  $headers .= '\r\nX-Mailer: PHP/' . phpversion();

  mail($send_to, $subject, $message, $headers, $return);
}
add_action('wp_head', 'send_my_form');

1 个答案:

答案 0 :(得分:3)

这就是我在条件中运行它的意思。在运行邮件脚本之前验证字段是否已设置。

function send_my_form(){
  if(isset($_POST['fstname']) && isset($_POST['lstname']) && isset($_POST['email']) && isset($_POST['message']))
  {
    $form = array();
    $form['fstname']  = $_POST['fstname'];
    $form['lstname']  = $_POST['lstname'];
    $form['email']    = $_POST['email'];
    $form['message']  = $_POST['message'];

    $send_to  = 'fakeemail@gmail.com';
    $subject  = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
    $return   = "-f" . $send_to;

    $message  = "First Name: " . $form['fstname'] . "\r\n";
    $message .= "Last Name: " . $form['lstname'] . "\r\n";
    $message .= "Email: " . $form['email'] . "\r\n";
    $message .= "Message: " . $form['message'] . "\r\n";

    $headers  = 'MIME-Version: 1.0' . '\r\n';
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
    $headers .= 'From: ' . $send_to . '\r\n';
    $headers .= 'Reply-To: ' . $form['email'] . '\r\n';
    $headers .= 'Return-Path: ' . $send_to . '\r\n';
    $headers .= '\r\nX-Mailer: PHP/' . phpversion();

    mail($send_to, $subject, $message, $headers, $return);
  }
}