<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>North Atlanta Web Design &#187; email class</title>
	<atom:link href="https://www.northatlantawebdesign.com/index.php/tag/email-class/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.northatlantawebdesign.com</link>
	<description>Programming Examples, Samples, and Tutorials</description>
	<lastBuildDate>Mon, 15 Aug 2011 20:18:35 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>Easy to use, yet powerful PHP E-mail Class</title>
		<link>https://www.northatlantawebdesign.com/index.php/2009/07/21/easy-to-use-yet-powerful-php-e-mail-class/</link>
		<comments>https://www.northatlantawebdesign.com/index.php/2009/07/21/easy-to-use-yet-powerful-php-e-mail-class/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 15:34:33 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Gibeau]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[E-mail]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[email class]]></category>

		<guid isPermaLink="false">http://www.northatlantawebdesign.com/?p=111</guid>
		<description><![CDATA[This is a pretty robust PHP Email class that I wrote a few years back. Has the capability to do text or html e-mails with multiple attachments. Enjoy! Usage Examples: Useful Links: PHP Quick Reference]]></description>
				<content:encoded><![CDATA[<p>This is a pretty robust PHP Email class that I wrote a few years back.  Has the capability to do text or html e-mails with multiple attachments.  Enjoy!<br />
<span id="more-111"></span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
/******************************************************************************\
--
--	Class Name: eMail
--		Purpose: Handles various aspects of sending/generating e-mails in PHP.
--		Current Capabilities:
			- Allows multiple &quot;To&quot; recipients
			- Allows multiple &quot;Cc&quot; recipients
			- Allows multiple &quot;Bcc&quot; recipients
			- Allows &quot;From&quot; header to be modified
			- Allows &quot;Sender&quot; header to be modified
			- Allows &quot;Content-Type&quot; for text or html
			- Allows for multiple attachments
			- Checks e-mail addresses via regular expressions to verify they
				will not cause errors
			-
--
\******************************************************************************/


	//Define class
class eMail {

/******************************************************************************\
--
--	User Specific Variables
--		Purpose: Variables which should/may be modified by the user.  This
--			defines defaults used in the program that can be changed later.
--
\******************************************************************************/
	//Set Default Variables
	var $replyTo = 'System &lt;system@example.com&gt;';	//Default reply address
	var $from = 'System &lt;system@example.com&gt;';		//Default From address
	var $sender = 'System &lt;system@example.com&gt;';	//Default Sender address
	var $isHTML = false;				//False sends e-mail in plain text by default
	var $nl = &quot;\n&quot;;						// New Line character, either \r\n or \n


/******************************************************************************\
--
--	Constant variables
--		Purpose: The constant variables should not be changed/modified unless
--			you know what you are messing with.
--
\******************************************************************************/
	//Constants
	var $errors = null;				//Array to hold any errors which may occur
	var $attachments = null;		//Array to hold attachments
 	var $mime_boundary = null;		//boundary set for using attachments
	var $to = null;					//Array to hold recipients
	var $cc = null;					//Array to hold recipients
	var $bcc = null;				//Array to hold recipients
	var $header = null;				//Header used to send e-mail
	var $subject = null;			//Subject of the e-mail
	var $body = null;				//Message portion of the e-mail
	var $message = null;			//used to build message when mail is sent


/******************************************************************************\
--
--	Regular Expressions
--		Purpose: Used in error checking e-mail addresses and various parts of
--			the emails.
--
\******************************************************************************/

 	//Regular Expressions for Error Checking
		//Email address with no name attached
	var $regex_email = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$';
		//Email address with name attached
	var $regex_email2 = '^([\._A-Za-z0-9-]+[ ]+)+[&lt;][_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+[&gt;]$';
 		//Regular Expression for limiting body of message
 	var $regex_body = '^.*$';
 		//Regular Expression for limiting subject of message
 	var $regex_subject = '^.+$';


/******************************************************************************\
--
--	User Functions
--		Purpose: Allows user to add recipients, attachments, body, subject,etc.
--
\******************************************************************************/

		//Class Constructor, automatically called when a new object is created
	function eMail(){}

		//Adds a recipient for the e-mail
	function addTo($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;to[sizeOf($this-&gt;to)] = $email_addr;
			return true;
		} else{
			return false;
		}
	}

		//Adds a Carbon Copy address for the e-mail
	function addCC($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;cc[sizeOf($this-&gt;cc)] = $email_addr;
			return true;
		} else{
			return false;
		}
	}

		//Adds a Blind Carbon Copy address for the e-mail
	function addBCC($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;bcc[sizeOf($this-&gt;bcc)] = $email_addr;
			return true;
		} else{
			return false;
		}
	}

		//Sets the from header for the e-mail
	function setFrom($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;from = $email_addr;
			return true;
		} else{
			$this-&gt;addError(&quot;Invalid Format for FROM e-mail address&quot;);
			return false;
		}
	}

		//Sets the ReplyTo header for the e-mail
	function setReplyTo($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;replyTo = $email_addr;
			return true;
		} else{
			$this-&gt;addError(&quot;Invalid Format for ReplyTo e-mail address&quot;);
			return false;
		}
	}

		//Sets the Sender header for the e-mail
	function setSender($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;replyTo = $email_addr;
			return true;
		} else{
			$this-&gt;addError(&quot;Invalid Format for Sender e-mail address&quot;);
			return false;
		}
	}

		//Sets the From, ReplyTo, and Sender header with one e-mail
	function setSenderInfo($email_addr){
		$email_addr = $this-&gt;clean_email($email_addr);
		if($this-&gt;verify_email($email_addr)){
			$this-&gt;replyTo = $email_addr;
			$this-&gt;sender = $email_addr;
			$this-&gt;from = $email_addr;
			return true;
		} else{
			$this-&gt;addError(&quot;Invalid Format for From, Sender, and ReplyTo e-mail addresses&quot;);
			return false;
		}
	}

		//Sets the subject of the e-mail and verifies that all characters are allowed
	function setSubject($subjectIn){
		if($this-&gt;verify_subject($subjectIn)){
			$this-&gt;subject = $subjectIn;
			return true;
		} else{
			return false;
		}
	}

		//Sets the body of the e-mail and verifies that all characters in the body is allowed
	function setBody($bodyIn, $html_flag = false){
		$this-&gt;isHTML = $html_flag;
		if($this-&gt;verify_body($bodyIn)){
			$this-&gt;body = $bodyIn;
			return true;
		} else{
			return false;
		}
	}

		//Adds an attachment to be sent with the email
	function addAttachment($fileData, $fileName = &quot;unknown&quot;, $fileType = &quot;&quot;){
		$currSize = sizeOf($this-&gt;attachments);
		$this-&gt;attachments[$currSize][0] = $fileData;
		$this-&gt;attachments[$currSize][1] = $fileName;
		$this-&gt;attachments[$currSize][2] = $fileType;
	}



/******************************************************************************\
--
--	Send Mail Function
--		Purpose: Sends the e-mail after everything is setup.
--
\******************************************************************************/
		//Sets the mail headers for HTML, generates all the e-mail parameters and sends it
	function sendMail(){
			//Check body content via regular expression
		$this-&gt;verify_body($this-&gt;body);
			//Check subject content via regular expression
		$this-&gt;verify_subject($this-&gt;subject);

			//Verify at least on &quot;To&quot;,&quot;Cc&quot;, or &quot;Bcc&quot; address is contained
		if($this-&gt;to==null &amp;&amp; $this-&gt;cc==null &amp;&amp; $this-&gt;bcc==null){
			$this-&gt;addError(&quot;E-mail must contain at least one \&quot;To\&quot;,\&quot;Cc\&quot;, or \&quot;Bcc\&quot; address&quot;);
		}

			//Begin to build the header
		$this-&gt;header = &quot;&quot;;
			//Set a MIME-version
		$this-&gt;header .= &quot;MIME-version: 1.0&quot; . $this-&gt;nl;

			//Setup Sender, From, Bcc, CC, and ReplyTo regardless of mail format
		$this-&gt;replyTo = $this-&gt;clean_email($this-&gt;replyTo);
		if($this-&gt;replyTo != null){
			if($this-&gt;verify_email($this-&gt;replyTo)){
				$this-&gt;header .= &quot;Reply-To: $this-&gt;replyTo&quot; . $this-&gt;nl;
			} else{
				$this-&gt;addError(&quot;Invalid Reply-To Header: $this-&gt;replyTo&quot;);
			}
		}

			//Setup sender
		$this-&gt;sender = $this-&gt;clean_email($this-&gt;sender);
		if($this-&gt;sender != null){
			if($this-&gt;verify_email($this-&gt;sender)){
				$this-&gt;header .= &quot;Sender: $this-&gt;sender&quot; . $this-&gt;nl;
			} else{
				$this-&gt;addError(&quot;Invalid Sender Header: $this-&gt;sender&quot;);
			}
		}

			//Setup from
		$this-&gt;from = $this-&gt;clean_email($this-&gt;from);
		if($this-&gt;from != null){
			if($this-&gt;verify_email($this-&gt;from)){
				$this-&gt;header .= &quot;From: $this-&gt;from&quot; . $this-&gt;nl;
			} else{
				$this-&gt;addError(&quot;Invalid From Header: $this-&gt;from&quot;);
			}
		}

			//Verify all addresses in cc
		if($this-&gt;cc!=null){
			foreach($this-&gt;cc as $email_addr){
				$email_addr = $this-&gt;clean_email($email_addr);
				$this-&gt;verify_email($email_addr);
				$email_addr = $email_addr;
				$this-&gt;header .= &quot;Cc: $email_addr&quot; . $this-&gt;nl;
			}
		}

			//Verify all addresses in bcc
		if($this-&gt;bcc != null){
			foreach($this-&gt;bcc as $email_addr){
				$email_addr = $this-&gt;clean_email($email_addr);
				$this-&gt;verify_email($email_addr);
				$email_addr = $email_addr;
				$this-&gt;header .= &quot;Bcc: $email_addr&quot; . $this-&gt;nl;
			}
		}
			//If contains attachments, generate mail with multiple parts
		if($this-&gt;attachments!=null){
				//Generate a boundary to separate the message body
				//  and attachments
			$this-&gt;mime_boundary = &quot;==Multipart_Boundary_x{&quot;.md5(time()).&quot;}x&quot;;

				//Set Content type to multipart for attachments
			$this-&gt;header .= &quot;Content-type: multipart/mixed;&quot; . $this-&gt;nl;
				//Put boundary in header of e-mail
			$this-&gt;header .= &quot; boundary=\&quot;{$this-&gt;mime_boundary}\&quot;&quot;;

				//Setup the boundary for the body content
            $this-&gt;message .= &quot;--{$this-&gt;mime_boundary}&quot; . $this-&gt;nl;

				//If html set content type to that in message, else plain text
			if($this-&gt;isHTML){
	            $this-&gt;message .= &quot;Content-Type: text/html; charset=ISO-8859-1&quot; . $this-&gt;nl;
			} else{
				$this-&gt;message .= &quot;Content-type: text/plain; charset=ISO-8859-1&quot; . $this-&gt;nl;
			}
				//Set transfer encoding
			$this-&gt;message .= &quot;Content-Transfer-Encoding: 7bit&quot; . $this-&gt;nl . $this-&gt;nl;

				//Add the body to the message
			$this-&gt;message .= $this-&gt;body . $this-&gt;nl . $this-&gt;nl;

				//Add attachments
			for($i=0;$i&lt;sizeOf($this-&gt;attachments);$i++){
					//Put boundary before each attachment
				$this-&gt;message .= &quot;--{$this-&gt;mime_boundary}&quot; . $this-&gt;nl;
					//Set content type of attachment
				$this-&gt;message .= &quot;Content-Type: {$this-&gt;attachments[$i][2]};&quot; . $this-&gt;nl;
					//Set name for the attachment
				$this-&gt;message .= &quot; name=\&quot;&quot;.$this-&gt;attachments[$i][1].&quot;\&quot;&quot; . $this-&gt;nl;
					//Let message know this is an attachment
				$this-&gt;message .= &quot;Content-Disposition: attachment;&quot; . $this-&gt;nl;
					//Set filename for the attachment
				$this-&gt;message .= &quot; filename=\&quot;&quot;.$this-&gt;attachments[$i][1].&quot;\&quot;&quot; . $this-&gt;nl;
					//Set to base64 content encoding
				$this-&gt;message .= &quot;Content-Transfer-Encoding: base64&quot; . $this-&gt;nl . $this-&gt;nl;
					//Add attachment and encode to base64
				$this-&gt;message .= chunk_split(base64_encode($this-&gt;attachments[$i][0])) . $this-&gt;nl;
			}
				//Close off attachments
			$this-&gt;message .= &quot;--{$this-&gt;mime_boundary}--&quot; . $this-&gt;nl . $this-&gt;nl;
		}
			//Does not contain attachments, generate mail normally
		else {
				//If html set header to define it
			if($this-&gt;isHTML){
				$this-&gt;header .= &quot;Content-type: text/html; charset=iso-8859-1&quot; . $this-&gt;nl;
			} else{
				$this-&gt;header .= &quot;Content-type: text/plain; charset=ISO-8859-1&quot; . $this-&gt;nl;
			}
				//Add body to the message
			$this-&gt;message = $this-&gt;body;
		}

			//Generate To: recipients
			$to_string = &quot;&quot;;
		if($this-&gt;to!=null){
			$this-&gt;count=0;
			foreach($this-&gt;to as $email_addr){
				$email_addr = $this-&gt;clean_email($email_addr);
				if($this-&gt;verify_email($email_addr)){
					if($this-&gt;count&gt;0){
						$to_string .= ', ';
					}
					$to_string .= $email_addr;
					$this-&gt;count++;
				}
			}
		}
			//If an error has occurred, do not send the message and return the last error
		if($this-&gt;errors!=null){
			return false;
		} else{
				//Close off the header of the message
			$this-&gt;header .= $this-&gt;nl;
				//Send the message
			return mail($to_string, $this-&gt;subject, $this-&gt;message, $this-&gt;header);
		}
	}

/******************************************************************************\
--
--	Error Functions
--		Purpose: Internal functions to store errors and public ones to retrieve
--			them.
--
\******************************************************************************/
		//Returns the array of errors
	function getErrors(){
		return $this-&gt;errors;
	}

		//Returns only the last error which was logged
	function getLastError(){
		if($this-&gt;errors!=null){
			return $this-&gt;errors[sizeOf($this-&gt;errors)-1];
		} else {
			return false;
		}
	}

		//Adds an error to the error array
	function addError($errorIn){
		$this-&gt;errors[sizeOf($this-&gt;errors)] = $errorIn;
	}

/******************************************************************************\
--
--	Message Compilation Functions
--		Purpose: Functions which return different components of the message
--
\******************************************************************************/
		//Returns the message header
	function getHeader(){
		return $this-&gt;header;
	}

		//Returns the message contents, not generated until after mail is sent
	function getMessage(){
		return $this-&gt;message;
	}

		//Returns the body portion of the e-mail
	function getBody(){
		return $this-&gt;body;
	}

/******************************************************************************\
--
--	Error Checking/Cleansing Functions
--		Purpose: Functions to remove common errors from input and to verify
--			different portions of the message with regular expressions
--
\******************************************************************************/
		//Removes e-mail addresses of commas and semi-colons which may cause
		// problems in the message headers
	function clean_email($email_addr){
		if($email_addr!=null){
			$email_addr = str_replace(&quot;,&quot;,&quot; &quot;,$email_addr);
			$email_addr = str_replace(&quot;;&quot;,&quot; &quot;,$email_addr);
		}
		return $email_addr;
	}

		//Verifies that e-mail addresses in arguments match one form of
		// e-mail via regular expressions
	function verify_email($email_addr){
		if (eregi($this-&gt;regex_email, $email_addr)) return true;
		else if (eregi($this-&gt;regex_email2, $email_addr)) return true;
		else {
			$this-&gt;addError(&quot;Invalid E-mail Address: $email_addr&quot;);
			return false;
		}
	}

		//Verifies that the body portion of message matches a given
		// regular expression.  Nice for setting a minimum message length.
	function verify_body($body){
		if (eregi($this-&gt;regex_body, $body)) return true;
		else {
			$this-&gt;addError(&quot;Invalid Body Content: $body&quot;);
			return false;
		}
	}

		//Verifies that the subject portion of message matches a given
		// regular expression.  Nice for setting a minimum subject length.
	function verify_subject($subject){
		if (eregi($this-&gt;regex_subject, $subject)) return true;
		else {
			$this-&gt;addError(&quot;Invalid Subject Content: $subject&quot;);
			return false;
		}
	}

/******************************************************************************\
\******************************************************************************/


	//Close Class Definition
}
</pre>
<p>Usage Examples:</p>
<pre class="brush: php; title: ; notranslate">
/******************************************************************************\
--
--	Program Use Examples
--		Purpose: The following are a few examples on using the eMail class in
--			an application.
--
\******************************************************************************/
/******************************************************************************\

Example 1:  Basic Text E-mail
		//Initialize e-mail object
	$email = new eMail();

		//Add a To recipient
	$email-&gt;addTo(&quot;example@example.com&quot;);

		//Add a subject for the e-mail
	$email-&gt;subject = &quot;Test E-mail Subject&quot;;

		//Set the body portion of the e-mail
	$email-&gt;setBody('
	This is a text body in an e-mail.
	Nothing else is required, it can now be sent.
	');

		//Send the e-mail
	$email-&gt;sendMail();

--------------------------------------------------------------------------------

Example 2:  Basic Html E-mail
		//Initialize e-mail object
	$email = new eMail();

		//Add a To recipient
	$email-&gt;addTo(&quot;example@example.com&quot;);

		//Add a subject for the e-mail
	$email-&gt;subject = &quot;Test E-mail Subject&quot;;

		//Set the body portion of the e-mail, setting it to HTML with the
		// second parameter.
	$email-&gt;setBody('
	This is now a html body in an e-mail.&lt;HR&gt;
	Nothing else is required, it can now be sent.
	', true);

		//Send the e-mail
	$email-&gt;sendMail();

--------------------------------------------------------------------------------

Example 3:  Basic Attachment Example with an Html body
		//Initialize e-mail object
	$email = new eMail();

		//Add a To recipient
	$email-&gt;addTo(&quot;example@example.com&quot;);

		//Add a subject for the e-mail
	$email-&gt;subject = &quot;Test E-mail Subject&quot;;

		//Set the body portion of the e-mail, setting it to HTML with the
		// second parameter.
	$email-&gt;setBody('
	This is now a html body in an e-mail.&lt;HR&gt;
	Nothing else is required, it can now be sent.
	', true);

		//Add an attachment to the e-mail
	$email-&gt;addAttachment(&quot;Text in the attachment&quot;,&quot;filename.txt&quot;,&quot;text/plain&quot;);

		//Send the e-mail
	$email-&gt;sendMail();

--------------------------------------------------------------------------------

Example 4:  Multiple Attachments
		//Initialize e-mail object
	$email = new eMail();

		//Add a To recipient
	$email-&gt;addTo(&quot;example@example.com&quot;);

		//Add a subject for the e-mail
	$email-&gt;subject = &quot;Test E-mail Subject&quot;;

		//Set the body portion of the e-mail, setting it to HTML with the
		// second parameter.
	$email-&gt;setBody('
	This is now a html body in an e-mail.&lt;HR&gt;
	Nothing else is required, it can now be sent.
	', true);

		//Attach an html file containing the body of the message
	$email-&gt;addAttachment($email-&gt;body,&quot;email_body.html&quot;,&quot;text/html&quot;);

		//Attach a second file
	$email-&gt;addAttachment(&quot;File Data&quot;,&quot;Attachment01.txt&quot;);

		//Send the e-mail
	$email-&gt;sendMail();

\******************************************************************************/
?&gt;
</pre>
<p>Useful Links:</p>
<ul>
<li><a href="http://www.php.net/quickref.php">PHP Quick Reference</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://www.northatlantawebdesign.com/index.php/2009/07/21/easy-to-use-yet-powerful-php-e-mail-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
