<?php
/*
version 1.1 added html messages
*/
/**
*
* a simple php mailer class
*
* @author Harold Kim E. Cantil
* @copyright Harold Kim E. Cantil 2007-2008
* @version $1.1$
* @package none
* @subpackage none
*/
class Mailer{
/**
* mail headers
* @var string
* @access private
*/
var $headers;
/**
* mail subject
* @var string
* @access private
*/
var $subject = "";
/**
* mail message
* @var string
* @access private
*/
var $message = "";
/**
* mail sender - name <email>
* @var string
* @access private
*/
var $sender = "";
/**
* mail send address
* @var string
* @access private
*/
var $to = "";
/**
* mail cc recipients
* @var string
* @access private
*/
var $cc = "";
/**
* mail bcc recipients
* @var string
* @access private
*/
var $bcc = "";
/**
* mail reply address
* @var string
* @access private
*/
var $replyTo = "";
/**
* mail sending error
* @var string
* @access private
*/
var $error = FALSE;
/**
*
* class constructor
*
* @return none
* @access public
* @param none
*/
function Mailer(){
}
/**
*
* set if mail sending has an error
*
* @return bool - returns TRUE if error occur and FALSE if has none
* @access public
*/
function hasError(){
return $this->error;
}
/**
*
* sets the mail subject
*
* @access public
* @param $subject - the mail subject
*/
function setSubject( $subject = "" ) {
$this->subject = $subject;
}
/**
*
* get the mail subject
*
* @return string - the mail subject
* @access public
*/
function getSubject(){
return $this->subject;
}
/**
*
* sets the mail message
*
* @access public
* @param $message - the email message
*/
function setMessage( $message ){
$this->message = $message;
}
/**
*
* get the mail message
*
* @return string - the mail message
* @access public
*/
function getMessage(){
return $this->message;
}
/**
*
* sets the senders name and senders email
*
* @access public
* @param $senderName - mail sender name
* @param $senderEmail - sender's mail
*/
function setSender( $senderName = "" , $senderEmail = "" ) {
if( $senderEmail ){
$senderEmail = "<" . $this->encode( $senderEmail ) .">";
}
if ( $senderName ){
$senderName = $senderName ;
}
$this->sender = $senderName . $senderEmail ;
}
/**
*
* get the senders name and email address
*
* @return string - the senders name and senders email name
* @access public
*/
function getSender(){
return $this->sender;
}
/**
*
* sets the mail cc
*
* @access public
* @param $cc - can be a string or an array - mail cc address
*/
function setCc( $cc ) {
if ( is_array( $cc ) ) {
$this->cc = implode ( $cc , "," );
}else {
$this->cc = $cc;
}
}
/**
*
* get the mail cc
*
* @return string - the mail cc
* @access public
*/
function getCc(){
return $this->cc;
}
/**
*
* sets the mail bcc
*
* @access public
* @param $bcc - can be a string or array of email address
*/
function setBcc( $bcc ) {
if ( is_array( $bcc ) ) {
$this->bcc = implode( $bcc , "," );
}else {
$this->bcc = $bcc;
}
}
/**
*
* gets the mail bcc addresses
*
* @return string - mail bcc
* @access public
*/
function getBcc(){
return $this->bcc;
}
/**
*
* sets the mail reply to
*
* @access public
* @param $replyTo (string) - sets the mail reply address
*/
function setReplyTo( $replyTo ){
$this->replyTo = $replyTo ;
}
/**
*
* get the mail reply to
*
* @return string - mail reply address
* @access public
*/
function getReplyTo(){
return $this->replyTo;
}
/**
*
* get the mail headers
*
* @return string - mail headers
* @access public
*/
function getHeaders(){
return $this->headers;
}
/**
*
* create the mail headers
*
* @return none
* @access private
*/
function createHeaders(){
// sender
$this->headers = "From: ". $this->sender . "\r\n" ;
$this->headers .= "Content-type: text/html;\r\n";
// reply address
if( $this->replyTo ) {
$this->headers .= "Reply-To: " . $this->replyTo . "\r\n";
}
// cc recipients
if ( $this->cc ) {
$this->headers .= "Cc: " . $this->cc . "\r\n";
}
// bcc recipients
if ( $this->bcc ) {
$this->headers .= "Bcc: " . $this->bcc . "\r\n";
}
}
/**
*
* sends the email
*
* @return none
* @access public
* @param $to - can be a string or an array of emails
*/
function sendTo ( $to ) {
$this->createHeaders();
$this->error = FALSE;
// mass mailer
if ( is_array( $to ) ){
$flag = FALSE;
foreach( $to as $val ) {
if ( !mail ( $val , $this->subject , $this->message ,
$this->headers ) ) {
if ( !$flag ) {
$this->error = TRUE;
$flag = TRUE;
}
}
}
// 1 email only
}else{
if ( !mail ( $to , $this->subject , $this->message ,
$this->headers ) ){
$this->error = TRUE;
}
}
}
/**
*
* sends mail enconding
*
* @return string - the string with proper encoding
* @access public
* @param $inStr - the input string
* @param $charset - the character encoding
*/
function encode( $inStr , $charset = "" ) {
if ($charset){
$charset_ = $charset;
}
$outStr = $inStr;
if ( $outStr && $charset_ ){
// define start delimimter, end delimiter and spacer
$end = "?=";
$start = "=?" . $charset_ . "?B?";
$spacer = $end . "\r\n " . $start;
// determine length of encoded text within chunks
// and ensure length is even
$length = 75 - strlen($start) - strlen($end);
$length = floor($length/2) * 2; $senderName = "<$senderName>";
// encode the string and split it into chunks
// with spacers after each chunk
$outStr = base64_encode($outStr);
$outStr = chunk_split($outStr, $length, $spacer);
// remove trailing spacer and
// add start and end delimiters
$spacer = preg_quote($spacer);
$outStr = preg_replace("/" . $spacer . "$/", "", $outStr);
$outStr = $start . $outStr . $end;
}
return $outStr;
}
}
?>
|