PHP Mail Class

This is a very simple mailer class that is also easy to use.

Usage

Using the mail class is easy. Simply create a new ZFmail object, passing the parameters $to,$from,$subject, and $body, then call the method send on the object that you created. It’s as easy as pie. The following example is for a simple form mail script.

Example

10 Responses to “PHP Mail Class”

  1. Nick T Says:

    Not to put you down or anything, but wouldnt it just be quicker to :

    N

  2. Nick T Says:

    Frogot could post php code. Oops :S

    $to = ‘me@thissite.co.uk’;
    $subject = ‘My Subject’;
    $message = ‘Hi, this is my mail message!’;
    $headers = ‘From: donotreply@thissite.co.uk‘ . “\r\n” .
    ‘Reply-To: donotreply@thissite.co.uk‘ . “\r\n” .
    ‘X-Mailer: PHP/’ . phpversion();

    mail($to, $subject, $message, $headers);

    N

  3. Zachary Fox Says:

    @Nick

    Yes, for this example. This is a new post on my blog, but it’s actually a very old page on my site that was just moved into wordpress. I always meant to update it to show how using objects can help when you’re performing more complex operations.

    Even if it’s simply a matter of changing your mail transport, it would be easier to update the class than changing multiple places in the code.

    Perhaps I’ll flesh this out further one day, but I’m working on some more interesting posts right now.

  4. Michael Melen Says:

    I really enjoyed this post (not that I didn’t enjoy the others as well 😉 )- nice work man.

  5. ravi Says:

    nice blog dude

  6. followme Says:

    i like you. get dofollow and i subscribe 😀

  7. Caine rain Says:

    I really wanted to set something up like this for quite some time now!
    Thanks a ton!

  8. tutoriale pc Says:

    I found a class similar to this one, with a few more options. you should upgrade it to support different encoding, content type and some debugging for valid emails.
    for example you may use this:

    if($this->error == ” && mail($this->to,$this->subject,$this->body,$this->headers)){
    return true;
    }else{
    return false;
    }

    and based on this in constructor you should have:

    $this->from = filter_var($from, FILTER_VALIDATE_EMAIL);
    if($this->from === false){
    $this->error = ‘From email adress is not valid’;
    }

    and so on…

    thanks for posting this. some noobs don’t see the fun of writing simple classes and their usefulness.

  9. Shoaib Says:

    I am a newbei & tried to use you class…
    the error I am getting is

    Catchable fatal error: Object of class ZFmail could not be converted to string in eg.php on line ……

    $mail = new ZFmail($to,$from,$subject,$body);

    is that a not working script?

  10. dean Says:

    Frogot could post php code. Oops :S

    $to = ‘me@thissite.co.uk’;
    $subject = ‘My Subject’;
    $message = ‘Hi, this is my mail message!’;
    $headers = ‘From: donotreply@thissite.co.uk‘ . “\r\n” .
    ‘Reply-To: donotreply@thissite.co.uk‘ . “\r\n” .
    ‘X-Mailer: PHP/’ . phpversion();

    mail($to, $subject, $message, $headers);