Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RonB1985's avatar

Simple PHP contact class

I've been learning more and more web development lately, watching videos here at laracasts, also following two official online courses in regards to PHP and one for AJAX, and I'm kinda stuck at the moment because I'm doing a lot of different things at the same time. So I decided to write a little PHP email class to keep myself busy which I want to use later for small web projects.

I'm just looking for different inputs and views from others on this simple class. I know there is no validation yet, and no spam protection and so on, but other than that, any input on what I can improve or are there any (major) errors with this class?

I'm not really looking for improvements such as the option to add an attachment or to change the priority, I'm just looking for insights on THIS class, as is.

Edit: I haven't commented anything since it's pretty straightforward and I will do that later when the class grows, but for displaying it here right now, it doesn't need any commenting imo. But yes, that is one thing I can improve indeed ;-)

class Email {

    private $recipient = '[email protected]';
    private $name;
    private $email;
    private $subject;
    private $message;
    private $headers = [];

    public function __construct()
    {
        $this->addHeader('MIME-Version: 1.0');
        $this->addHeader('X-Priority: 3');
        $this->addHeader('X-MSMail-Priority: Normal');
        $this->addHeader('X-Mailer: PHP/' . phpversion());
        $this->addHeader('Content-Type: text/html; charset=iso-8859-1');
    }

    public function addHeader($header)
    {
        $this->headers[] = $header;
    }

    public function setSender($name, $email)
    {
        $this->name = $this->sanitizeData($name);
        $this->email = $this->sanitizeData($email);

        $this->addHeader('From: ' . $this->name . ' <' . $this->email . '>');
    }

    public function setSubject($subject)
    {
        $this->subject = $this->sanitizeData($subject);
    }

    public function setMessage($message)
    {
        $this->message = nl2br($this->sanitizeData($message));
    }

    public function sendMail()
    {
        mail($this->recipient, $this->subject, $this->message, implode("\r\n", $this->headers));
    }

    function sanitizeData($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);

        return $data;
    }
}

// Usage:

$mail = new Email;
$mail->setSender('My name', 'My email');
$mail->setSubject('My subject');
$mail->setMessage('My message');
$mail->sendMail();
0 likes
7 replies
WebKenth's avatar
WebKenth
Best Answer
Level 16

Looks OK, an improvement could be made in the constructor to allow an array to be passed to quickly populate the object. something like:

$mail = new Email([ 
    'sender' => 'sender', 
    'subject' => 'subject', 
    'message' => 'message'
]);
$mail->send();

what if you wanted to attach an image or a pdf to the email? :)

And would it not be more appropriate to define the recipient rather than the sender?

I would be careful with the sanitizement with trimming and stripping slashes, maybe add the sanitize as an option? defaulting to true.

1 like
RonB1985's avatar

@ejdelmonico, thanks, wasn't aware of those, well, only of a few :)

@webKenth, The recipient is defined, not the sender, or what do you mean?

Thanks for that tip on the quickly populating, that will improve it indeed.

WebKenth's avatar

@RonB1985 It would make more sense if i could choose who would receive the email, not who sends it.

Currently your class can only send an email to [email protected].

The sender should be whoever is using your class, which could be set in a config file

RonB1985's avatar

@WebKenth, Ah yeah I see what you mean. My class is meant as a contact form on a site, where a user can just use the script to send an e-mail to the webmaster or owner, for example. An in this case I only need to submit the sender's name, e-mail address and message, not even the subject, since that would be something like "Webpage Contact Form".

So as for the passing an array to the constructor, I never done anything like that, would this be appropriate?

    public function __construct($data)
    {
        $this->setSender($data['name'], $data['email']);
        $this->setSubject($data['subject']);
        $this->setMessage($data['message']);
    }


$mail = new Email([
    'name' => 'My name',
    'email' => 'My email',
    'subject' => 'My subject',
    'message' => 'My message'
]);
WebKenth's avatar

@RonB1985 close :) i would set it to null as default and only set it if it is an array and not null, you could add more check to be sure it has name/email ect.

public function __construct($data = null)
{
    if(is_array($data) && $data)
    {
            $this->setSender($data['name'], $data['email']);
            $this->setSubject($data['subject']);
            $this->setMessage($data['message']);
    }
}
1 like

Please or to participate in this conversation.