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. 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. 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:
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:
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
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:
<?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 $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:
<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. You've only set it within the sendMail
class, but you used $this->contents
. Try setting private $contents;
as an instance variable, or just use $contents
on this line:
$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.
Try to use phpmailer library(smtp) or try to upload script to hosting with mail enabled server.
<?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. If you want to be able to set it like this, then it should be declared as public. Also, there is no $toEmail
field in the HTML form you posted, so you probably want to write toEmail
here.
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 $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.
$sendmail = mail($this->toEmail, $this->subject, $this->message, $this->contents, $headers);
The mail()
function may be disabled by the hosting provider. 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();