PHP Email Class

Fri, 1 Oct 2010

I made a new email class (Open Source GPL) for you. I felt it would be fitting to branch the To/From together in the construct, and the Subject/Message in a separate method (content was the most logical name I could think). This is better than having a method for all four properties, I think. There is also a Bcc method that can be used, just use CSV (Comma Separated Values) as noted in the phpDoc.

I think it requires creativity to simplify things in an intelligible way, I am trying my best to accomplish this.

Download, or View below (The download contains the latest version):
<?php
/**
 * Email 0.1a
 *
 * @author		JREAM
 * @link		http://www.jream.com
 * @copyright	2010 Jesse Boyer (contact@jream.com)
 * @license		GNU General Public License 3 (http://www.gnu.org/licenses/)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details:
 * http://www.gnu.org/licenses/
 *
 * @uses

	$Email = new Email('sendto@thisperson.com', 'you@yoursite.com');
	$Email->Content('Welcome Email', 'Hey there, I just wanted to welcome you!');
	$Email->Send();

	// To see the result (boolean)
	echo $Email->Result;
 *
 */

class Email
{

	/** For checking if the email sent after you use Send() */
	public $Result = 0;

	/**	Sending Details */
	private $_To;
	private $_From;
	private $_Bcc;
	
	/** Content Details */
	private $_Subject;
	private $_Message;
	
	/**
	 * @param  $to Who is the email going to?
	 * @param  $from Who is the email coming from? (You)
	 */
	public function construct($to, $from)
	{
		$this->_To = $to;
		$this->_From = $from;
	}

	/**
	 * @param  $subject The subject line of the email
	 * @param  $message All the contents of the message.
	 */
	public function Content($subject, $message)
	{
		$this->_Subject = $subject;
		$this->_Message = $message;
	}

	/**
	 * @param  $bcc Use CSV format for BCC
	 */	
	public function Bcc($bcc)
	{
		$this->_Bcc = $bcc;
	}

	
	/**
	 * @desc Sends the email, by default this is sent in HTML format just because it will revert to text
	 * 		 if the users html feature is disabled.
	 */	
	public function Send()
	{
	
		$headers =	"From: {$this->_From}" . "\r\n";
		$headers .=	"Reply-To: {$this->_From}" . "\r\n";
		$headers .=	"Bcc: {$this->_Bcc}" . "\r\n";
		$headers .=	'MIME-Version: 1.0' . "\r\n" ;
		$headers .=	'Content-type: text/html; charset=iso-8859-1' . "\r\n" ;
		$headers .=	'X-Mailer: PHP/' . phpversion();

		if (mail($this->_To, $this->_Subject, $this->_Message, $headers))
		$this->Result = 1;
	}

}