简体   繁体   English

面向对象的PHP电子邮件联系表单未发送

[英]Object Oriented PHP Email Contact Form Not Sending

I have done quite a few contact forms in PHP no problem, however I'm very new to oop and I can't quite wrap my head around how this should work. 我已经用PHP完成了很多联系表单,但没有问题,但是我是新手,我无法完全理解应该如何工作。 I'm working with a tutor who is helping me self teach this, basically giving me resources to learn oop, but I'm stuck on this. 我正在与一位导师合作,这位导师正在帮助我自我学习,基本上给了我学习oop的资源,但我仍然坚持这样做。 I was to make a simple contact form that send the user info to the email specified on the same form. 我要制作一个简单的联系表单,将用户信息发送到同一表单上指定的电子邮件中。 These were my requirements: 这些是我的要求:

The User class should: User类应:

  • Contain the relevant submitted data as properties of the class 包含相关的提交数据作为类的属性

  • Have a constructor method that will let you declare the class and set the data in one statement 有一个构造函数方法,使您可以声明该类并在一条语句中设置数据

The Email class should: Email类应:

  • Be static 保持静态

  • Have a method that will let you send the email in one statement 有一种方法可以让您在一份声明中发送电子邮件

  • Have a method that will replace the formatting tags ([b] / [i] / [[name]]) with the proper equivalent 有一种方法可以将格式标记([b] / [i] / [[name]])替换为适当的等效方法

  • Have proper access to the methods (private / public) 可以正确使用方法(私有/公共)

I'm not worried about the validation or syntax conversion right now as I just want to work through why the email isn't sending. 我现在不担心验证或语法转换,因为我只想了解为什么电子邮件不发送。

runner.php: Runner.php:

<?php
class User  {

public $firstName;
public $lastName;
public $toEmail;
public $fromName;
public $fromEmail;
public $subject;
public $message;

public function post() {
if (isset($_POST['Submit'])) {
$mail = new email($_POST['firstName'], $_POST['lastName'], $_POST['toEmail'],     $_POST['fromName'], $_POST['fromEmail'], $_POST['subject'], $_POST['message']);
$mail->toEmail = $POST['$toEmail'];
$mail->sendMail();
    }
  }
}

class email {

private $fristName;
private $lastName;
private $toEmail;
private $fromName;
private $fromEmail;
private $subject;
private $message;

public static function __construct($firstName, $lastName, $toEmail, $fromName, $fromEmail, $subject, $message) {
    $this->firstName = $firstName;
    $this->lastName = $lastName;
    $this->toEmail = $toEmail;
    $this->fromName = $fromName;
    $this->fromEmail = $fromEmail;
    $this->subject = $subject;
    $this->firstName = $message;
}

 function sendMail($mail) {
    $contents = "To: $firstName&nbsp;$lastName<br />From: $fromName<br />Subject: $subject<br />Message: $message";
    $headers = "From:" . $fromEmail;
    $sendmail = mail($this->toEmail, $this->subject, $this->message, $this->contents, $headers);
    }
}
?>

form.html: form.html:

<html>
<head>
</head>
<body>
<form action="runner.php" method="POST">
<b>To</b></br>
<label>First Name</label>
<input type="text" name="firstName" id="firstName"></br>
<label>Last Name</label>
<input type="text" name="lastName" id="lastName"></br>
<label>Email</label>
<input type="text" name="toEmail" id="toEmail"></br></br>
<b>From</b></br>
<label>Name</label>
<input type="text" name="fromName" id="fromName"></br>
<label>Email</label>
<input type="text" name="fromEmail" id="fromEmail"></br></br>
<b>Compose Email</b></br>
[b]<b>bold</b>[\b] and [i]<i>italic</i>[\i] tags permitted in email body</br>
<label>Subject</label>
<input type="text" name="subject" id="subject"></br>
<label>Body</label>
<textarea name="message" id="message"></textarea></br></br></br>
<input type="submit" value="Submit"></form>
</body>
</html>

I don't even know if I'm on the right track with this. 我什至不知道我是否在正确的轨道上。 Any help/resources would be greatly appreciated. 任何帮助/资源将不胜感激。

I might be wrong, but I think it's to do with your $contents variable. 我可能是错的,但我认为这与您的$contents变量有关。 You've only set it within the sendMail class, but you used $this->contents . 您仅在sendMail类中进行了设置,但是使用了$this->contents Try setting private $contents; 尝试设置private $contents; as an instance variable, or just use $contents on this line: 作为实例变量,或仅在此行上使用$contents

$sendmail = mail($this->toEmail, $this->subject, $this->message, $this->contents, $headers);

(should be:) (应该:)

$sendmail = mail($this->toEmail, $this->subject, $this->message, $contents, $headers);

IF you are working in local, php mail function will not work. 如果您在本地工作,php邮件功能将无法工作。

Try to use phpmailer library(smtp) or try to upload script to hosting with mail enabled server. 尝试使用phpmailer库(smtp)或尝试将脚本上传到启用了邮件的服务器上进行托管。

<?php
class User  {

public $firstName;
public $lastName;
public $toEmail;
public $fromName;
public $fromEmail;
public $subject;
public $message;

public function post() {
if (isset($_POST['Submit'])) {
$mail = new email($_POST['firstName'], $_POST['lastName'], $_POST['toEmail'],     $_POST['fromName'], $_POST['fromEmail'], $_POST['subject'], $_POST['message']);
$mail->toEmail = $POST['$toEmail'];

toEmail is a private property. toEmail是私有财产。 If you want to be able to set it like this, then it should be declared as public. 如果希望能够这样设置,则应将其声明为public。 Also, there is no $toEmail field in the HTML form you posted, so you probably want to write toEmail here. 另外,您发布的HTML表单中没有$toEmail字段,因此您可能想在此处写入toEmail

Also, you already set this value in the constructor. 另外,您已经在构造函数中设置了此值。

$mail->sendMail();
    }
  }
}

class email {

It is a general convention that classes should start with a capital letter, but this is mainly a style issue. 通常,类应以大写字母开头,但这主要是样式问题。

private $fristName;
private $lastName;
private $toEmail;
private $fromName;
private $fromEmail;
private $subject;
private $message;

public static function __construct($firstName, $lastName, $toEmail, $fromName, $fromEmail, $subject, $message) {

You cannot have static constructors... You should either make this a normal constructor, or, if you want the class to only contain static properties, then remove it altogether. 您不能具有静态构造函数...您应该使它成为普通的构造函数,或者,如果您希望类仅包含静态属性,则将其完全删除。 If you want the properties to be static though, you should define them as such in the code above. 但是,如果您希望属性为静态,则应在上面的代码中定义它们。

Also, if this must be a class containing only static properties, then you likely don't want to instantiate it. 另外,如果这必须是仅包含静态属性的类,则您可能不想实例化它。

    $this->firstName = $firstName;
    $this->lastName = $lastName;
    $this->toEmail = $toEmail;
    $this->fromName = $fromName;
    $this->fromEmail = $fromEmail;
    $this->subject = $subject;
    $this->firstName = $message;
}

 function sendMail($mail) {
    $contents = "To: $firstName&nbsp;$lastName<br />From: $fromName<br />Subject: $subject<br />Message: $message";
    $headers = "From:" . $fromEmail;

$firstName , $lastName , ..., are not available here, you probably mean to use $this->firstName , ..., etc. $firstName$lastName ,...在这里不可用,您可能打算使用$this->firstName ,...等。

    $sendmail = mail($this->toEmail, $this->subject, $this->message, $this->contents, $headers);

The mail() function may be disabled by the hosting provider. 托管提供商可能会禁用mail()函数。 You should see an error message if that happens. 如果发生这种情况,您应该会看到一条错误消息。

    }
}
?>

You're not instantiating any of these classes. 您没有实例化任何这些类。 You probably want to do something like: 您可能想要执行以下操作:

$user = new User();
$user->post();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM