Technology Temerity

PHP – E-mail Class

E-mailing is one of the more common and versatile ways for webmasters and their customers to keep abreast of events. Naturally databases and logs are best practice, but as a quick and dirty way to get form submissions, error alerts and so on E-mail is tough to beat. Even with an established database E-mail still has its place as an alert mechanism.

Given the above and the ubiquitous presence of E-mail in general, it only makes sense to simplify the process of sending as much as possible. To that end, this PHP class wraps the PHP Mail function to extend capability in such a way that oft repeated coding and formatting is eliminated. Specific goals are:

  • Eliminate the need to include the webmaster in any “To list”; the webmaster is always sent a Bcc by default.
  • Eliminate need to add headers for HTML layout with an option to remove if needed.
  • Enable the simple organization of those oh so common form submission or list E-mails to be formatted into a table layout without any markup in the initial mail string.
  • Provide defaults so the bare minimum of parameters may be used when calling.

Elements

Requirements

  • PHP 5.3+

Files

Copy the following into your class file.

class class_mail
{    

	/*
	class_mail - https://www.caskeys.com/dc/?p=5031
	Damon Vaughn Caskey
	2012_12_10
	
	Mail handler. 
	*/	
		
	const	c_bWMAlert	= TRUE;										//Send webmaster a blind copy?
	const	c_cEDefMsg	= "...";									//Default message.
	const	c_cEHead	= "MIME-Version: 1.0 \r\nContent-type: text/html; charset=iso-8859-1\r\n";	//Default E-mail headers.
	const	c_cESubject	= "From EHS Web";							//Default outgoing E-mail subject.
	const	c_cEWMIn	= "dvcask2@uky.edu";						//Default webmaster's incoming E-mail address.
	const	c_cEWMOut	= "ehs_noreply@uky.edu";					//Default address when server sends mail.
				
	public function mail_send($cMsg=self::c_cEDefMsg, $cSubject=self::c_cESubject, $cTo=self::c_cEWMIn, $cFrom=self::c_cEWMOut, $cBcc=NULL, $bWMAlert=self::c_bWMAlert, $cHeader=self::c_cEHead, $cParams=NULL)
	{	
		/*
		mail_send
		Damon Vaughn Caskey
		2012_12_28
		
		Send HTML mail with standard defaults.
		
		$cMsg:		Body of E-mail.
		$cSubject:	Subject line.
		$cTo:		Outgoing address list.
		$cFrom:		Return address.
		$cBcc:		Blind carbon copy address list.
		$bWMAlert:	Send Bcc to webmaster.
		$cHeader:	Header information.
		$cParams:	Optional parameters.
		*/
	
		$cBody = NULL;	//Final sting for message body.
	
		/*
		Insert From address to header.
		*/
		$cHeader .= "From: ".$cFrom. "\r\n";		
		
		/* 
		If Webmaster alert is on, insert address into Bcc and add to header. Otherwise just add Bcc to header as is.
		*/
		if($bWMAlert===TRUE)
		{			
			$cHeader .= "Bcc: ".self::c_cEWMIn. ", ".$cBcc."\r\n";	
		}
		else
		{
			$cHeader .= "Bcc: ".$cBcc."\r\n";
		}
		
		$cHeader .="\r\n";
		
		/*
		If message passed as a key array, break into list and output as table layout.
		*/		
		if (is_array($cMsg))
		{
			/*
			Initial html and table markup.
			*/
			$cBody = "<html>
						<head>
						  <title>".$cSubject."</title>
						</head>
						<body>
						  <h1>".$cSubject."</h1>
						  <table cellpadding='3'>";
			
			/*
			Get each item in array and place into two column table row.
			*/
			foreach($cMsg as $key => $value)
			{			
				$cBody .= "<tr><th>".$key.":</th><td>".$value."</td></tr>";			
			}	
			
			/*
			Add closing markup.
			*/
			$cBody .= "</table>
					</body>
					</html>";	
		}
		else
		{
			/*
			Output message as is.
			*/
			$cBody = $cMsg;
		}
			
		/*
		Run mail function.
		*/
		return mail($cTo, $cSubject, $cBody, $cHeader, $cParams);		
	}	
}

Now include the file in your script and declare a class object. You are ready to go. Personally I prefer to place common class declarations in a single config file and use injected dependency to avoid multiple objects.

require_once('mail.php');	//Path to mail class.

$oMail = NULL;				//Initialize class object.

$oMail = new class_mail();	//Class object.

Use

Constants

Adjust the constant settings to suit your needs.

const	c_bWMAlert	= TRUE;										//Send webmaster a blind copy?
const	c_cEDefMsg	= "...";									//Default message.
const	c_cEHead	= "MIME-Version: 1.0 \r\nContent-type: text/html; charset=iso-8859-1\r\n";	//Default E-mail headers.
const	c_cESubject	= "From EHS Web";							//Default outgoing E-mail subject.
const	c_cEWMIn	= "dvcask2@uky.edu";						//Default webmaster's incoming E-mail address.
const	c_cEWMOut	= "ehs_noreply@uky.edu";					//Default address when server sends mail.
  • c_bWMAlert: Default option to always send Webmaster a Bcc of the outgoing mail.
  • c_cEDefMsg: Default message text.
  • c_cEHead: Default headers. Included in the class are the headers required to send E-mail with HTML formatting (tables, title, etc.).
  • c_cESubject: Default E-mail subject.
  • c_cEWMIn: Webmaster’s incoming address. By default a Bcc of every mail will be sent to this address.
  • c_cEWMOut: Default “from” address.

Functions

mail_send

Primary (and as of this writing the only) function. Sends an E-mail; includes an optional tabled layout.

public function mail_send($cMsg=self::c_cEDefMsg, $cSubject=self::c_cESubject, $cTo=self::c_cEWMIn, $cFrom=self::c_cEWMOut, $cBcc=NULL, $bWMAlert=self::c_bWMAlert, $cHeader=self::c_cEHead, $cParams=NULL)
	{	
		/*
		mail_send
		Damon Vaughn Caskey
		2012_12_28
		
		Send HTML mail with standard defaults.
		
		$cMsg:		Body of E-mail.
		$cSubject:	Subject line.
		$cTo:		Outgoing address list.
		$cFrom:		Return address.
		$cBcc:		Blind carbon copy address list.
		$bWMAlert:	Send Bcc to webmaster.
		$cHeader:	Header information.
		$cParams:	Optional parameters.
		*/
	
		$cBody = NULL;	//Final sting for message body.
	
		/*
		Insert From address to header.
		*/
		$cHeader .= "From: ".$cFrom. "\r\n";		
		
		/* 
		If Webmaster alert is on, insert address into Bcc and add to header. Otherwise just add Bcc to header as is.
		*/
		if($bWMAlert===TRUE)
		{			
			$cHeader .= "Bcc: ".self::c_cEWMIn. ", ".$cBcc."\r\n";	
		}
		else
		{
			$cHeader .= "Bcc: ".$cBcc."\r\n";
		}
		
		$cHeader .="\r\n";
		
		/*
		If message passed as a key array, break into list and output as table layout.
		*/		
		if (is_array($cMsg))
		{
			/*
			Initial html and table markup.
			*/
			$cBody = "<html>
						<head>
						  <title>".$cSubject."</title>
						</head>
						<body>
						  <h1>".$cSubject."</h1>
						  <table cellpadding='3'>";
			
			/*
			Get each item in array and place into two column table row.
			*/
			foreach($cMsg as $key => $value)
			{			
				$cBody .= "<tr><th>".$key.":</th><td>".$value."</td></tr>";			
			}	
			
			/*
			Add closing markup.
			*/
			$cBody .= "</table>
					</body>
					</html>";	
		}
		else
		{
			/*
			Output message as is.
			*/
			$cBody = $cMsg;
		}
			
		/*
		Run mail function.
		*/
		return mail($cTo, $cSubject, $cBody, $cHeader, $cParams);		
	}

Parameters

Unless noted, parameters are not required. Class defaults will be used for any missing.

  • $cMsg: The message to send (i.e. the body text). If passed as a keyed array, the listed items will be laid out as a table. See below for details.
  • $cSubject: Mail subject.
  • $cTo: Recipient address(s).
  • $cFrom: Sender address.
  • $cBcc: Blind copy address(s).
  • $bWMAlert: Send a blind copy to webmaster.
  • $cHeader: Header options.
  • $cParams: Optional parameters. See PHP Mail for details.

Examples

Send regular E-mail

This call will send a common E-mail to the designated recipients with the automatic webmaster Bcc turned off:

$cMsg         = "Hello world!";
$cSubject    = "Hi";
$cTo        = "someone@somewhere.com, someoneelse@somewhereelse.com";
$cFrom        = "me@myplace.com";

$oMail->mail_send($cMsg, $cSubject, $cTo, $cFrom, NULL, FALSE);
Send table E-mail

A common task for E-mail is to document errors, alerts, form submissions and other types of keyed data that is far easier to read in table format. It’s certainly possible to do this by manually coding table markup into the message, but also repetitive and time consuming.

Instead, you can pass the message as a keyed array; a simple but elegant table from your data will be created and sent to the recipients. The E-mail’s subject will be added as an attention grabbing header just above the table.

$cMsg 	= NULL;

		$cMsg = array(
			"Time"				=>	$this->cErrTOE,
			"Type"				=>	$this->cErrType,
			"IP"				=>	$this->cIP,
			"Def. Source File"	=>	$this->cSource,
			"Source File"		=>	$this->cErrFile,
			"Line"				=>	$this->cErrLine,
			"State"				=>	$this->cErrState,
			"Code"				=>	$this->cErrCode,
			"Message"			=>	$this->cErrMsg,
			"Details"			=>	$this->cErrDetail
		);

$this->oMail->mail_send($cMsg, "Error Report");
Example output:
In this example, an error alert is sent using table layout.
In this example, an error alert is sent using table layout.

Author: Damon Caskey

Hello all, Damon Caskey here - the esteemed owner of this little slice of cyberspace. Welcome!

1 Comment

Leave a Reply