PHP Mail Class
This is a very simple mailer class that is also easy to use.
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 |
<?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; } } ?> |
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
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 |
<?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(); ?> |
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!
December 9th, 2010 at 1:13 am
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.
July 8th, 2011 at 12:47 am
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?
March 2nd, 2012 at 1:04 am
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);