I am relatively new to OOP. I have an application that requires to send some emails from the server for various use cases. I have moved my code into a Mail class to try to abstract this out. Coming from years as a functional programmer, it is becoming challenging to move into the OOP space. This class is for very generous purposes, and I do not expect to handle attachments, bcc, or any other extra mailer usages. While there are nice and handy Mail packages I can find and implement within a few minutes, I thought it would be best to create this on my own.
My class is as follows:
<?php
/**
* Mail Class
*/
namespace Utility;
class Mailer
{
public $sendTo;
public $sendFrom;
public $subject;
public $message;
public $headers;
public $error = [];
public function __construct($sendTo, $sendFrom = null, $subject, $message)
{
$this->sendTo = $sendTo;
$this->sendFrom = ($sendFrom) ? $sendFrom : '[email protected]';
$this->subject = $subject;
$this->message = $message;
//$this->headers = $this->setHeader();
}
public function setTo($email, $name) {
return $this->sendTo = $email;
}
public function getTo() {
return $this->sendTo;
}
public function setFrom($email, $name) {
return $this->sendFrom = $email;
}
public function setSubject($subject) {
return $this->subject = $subject;
}
public function getSubject() {
return $this->subject;
}
public function setMessage($message) {
$this->message = $message;
//@TODO:string repace, etc
return $this;
}
public function getMessage() {
return $this->message;
}
/*public function setHeader($header, $email, $name = null) {
$address = $this->formatHeader((string) $email, (string) $name);
$this->headers[] = sprintf('%s: %s', (string) $header, $address);
return $this;
}*/
public function setHeader() {
$headers = 'From: '.$this->getFrom() . "\r\n" .
'Reply-To: '.$this->getFrom() . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$this->headers = $headers;
return $this;
}
public function getFrom() {
return $this->sendFrom;
}
public function send() {
$to = $this->sendTo;
$from = $this->sendFrom;
$subject = $this->subject;
$message = $this->message;
//$headers = $this->headers;
$headers = 'From: '.$this->getFrom() . "\r\n" .
'Reply-To: '.$this->getFrom() . "\r\n" .
'X-Mailer: PHP/' . phpversion();
return mail($to, $subject, $message, $headers);
}
}
And to implement the class I do the following in my code:
//alert admin of failed login
$adminFailedAlert = new Mailer('[email protected]', null, 'User Failed Login', 'A user has unsuccessfully logged in at '.Utility::getDateTime().'. You may want to look into this.' );
$adminFailedAlert->send();
This Class works fine for me. The only issue I am having currently is creating dynamic header string - currently I just manually place them into the send() method - which is why you see the commented out header references. I have not implemented the $errors method so will work on that. I guess at this point, am I on the right path? I am also confused why I should have my getter and setter methods when I am not even using it when I implement the Class. I am also trying to overcome to obstacle of knowing when to use private or public, etc method declarations.