Simple Email Class
<?php
/*
* PHP Email Class
*
* Class to deal with sending emails in PHP
* Makes it easy to send emails in PHP with attachments and such.
*
* Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com
*
* LICENSE
* ---
* Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used.
*
* USAGE
* --
*
* $objEmail = new RVJ_mail();
*
* $objEmail->addUser("jacob@redvodkajelly.com");
* $objEmail->setFrom("Frozensheep Ltd <admin@frozensheep.com>");
* $objEmail->setHTML();
* //$objEmail->setPlain();
* $objEmail->setPriority(3);
* $objEmail->addReadConfirmationEmail("admin@frozensheep.com");
* $objEmail->addAttachment("logo.gif", ‘logo.gif', ‘image/gif');
* $objEmail->addAttachment("word.doc", ‘word.doc', ‘application/msword');
* $objEmail->addAttachment("excel.xls", ‘excel.xls', ‘application/vnd.ms-excel');
* $objEmail->setSubject("Testing new class");
* $objEmail->setContent("hey <b>dude</b>");
* $objEmail->send();
*
*/
class RVJ_mail {
var $arrTo = array();
var $arrCC = array();
var $arrBCC = array();
var $strContent = "";
var $strSubject = "";
var $arrHeader = array();
var $arrAttachments = array();
var $numPriority = 3;
var $strCharSet = "utf-8";
var $strContentType = "text/plain";
var $strEncoding = "8bit";
/*
*
* @Method: addUser
* @Parameters: 1
* @Param-1: strAddress - String - Holds an email address
* @Description: Adds a user to send the email to
*
*/
function addUser($strAddress){
if($this->emailCheck($strAddress)){
$this->arrTo[] = $strAddress;
return 1;
}else{
return 0;
}
}
/*
*
* @Method: removeUser
* @Parameters: 1
* @Param-1: strAddress - String - Holds an email address
* @Description: Removes an address from the array
*
*/
function removeUser($strAddress){
//create array or arrays to check
$arrVars = array('arrTo', 'arrCC', 'arrBCC');
foreach($arrVars as $resVars){
//sees if the item exists in the array
$numKey = array_search($strAddress, $this->{$resVars});
if(isset($numKey)){
//remove the email address
$this->{$resVars}[$numKey] = "";
}
unset($numKey);
}
}
/*
*
* @Method: addCC
* @Parameters: 1
* @Param-1: strAddress - String - Holds an email address
* @Description: Adds a CC user to send to
*
*/
function addCC($strAddress){
if($this->emailCheck($strAddress)){
$this->arrCC[] = $strAddress;
return 1;
}else{
return 0;
}
}
/*
*
* @Method: addBCC
* @Parameters: 1
* @Param-1: strAddress - String - Holds an email address
* @Description: Adds a BCC user to send to
*
*/
function addBCC($strAddress){
if($this->emailCheck($strAddress)){
$this->arrBCC[] = $strAddress;
return 1;
}else{
return 0;
}
}
/*
*
* @Method: addReplyTo
* @Parameters: 1
* @Param-1: strEmail - String - Holds the reply-to email address
* @Description: Adds a reply-to email address
*
*/
function addReplyTo($strEmail){
if($this->emailCheck($strEmail)){
$this->addHeader("Reply-to: $strEmail");
}
}
/*
*
* @Method: setContent
* @Parameters: 1
* @Param-1: strText - String - Holds the email content
* @Description: Adds the content of the email
*
*/
function setContent($strText){
$this->strContent = $strText;
}
/*
*
* @Method: setSubject
* @Parameters: 1
* @Param-1: strText - String - Holds the email subject
* @Description: Adds the email subject
*
*/
function setSubject($strText){
$this->strSubject = $strText;
}
/*
*
* @Method: setFrom
* @Parameters: 1
* @Param-1: strText - String - Holds the details of who the email is from
* @Description: Adds the sender info to the headers
*
*/
function setFrom($strText){
$this->addHeader("From: $strText");
}
/*
*
* @Method: setHTML
* @Parameters: 0
* @Description: Sets the email to allow html
*
*/
function setHTML(){
$this->strContentType = "text/html";
}
/*
*
* @Method: setPlain
* @Parameters: 0
* @Description: Sets the email to plain text
*
*/
function setPlain(){
$this->strContentType = "text/plain";
}
/*
*
* @Method: setPriority
* @Parameters: 1
* @Param-1: numPriority - Number - The email priority
* @Description: Sets the email priority from 1 - Urgent to 5 - not so
*
*/
function setPriority($numPriority){
$this->numPriority = $numPriority;
}
/*
*
* @Method: addReadConfirmationEmail
* @Parameters: 1
* @Param-1: strEmail - String - Email address to send read confirmation to
* @Description: Ensures that a confirmation email is sent when read
*
*/
function addReadConfirmationEmail($strEmail){
if($this->emailCheck($strEmail)){
$this->addHeader("Disposition-Notification-To: <$strEmail>");
}
}
/*
*
* @Method: addHeader
* @Parameters: 1
* @Param-1: strText - String - Holds the header details
* @Description: Adds the header details
*
*/
function addHeader($strText){
$this->arrHeader[] = $strText;
}
/*
*
* @Method: addAttachment
* @Parameters: 3
* @Param-1: strFile - String - The file that you want to attach
* @Param-1: strName - String - The name of the attachment you want displayed
* @Param-1: strType - String - The MIME type of the file
* @Description: Adds an attachment to be sent with the email
*
*/
function addAttachment($strFile, $strName, $strType){
//check to make sure the file exists
if(file_exists($strFile)){
$this->arrAttachments[] = array('path' => $strFile, 'name' => $strName, 'type' => $strType);
}
}
/*
*
* @Method: send
* @Parameters: 0
* @Description: Sends the email to all the required people
*
*/
function send(){
//get all the emails in a string to use
$strTo = implode(", ", $this->arrTo);
//add any CC addresses if needed
if($this->arrCC){
$this->addHeader("Cc: ".implode(", ", $this->arrCC));
}
//add any BCC addresses if needed
if($this->arrBCC){
$this->addHeader("Bcc: ".implode(", ", $this->arrBCC));
}
//append any attachments to the end of the content
if(count($this->arrAttachments)){
$this->appendAttachments();
}
//add the additional headers
$this->addAdditionalHeaders();
//implode the headers as they need to be in a single string
$strHeader = implode("\r\n", $this->arrHeader);
//send the email
mail($strTo, $this->strSubject, $this->strContent, $strHeader);
}
/*
*
* @Method: appendAttachments
* @Parameters: 0
* @Description: Appends any attachments to the end of the email content
*
*/
function appendAttachments(){
//create a unique boundary value
$strBoundary = "H2O-".time();
//add the boundary to the headers
$this->addHeader("Content-Type: multipart/alternitive; boundary=$strBoundary");
//if there is content to the email already we need to add the boundary and content type
if($this->strContent){
$strContent = "-$strBoundary\r\n";
$strContent .= "Content-Transfer-Encoding: {$this->strEncoding}\r\n";
$strContent .= "Content-type: {$this->strContentType}; charset={$this->strCharSet}\r\n\r\n";
$this->strContent = $strContent.$this->strContent."\r\n\r\n\r\n";
}
//loop through all the attachments
foreach($this->arrAttachments as $arrFile){
//read the content of the file into a string and base64 encode and split into the correct chunk size
$strData = chunk_split(base64_encode(implode("", file($arrFile['path']))));
//add the attachments
$strAttachment = "-$strBoundary\r\n";
$strAttachment .= "Content-Transfer-Encoding: base64\r\n";
$strAttachment .= "Content-Type: {$arrFile['type']}; name={$arrFile['name']}\r\n";
$strAttachment .= "Content-Disposition: attachment; filename={$arrFile['name']}\r\n\r\n";
$strAttachment .= "$strData\r\n";
//add the attachment to the email content
$this->strContent .= $strAttachment;
unset($strAttachment);
}
//add the closing boundary
$this->strContent .= "-$strBoundary-";
}
/*
*
* @Method: addAdditionalHeaders
* @Parameters: 0
* @Description: Adds additional headers for content type and priority etc
*
*/
function addAdditionalHeaders(){
$this->addHeader("MIME-Version: 1.0");
//add the content type and charset if there are no attachments
if(!count($this->arrAttachments)){
$this->addHeader("Content-type: {$this->strContentType}; charset={$this->strCharSet}");
}
//add the priority
$this->addHeader("X-Priority: $this->numPriority");
}
/*
*
* @Method: emailCheck
* @Parameters: 1
* @Param-1: strAddress - String - Holds an email address
* @Description: Checks to ensure that the email address is valid
*
*/
function emailCheck($strAddress){
if(ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $strAddress)){
return 1;
}else{
return 0;
}
}
}
?>
Popularity: 5% [?]
No comments yet.