My Profile Photo

Personal Webpage of David Duggins


Well, I was born on a normal day in July, 1981 and have been creating chaos ever since. Born in North Carolina, but raised in the aftermath of the Soviet Union, Kazakhstan, I have been messing around with computers nearly my entire life. I wrote my first program in assembly when I was 11. In my early teens I ran a BBS connected to Fidonet and started building a website for my band. In 1999 I was introduced to Linux, and it was love at first compile. I started my career in IT in the early 2000's doing IT for a Car Dealership in Charlotte NC. I wrote my first major web app in Cold Fusion (an ecom app) at that time. In 2006 I left Charlotte and moved down to Columbia where starting working as a developer, freelancer and consultant. Currently I am working as a freelance developer and DevOps consultant!!


Object Oriented PHP Lesson 3 Coding a mailer Class

\n Today we are going to create a mailer class that will assist us in emailing site visitors. Lets start out buy creating the class and the variables we will need.

class Mailer {

}

Okay, let see. We will need a sender, a recipient, a subject and a body. Well, if you know anything about email, the sender is set in the header. So…

class Mailer {
    var $headers; 
    var $recipient;
    var $subject; 
    var $message;  
}

Now comes the fun part! Lets build the email!

First we have a from setter:

function from($email, $name){ 

     $this->headers[] = 'From: '.$email.' ';
 } What we have done, is we have appended the required format for the Header.  In php the '.' is what we use to concatenate strings and variable data together.  Also, note that the header is an array.  We could add more than just "From" to the header if we needed to.

Now we add rest of the functions.

function add_recipient($email){

    $this->recipient = $email;
 }


 function subject($subject){  

    $this->subject = $subject;
 }

 function message($message) {

    $this->message = $message;
 }

Notice that we don’t have any getter methods defined. Although it’s a good practice to have them in your class for a program, it would just add extra stuff to a class that’s sole purpose in life is to run a contact me page. Now, if you wanted to use this in a larger site. Say a cms or a social portal, you would want to add a bunch more functions to it. That way you could get all of your email functionality out of a single class file.

But for now, this is enough. Let’s create a utillity function to send the mail.

function send()
{  //sends the email

    $headers = "";
        foreach($this->headers as $header)
        {
           $headers .= $header.';';
        }
        return mail($this->recipient, $this->subject, $this->message, $headers);
 }

Essentially, we add all the header information, then the rest of the data is pulled into the mail function.

And how do we use this? By now I hope that you understand the layout of my driver files. So it’s your turn. Create a contact form and use this class to make it work.

© 2024 David Duggins. All rights reserved.