PHP Email Class

With the number of different emails a website may send, I find it easy to stay consistent in keeping one email format.

I decided to build a small PHP Email Class to send HTML e-mails.
Here is the class, below is the way it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
    error_reporting(0);
    class email{
        var $subject = '';
        var $from = 'test@example.com';   //THIS IS THE EMAIL PEOPLE WILL REPLY TO
        var $to = '';
        var $message = '';
        var $logo = 'true';
        var $title = '';
   
        function __construct(){
            $this->headers = "From: " . strip_tags($this->from) . "\r\n";
            $this->headers .= "Reply-To: ". strip_tags($this->from) . "\r\n";
            //$this->headers .= "CC: test@example.com\r\n";     //IF YOU WANT TO MAKE A CARBON COPY
            $this->headers .= "MIME-Version: 1.0\r\n";
            $this->headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        }
       
        function subject($subject){
            $this->subject = $subject;
        }
       
        function title($title){
            $this->title = '<h3>'.$title.'</h3>';
        }
       
        function logo(){
                    //HTML code will go here for the logo of the e-mail, this could include a link (Remember it has to absolute)
                    //EXAMPLE
                    return '<img src="http://mysite.com/images/logo.png"/>';
        }
       
        function to($to){
            //THIS NEEDS TO BE EITHER ONE VALUE OR AN ARRAY
            //user@example.com, anotheruser@example.com    Array('test@test.com','test@test.com');
            //User <user@example.com>    Array(0 => Array('user','test@test.com'));
            //User <user@example.com>, Another User <anotheruser@example.com>   Array(0
            $this->to = $to;
        }
       
        function from($from){
            if(isset($from)){
                $this->from = $from;
            }
        }
       
        function message($msg){
            $this->message = '';
            if($this->logo == 'true'){
                $this->message .= $this->logo();
            }
            $this->message .= $this->title;
             $this->message .= $msg;
        }
       
        function send(){
            if($this->to == '' || ($this->message == '' && $this->subject == '') ){
                //NO NAME
                return false;
            }
           
            if(is_array($this->to)){
                //MULTIPLE
                foreach($this->to as $key => $var){
                    if(isset($var[1])){
                        //NAMES
                        mail($var[0].'<'.$var[1].'>', $this->subject, $this->message, $this->headers);
                    }else{
                        //JUST EMAILS
                        mail($var[0], $this->subject, $this->message, $this->headers);
                    }
                }
            }else{
                //ONE EMAIL
                mail($this->to, $this->subject, $this->message, $this->headers);
            }
        }
       
        function __destroy(){
            $this->subject = null;
            $this->from = null;
            $this->to = null;
            $this->message = null;
        }
    }
?>

So how easy is it to get working???

1
2
3
4
5
6
7
8
require_once 'email.class.php';
$email = new email();
$email->to('user@example.com');
$email->subject('Welcome to my site');
$email->title('Hi there!');
$email->message('Just wanted to welcome you to my site');
$email->send();
$email->__destroy();

That’s how easy it is to send an HTML e-mail with that class.
Some notes

  • Will accept no subject or no message but not both
  • The above is expecting a logo, to remove the logo fully just use $email->logo = ‘false’;
  • There is a comment in the code on how to send to multiple people, example below
1
2
3
4
5
6
7
8
9
10
11
   $email = Array(
        0 => 'me@me.com',
        1 => 'test@test.com'
    );


    //OR
    $email = Array(
        0 => Array('Jack','jack@hill.com'),
        1 => Array('Jill','jill@hill.com')
    );

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>