为什么我的mail()类发送两次?

时间:2015-04-01 11:23:45

标签: php html forms oop email

我的邮件课遇到了一个小问题,出于某种原因它的邮件发送了两次。 我不知道为什么,这是我的邮件类:

<?php

class Contact {

public $senderName;
public $senderEmail;
public $recipient;
public $copy;
public $subject;
public $message;
public $bcc;
public $errors;

public function __construct($senderName, $senderEmail, $subject, $message){
  $this->senderName = $senderName;
  $this->senderEmail = $senderEmail;
  $this->recipient = 'me@email.com'; //ofcourse not my real email
  $this->subject = $subject;
  $this->message = $message;
  $this->copy = '';
  $this->bcc = '';
  $this->errors = '';
}

public function sendMail()
{
        if ($this->senderName != "") {
            $this->senderName = filter_var($this->senderName, FILTER_SANITIZE_STRING);
            if ($this->senderName == "") {
                $this->errors .= '- Please enter a valid name!';
            }
        } else {
            $this->errors .= '- You forgot to enter a name!<br />';
        }

        if ($this->senderEmail != "") {
            $this->senderEmail = filter_var($this->senderEmail, FILTER_SANITIZE_STRING);
            if ($this->senderEmail == "") {
                $this->errors .= '- Please enter a valid Email!';
            }
        } else {
            $this->errors .= '- You forgot to enter an email!<br />';
        }

        if ($this->subject != "") {
            $this->subject = filter_var($this->subject, FILTER_SANITIZE_STRING);
            if ($this->subject == "") {
                $this->errors .= '- Please enter a valid subject!';
            }
        } else {
            $this->errors .= '- You forgot to enter a subject!<br />';
        }

        if ($this->message != "") {
            $this->message = filter_var($this->message, FILTER_SANITIZE_STRING);
            if ($this->message == "") {
                $this->errors .= '- Please enter a valid message!';
            }
        } else {
            $this->errors .= '- You forgot to enter a message!<br />';
        }

        if (!$this->errors) {
          $this->bcc="";

          $headers = "From: $this->senderName <$this->senderEmail>";
          $headers .= "\r\nCc: $this->copy";
          $headers .= "\r\nBcc: $this->bcc\r\n\r\n";

          $send_contact = mail("$this->recipient","$this->subject","$this->message","$headers");
          return true;
          exit;

        } else {
          echo '<p class=\'message error\'>';
          echo '<font color="#FFFFFF">' . $this->errors . '</font>';
          echo '</p><br />';
        }
}
}
?>

我很确定这不应该发布两次,我希望你们中的任何人都可以帮我解决这个问题。

编辑:添加了我的答案。

1 个答案:

答案 0 :(得分:-1)

&#39;返回true&#39;在mail()导致它双重发送之后。