第一个OOP脚本(PHP电子邮件脚本)

时间:2018-04-06 03:55:50

标签: php email

这是我的第一个OOP脚本,一个为我正在处理的网站设计的PHP电子邮件脚本:ndkutz(。)net。我希望用户能够从网站向理发店老板发送电子邮件。我是自学成才,即使我知道我走在正确的轨道上它也不起作用,我感到绝对迷失。我的代码有什么好处吗?此外,我在代码审核中发布此内容无效。

<?php

 $error = '';
 $errormsg = '';
 $finalMessage = '';
 $finalName = '';
 $finalSubject = '';
 $finalTo = '';
 $finalHeader = '';
 $sendingEmail = '';
 $emailMessageS = '';
 $emailMessageF = '';

class emailConstruction 
{
    private $from = "";
    private $name = "";
    private $message = "";

    public function scrubAll($data) {
        $data = htmlspecialchars($data);
        $data = trim($data);
        $data = strip_tags($data);
        return $data;
    }

    public function setfrom($from){
        $this->from = stripslashes($from);
        $this->from = $from;
    }

    public function getFrom(){
        return $this->from;
    }

    public function setName($name){
        $this->name = scrubAll($name);
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }


    public function setMessage($message){
        $this->message = scrubAll($data);
        $this->message = wordwrap($data,70,"<br />");
        $this->message = $message;
    }

    public function getMessage(){
        return $this->message;
    }
}

if(isset($_POST['submit']))
    {
        if(empty($_POST['uname']))
        {
            $error = 1;
            $errormsg = "Your name is required.";
            return false;
        }else{
            $error = 0;
            $createEmail = new emailConstruction;
            $createEmail->setName($_POST['uname']);
        }
        if(empty($_POST['umail']))
        {
            $error = 1;
            $errormsg = "Email address required.";
            return false;
        }else {
            $error = 0;
            $createEmail =  new emailConstruction;
            $createEmail->setTo($_POST['umail']);
        }
        if(empty($_POST['umsg']))
        {
            $error = 1;
            $errormsg = "Message is required";
            return false;
        }else{
            $error = 0;
            $createEmail = new emailConstruction;
            $createEmail->setMessage($_POST['umsg']);
        }
        if($error = 0)
        {   $finalHeader = 'from:' . $finalFrom;
            $finalHeader .='MIME-Version: 1.0\r\n';
            $finalHeader .='Content-type: text/html\r\n';
            $finalMessage = $createEmail->getMessage();
            $finalName = $createEmail->getName();
            $finalSubject = 'New potiential client by the name of ' . $finalName;
            $finalTo = 'madebyknight@icloud.com';

            $sendingEmail = mail($finalTo,$finalSubject,$finalMessage,$finalHeader);
            if($sendingEmail == true)
            {
                $emailMessageS = 'Email sent successfully!';
            }else{
                $emailMessageF = 'Error. Please try again!';
            }
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

嗯,首先,当您执行setXXX时,您的班级正在覆盖某些值。它应该是这样的:

class emailConstruction 
{
    private $from = "";
    private $name = "";
    private $message = "";

    public function scrubAll($data) {
        $data = htmlspecialchars($data);
        $data = trim($data);
        $data = strip_tags($data);
        return $data;
    }

    public function setfrom($from){
        $this->from = stripslashes($from);
    }

    public function getFrom(){
        return $this->from;
    }

    public function setName($name){
        $this->name = scrubAll($name);
    }

    public function getName(){
        return $this->name;
    }

    public function setMessage($message){
        $this->message = wordwrap(scrubAll($data), 70, "<br />");
    }

    public function getMessage(){
        return $this->message;
    }
}

注意我删除了一些代码行。另请注意我们如何在scrubAll($data)函数中将wordwrap的返回值分配给setMessage()

好的,现在有了这个问题,让我们继续吧!

您尝试在if语句的末尾进行一些输入验证。但是每次进行检查时都会创建一个新的emailConstruction对象。

我已经清理了一下:

if (isset($_POST['submit'])) {
  $error = 0;

  if (empty($_POST['uname'])) {
    $error = 1;
    $errormsg = "Your name is required.";
  }

  if (empty($_POST['umail'])) {
    $error = 1;
    $errormsg = "Email address required.";
  }

  if (empty($_POST['umsg'])) {
    $error = 1;
    $errormsg = "Message is required";
  }

  if ($error === 0) {
    $createEmail = new emailConstruction();
    $createEmail->setName($_POST['uname']);
    $createEmail->setTo($_POST['umail']);
    $createEmail->setMessage($_POST['umsg']);

    $finalHeader = 'from:' . $finalFrom;
    $finalHeader .='MIME-Version: 1.0\r\n';
    $finalHeader .='Content-type: text/html\r\n';
    $finalMessage = $createEmail->getMessage();
    $finalName = $createEmail->getName();
    $finalSubject = 'New potiential client by the name of ' . $finalName;
    $finalTo = 'madebyknight@icloud.com';

    $sendingEmail = mail($finalTo, $finalSubject, $finalMessage, $finalHeader);

    if ($sendingEmail == true) {
        $emailMessageS = 'Email sent successfully!';
    } else {
        $emailMessageF = 'Error. Please try again!';
    }
  } else {
    // Some fields were not filled up.
    // Do something.
  }
}

虽然它不会影响代码的功能,但也有一些惯例可以采用。

  • 班级名称通常以大写字母开头。因此,您不必命名您的班级emailConstruction,而是将其命名为EmailConstruction
  • 在可能的情况下尝试使用名词而不是动词来命名类也是一种很好的做法。为了代替命名类EmailConstruction,我们将其称为EmailMessage,或者类似的东西。

人们使用的细节很多,但是当你到达它时,你会越过那个桥梁!