PHP Mail Class
This is a very simple mailer class that is also easy to use.
<?php /** * mail.php * * A (very) simple mailer class written in PHP. * * @author Zachary Fox * @version 1.0 */ class ZFmail{ var $to = null; var $from = null; var $subject = null; var $body = null; var $headers = null; function ZFmail($to,$from,$subject,$body){ $this->to = $to; $this->from = $from; $this->subject = $subject; $this->body = $body; } function send(){ $this->addHeader('From: '.$this->from."\r\n"); $this->addHeader('Reply-To: '.$this->from."\r\n"); $this->addHeader('Return-Path: '.$this->from."\r\n"); $this->addHeader('X-mailer: ZFmail 1.0'."\r\n"); mail($this->to,$this->subject,$this->body,$this->headers); } function addHeader($header){ $this->headers .= $header; } } ?>
HTML code generated by vim-color-improved v.0.4.0.Download this code: mail.php
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
<?php /** * example/mail.php * * An example script to accept a post and send an email using ZFmail. * * @author Zachary Fox */ // Include the mail.php file that holds the class definition require_once('mail.php'); // First we set the to address. I would not let anyone put in a to // address in a web form, and neither should you. $to = 'me@example.com'; // Then we get the information we need from the $_POST array. // This step is not necessary, but in a production environment, // we would process and sanitize this data here, rather than // passing raw post data to the class. $from = $_POST['from']; $subject = $_POST['subject']; $body = $_POST['body']; // Then create the ZFmail object using the information from above $mail = new ZFmail($to,$from,$subject,$body); // Finally, call the object's send method to deliver the mail. $mail->send(); ?>
HTML code generated by vim-color-improved v.0.4.0.Download this code: mail.php
March 12th, 2008 at 5:14 pm
Not to put you down or anything, but wouldnt it just be quicker to :
N
March 12th, 2008 at 5:17 pm
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
March 13th, 2008 at 10:54 am
@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.
January 23rd, 2009 at 5:07 pm
I really enjoyed this post (not that I didn’t enjoy the others as well
)- nice work man.
January 24th, 2009 at 5:01 am
nice blog dude
March 19th, 2009 at 10:39 am
i like you. get dofollow and i subscribe
June 10th, 2009 at 9:57 pm
I really wanted to set something up like this for quite some time now!
Thanks a ton!