{"id":111,"date":"2009-07-21T11:34:33","date_gmt":"2009-07-21T15:34:33","guid":{"rendered":"http:\/\/www.northatlantawebdesign.com\/?p=111"},"modified":"2009-07-21T11:34:57","modified_gmt":"2009-07-21T15:34:57","slug":"easy-to-use-yet-powerful-php-e-mail-class","status":"publish","type":"post","link":"http:\/\/www.northatlantawebdesign.com\/index.php\/2009\/07\/21\/easy-to-use-yet-powerful-php-e-mail-class\/","title":{"rendered":"Easy to use, yet powerful PHP E-mail Class"},"content":{"rendered":"<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 \/>\n<!--more--><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/******************************************************************************\\\r\n--\r\n--\tClass Name: eMail\r\n--\t\tPurpose: Handles various aspects of sending\/generating e-mails in PHP.\r\n--\t\tCurrent Capabilities:\r\n\t\t\t- Allows multiple &quot;To&quot; recipients\r\n\t\t\t- Allows multiple &quot;Cc&quot; recipients\r\n\t\t\t- Allows multiple &quot;Bcc&quot; recipients\r\n\t\t\t- Allows &quot;From&quot; header to be modified\r\n\t\t\t- Allows &quot;Sender&quot; header to be modified\r\n\t\t\t- Allows &quot;Content-Type&quot; for text or html\r\n\t\t\t- Allows for multiple attachments\r\n\t\t\t- Checks e-mail addresses via regular expressions to verify they\r\n\t\t\t\twill not cause errors\r\n\t\t\t-\r\n--\r\n\\******************************************************************************\/\r\n\r\n\r\n\t\/\/Define class\r\nclass eMail {\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tUser Specific Variables\r\n--\t\tPurpose: Variables which should\/may be modified by the user.  This\r\n--\t\t\tdefines defaults used in the program that can be changed later.\r\n--\r\n\\******************************************************************************\/\r\n\t\/\/Set Default Variables\r\n\tvar $replyTo = 'System &lt;system@example.com&gt;';\t\/\/Default reply address\r\n\tvar $from = 'System &lt;system@example.com&gt;';\t\t\/\/Default From address\r\n\tvar $sender = 'System &lt;system@example.com&gt;';\t\/\/Default Sender address\r\n\tvar $isHTML = false;\t\t\t\t\/\/False sends e-mail in plain text by default\r\n\tvar $nl = &quot;\\n&quot;;\t\t\t\t\t\t\/\/ New Line character, either \\r\\n or \\n\r\n\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tConstant variables\r\n--\t\tPurpose: The constant variables should not be changed\/modified unless\r\n--\t\t\tyou know what you are messing with.\r\n--\r\n\\******************************************************************************\/\r\n\t\/\/Constants\r\n\tvar $errors = null;\t\t\t\t\/\/Array to hold any errors which may occur\r\n\tvar $attachments = null;\t\t\/\/Array to hold attachments\r\n \tvar $mime_boundary = null;\t\t\/\/boundary set for using attachments\r\n\tvar $to = null;\t\t\t\t\t\/\/Array to hold recipients\r\n\tvar $cc = null;\t\t\t\t\t\/\/Array to hold recipients\r\n\tvar $bcc = null;\t\t\t\t\/\/Array to hold recipients\r\n\tvar $header = null;\t\t\t\t\/\/Header used to send e-mail\r\n\tvar $subject = null;\t\t\t\/\/Subject of the e-mail\r\n\tvar $body = null;\t\t\t\t\/\/Message portion of the e-mail\r\n\tvar $message = null;\t\t\t\/\/used to build message when mail is sent\r\n\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tRegular Expressions\r\n--\t\tPurpose: Used in error checking e-mail addresses and various parts of\r\n--\t\t\tthe emails.\r\n--\r\n\\******************************************************************************\/\r\n\r\n \t\/\/Regular Expressions for Error Checking\r\n\t\t\/\/Email address with no name attached\r\n\tvar $regex_email = '^&#x5B;_a-z0-9-]+(\\.&#x5B;_a-z0-9-]+)*@&#x5B;a-z0-9-]+(\\.&#x5B;a-z0-9-]{2,})+$';\r\n\t\t\/\/Email address with name attached\r\n\tvar $regex_email2 = '^(&#x5B;\\._A-Za-z0-9-]+&#x5B; ]+)+&#x5B;&lt;]&#x5B;_a-z0-9-]+(\\.&#x5B;_a-z0-9-]+)*@&#x5B;a-z0-9-]+(\\.&#x5B;a-z0-9-]{2,})+&#x5B;&gt;]$';\r\n \t\t\/\/Regular Expression for limiting body of message\r\n \tvar $regex_body = '^.*$';\r\n \t\t\/\/Regular Expression for limiting subject of message\r\n \tvar $regex_subject = '^.+$';\r\n\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tUser Functions\r\n--\t\tPurpose: Allows user to add recipients, attachments, body, subject,etc.\r\n--\r\n\\******************************************************************************\/\r\n\r\n\t\t\/\/Class Constructor, automatically called when a new object is created\r\n\tfunction eMail(){}\r\n\r\n\t\t\/\/Adds a recipient for the e-mail\r\n\tfunction addTo($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;to&#x5B;sizeOf($this-&gt;to)] = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Adds a Carbon Copy address for the e-mail\r\n\tfunction addCC($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;cc&#x5B;sizeOf($this-&gt;cc)] = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Adds a Blind Carbon Copy address for the e-mail\r\n\tfunction addBCC($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;bcc&#x5B;sizeOf($this-&gt;bcc)] = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Sets the from header for the e-mail\r\n\tfunction setFrom($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;from = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\t$this-&gt;addError(&quot;Invalid Format for FROM e-mail address&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Sets the ReplyTo header for the e-mail\r\n\tfunction setReplyTo($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;replyTo = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\t$this-&gt;addError(&quot;Invalid Format for ReplyTo e-mail address&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Sets the Sender header for the e-mail\r\n\tfunction setSender($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;replyTo = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\t$this-&gt;addError(&quot;Invalid Format for Sender e-mail address&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Sets the From, ReplyTo, and Sender header with one e-mail\r\n\tfunction setSenderInfo($email_addr){\r\n\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t$this-&gt;replyTo = $email_addr;\r\n\t\t\t$this-&gt;sender = $email_addr;\r\n\t\t\t$this-&gt;from = $email_addr;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\t$this-&gt;addError(&quot;Invalid Format for From, Sender, and ReplyTo e-mail addresses&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Sets the subject of the e-mail and verifies that all characters are allowed\r\n\tfunction setSubject($subjectIn){\r\n\t\tif($this-&gt;verify_subject($subjectIn)){\r\n\t\t\t$this-&gt;subject = $subjectIn;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Sets the body of the e-mail and verifies that all characters in the body is allowed\r\n\tfunction setBody($bodyIn, $html_flag = false){\r\n\t\t$this-&gt;isHTML = $html_flag;\r\n\t\tif($this-&gt;verify_body($bodyIn)){\r\n\t\t\t$this-&gt;body = $bodyIn;\r\n\t\t\treturn true;\r\n\t\t} else{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Adds an attachment to be sent with the email\r\n\tfunction addAttachment($fileData, $fileName = &quot;unknown&quot;, $fileType = &quot;&quot;){\r\n\t\t$currSize = sizeOf($this-&gt;attachments);\r\n\t\t$this-&gt;attachments&#x5B;$currSize]&#x5B;0] = $fileData;\r\n\t\t$this-&gt;attachments&#x5B;$currSize]&#x5B;1] = $fileName;\r\n\t\t$this-&gt;attachments&#x5B;$currSize]&#x5B;2] = $fileType;\r\n\t}\r\n\r\n\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tSend Mail Function\r\n--\t\tPurpose: Sends the e-mail after everything is setup.\r\n--\r\n\\******************************************************************************\/\r\n\t\t\/\/Sets the mail headers for HTML, generates all the e-mail parameters and sends it\r\n\tfunction sendMail(){\r\n\t\t\t\/\/Check body content via regular expression\r\n\t\t$this-&gt;verify_body($this-&gt;body);\r\n\t\t\t\/\/Check subject content via regular expression\r\n\t\t$this-&gt;verify_subject($this-&gt;subject);\r\n\r\n\t\t\t\/\/Verify at least on &quot;To&quot;,&quot;Cc&quot;, or &quot;Bcc&quot; address is contained\r\n\t\tif($this-&gt;to==null &amp;&amp; $this-&gt;cc==null &amp;&amp; $this-&gt;bcc==null){\r\n\t\t\t$this-&gt;addError(&quot;E-mail must contain at least one \\&quot;To\\&quot;,\\&quot;Cc\\&quot;, or \\&quot;Bcc\\&quot; address&quot;);\r\n\t\t}\r\n\r\n\t\t\t\/\/Begin to build the header\r\n\t\t$this-&gt;header = &quot;&quot;;\r\n\t\t\t\/\/Set a MIME-version\r\n\t\t$this-&gt;header .= &quot;MIME-version: 1.0&quot; . $this-&gt;nl;\r\n\r\n\t\t\t\/\/Setup Sender, From, Bcc, CC, and ReplyTo regardless of mail format\r\n\t\t$this-&gt;replyTo = $this-&gt;clean_email($this-&gt;replyTo);\r\n\t\tif($this-&gt;replyTo != null){\r\n\t\t\tif($this-&gt;verify_email($this-&gt;replyTo)){\r\n\t\t\t\t$this-&gt;header .= &quot;Reply-To: $this-&gt;replyTo&quot; . $this-&gt;nl;\r\n\t\t\t} else{\r\n\t\t\t\t$this-&gt;addError(&quot;Invalid Reply-To Header: $this-&gt;replyTo&quot;);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\t\/\/Setup sender\r\n\t\t$this-&gt;sender = $this-&gt;clean_email($this-&gt;sender);\r\n\t\tif($this-&gt;sender != null){\r\n\t\t\tif($this-&gt;verify_email($this-&gt;sender)){\r\n\t\t\t\t$this-&gt;header .= &quot;Sender: $this-&gt;sender&quot; . $this-&gt;nl;\r\n\t\t\t} else{\r\n\t\t\t\t$this-&gt;addError(&quot;Invalid Sender Header: $this-&gt;sender&quot;);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\t\/\/Setup from\r\n\t\t$this-&gt;from = $this-&gt;clean_email($this-&gt;from);\r\n\t\tif($this-&gt;from != null){\r\n\t\t\tif($this-&gt;verify_email($this-&gt;from)){\r\n\t\t\t\t$this-&gt;header .= &quot;From: $this-&gt;from&quot; . $this-&gt;nl;\r\n\t\t\t} else{\r\n\t\t\t\t$this-&gt;addError(&quot;Invalid From Header: $this-&gt;from&quot;);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\t\/\/Verify all addresses in cc\r\n\t\tif($this-&gt;cc!=null){\r\n\t\t\tforeach($this-&gt;cc as $email_addr){\r\n\t\t\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\t\t\t$this-&gt;verify_email($email_addr);\r\n\t\t\t\t$email_addr = $email_addr;\r\n\t\t\t\t$this-&gt;header .= &quot;Cc: $email_addr&quot; . $this-&gt;nl;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t\t\/\/Verify all addresses in bcc\r\n\t\tif($this-&gt;bcc != null){\r\n\t\t\tforeach($this-&gt;bcc as $email_addr){\r\n\t\t\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\t\t\t$this-&gt;verify_email($email_addr);\r\n\t\t\t\t$email_addr = $email_addr;\r\n\t\t\t\t$this-&gt;header .= &quot;Bcc: $email_addr&quot; . $this-&gt;nl;\r\n\t\t\t}\r\n\t\t}\r\n\t\t\t\/\/If contains attachments, generate mail with multiple parts\r\n\t\tif($this-&gt;attachments!=null){\r\n\t\t\t\t\/\/Generate a boundary to separate the message body\r\n\t\t\t\t\/\/  and attachments\r\n\t\t\t$this-&gt;mime_boundary = &quot;==Multipart_Boundary_x{&quot;.md5(time()).&quot;}x&quot;;\r\n\r\n\t\t\t\t\/\/Set Content type to multipart for attachments\r\n\t\t\t$this-&gt;header .= &quot;Content-type: multipart\/mixed;&quot; . $this-&gt;nl;\r\n\t\t\t\t\/\/Put boundary in header of e-mail\r\n\t\t\t$this-&gt;header .= &quot; boundary=\\&quot;{$this-&gt;mime_boundary}\\&quot;&quot;;\r\n\r\n\t\t\t\t\/\/Setup the boundary for the body content\r\n            $this-&gt;message .= &quot;--{$this-&gt;mime_boundary}&quot; . $this-&gt;nl;\r\n\r\n\t\t\t\t\/\/If html set content type to that in message, else plain text\r\n\t\t\tif($this-&gt;isHTML){\r\n\t            $this-&gt;message .= &quot;Content-Type: text\/html; charset=ISO-8859-1&quot; . $this-&gt;nl;\r\n\t\t\t} else{\r\n\t\t\t\t$this-&gt;message .= &quot;Content-type: text\/plain; charset=ISO-8859-1&quot; . $this-&gt;nl;\r\n\t\t\t}\r\n\t\t\t\t\/\/Set transfer encoding\r\n\t\t\t$this-&gt;message .= &quot;Content-Transfer-Encoding: 7bit&quot; . $this-&gt;nl . $this-&gt;nl;\r\n\r\n\t\t\t\t\/\/Add the body to the message\r\n\t\t\t$this-&gt;message .= $this-&gt;body . $this-&gt;nl . $this-&gt;nl;\r\n\r\n\t\t\t\t\/\/Add attachments\r\n\t\t\tfor($i=0;$i&lt;sizeOf($this-&gt;attachments);$i++){\r\n\t\t\t\t\t\/\/Put boundary before each attachment\r\n\t\t\t\t$this-&gt;message .= &quot;--{$this-&gt;mime_boundary}&quot; . $this-&gt;nl;\r\n\t\t\t\t\t\/\/Set content type of attachment\r\n\t\t\t\t$this-&gt;message .= &quot;Content-Type: {$this-&gt;attachments&#x5B;$i]&#x5B;2]};&quot; . $this-&gt;nl;\r\n\t\t\t\t\t\/\/Set name for the attachment\r\n\t\t\t\t$this-&gt;message .= &quot; name=\\&quot;&quot;.$this-&gt;attachments&#x5B;$i]&#x5B;1].&quot;\\&quot;&quot; . $this-&gt;nl;\r\n\t\t\t\t\t\/\/Let message know this is an attachment\r\n\t\t\t\t$this-&gt;message .= &quot;Content-Disposition: attachment;&quot; . $this-&gt;nl;\r\n\t\t\t\t\t\/\/Set filename for the attachment\r\n\t\t\t\t$this-&gt;message .= &quot; filename=\\&quot;&quot;.$this-&gt;attachments&#x5B;$i]&#x5B;1].&quot;\\&quot;&quot; . $this-&gt;nl;\r\n\t\t\t\t\t\/\/Set to base64 content encoding\r\n\t\t\t\t$this-&gt;message .= &quot;Content-Transfer-Encoding: base64&quot; . $this-&gt;nl . $this-&gt;nl;\r\n\t\t\t\t\t\/\/Add attachment and encode to base64\r\n\t\t\t\t$this-&gt;message .= chunk_split(base64_encode($this-&gt;attachments&#x5B;$i]&#x5B;0])) . $this-&gt;nl;\r\n\t\t\t}\r\n\t\t\t\t\/\/Close off attachments\r\n\t\t\t$this-&gt;message .= &quot;--{$this-&gt;mime_boundary}--&quot; . $this-&gt;nl . $this-&gt;nl;\r\n\t\t}\r\n\t\t\t\/\/Does not contain attachments, generate mail normally\r\n\t\telse {\r\n\t\t\t\t\/\/If html set header to define it\r\n\t\t\tif($this-&gt;isHTML){\r\n\t\t\t\t$this-&gt;header .= &quot;Content-type: text\/html; charset=iso-8859-1&quot; . $this-&gt;nl;\r\n\t\t\t} else{\r\n\t\t\t\t$this-&gt;header .= &quot;Content-type: text\/plain; charset=ISO-8859-1&quot; . $this-&gt;nl;\r\n\t\t\t}\r\n\t\t\t\t\/\/Add body to the message\r\n\t\t\t$this-&gt;message = $this-&gt;body;\r\n\t\t}\r\n\r\n\t\t\t\/\/Generate To: recipients\r\n\t\t\t$to_string = &quot;&quot;;\r\n\t\tif($this-&gt;to!=null){\r\n\t\t\t$this-&gt;count=0;\r\n\t\t\tforeach($this-&gt;to as $email_addr){\r\n\t\t\t\t$email_addr = $this-&gt;clean_email($email_addr);\r\n\t\t\t\tif($this-&gt;verify_email($email_addr)){\r\n\t\t\t\t\tif($this-&gt;count&gt;0){\r\n\t\t\t\t\t\t$to_string .= ', ';\r\n\t\t\t\t\t}\r\n\t\t\t\t\t$to_string .= $email_addr;\r\n\t\t\t\t\t$this-&gt;count++;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\t\t\/\/If an error has occurred, do not send the message and return the last error\r\n\t\tif($this-&gt;errors!=null){\r\n\t\t\treturn false;\r\n\t\t} else{\r\n\t\t\t\t\/\/Close off the header of the message\r\n\t\t\t$this-&gt;header .= $this-&gt;nl;\r\n\t\t\t\t\/\/Send the message\r\n\t\t\treturn mail($to_string, $this-&gt;subject, $this-&gt;message, $this-&gt;header);\r\n\t\t}\r\n\t}\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tError Functions\r\n--\t\tPurpose: Internal functions to store errors and public ones to retrieve\r\n--\t\t\tthem.\r\n--\r\n\\******************************************************************************\/\r\n\t\t\/\/Returns the array of errors\r\n\tfunction getErrors(){\r\n\t\treturn $this-&gt;errors;\r\n\t}\r\n\r\n\t\t\/\/Returns only the last error which was logged\r\n\tfunction getLastError(){\r\n\t\tif($this-&gt;errors!=null){\r\n\t\t\treturn $this-&gt;errors&#x5B;sizeOf($this-&gt;errors)-1];\r\n\t\t} else {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Adds an error to the error array\r\n\tfunction addError($errorIn){\r\n\t\t$this-&gt;errors&#x5B;sizeOf($this-&gt;errors)] = $errorIn;\r\n\t}\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tMessage Compilation Functions\r\n--\t\tPurpose: Functions which return different components of the message\r\n--\r\n\\******************************************************************************\/\r\n\t\t\/\/Returns the message header\r\n\tfunction getHeader(){\r\n\t\treturn $this-&gt;header;\r\n\t}\r\n\r\n\t\t\/\/Returns the message contents, not generated until after mail is sent\r\n\tfunction getMessage(){\r\n\t\treturn $this-&gt;message;\r\n\t}\r\n\r\n\t\t\/\/Returns the body portion of the e-mail\r\n\tfunction getBody(){\r\n\t\treturn $this-&gt;body;\r\n\t}\r\n\r\n\/******************************************************************************\\\r\n--\r\n--\tError Checking\/Cleansing Functions\r\n--\t\tPurpose: Functions to remove common errors from input and to verify\r\n--\t\t\tdifferent portions of the message with regular expressions\r\n--\r\n\\******************************************************************************\/\r\n\t\t\/\/Removes e-mail addresses of commas and semi-colons which may cause\r\n\t\t\/\/ problems in the message headers\r\n\tfunction clean_email($email_addr){\r\n\t\tif($email_addr!=null){\r\n\t\t\t$email_addr = str_replace(&quot;,&quot;,&quot; &quot;,$email_addr);\r\n\t\t\t$email_addr = str_replace(&quot;;&quot;,&quot; &quot;,$email_addr);\r\n\t\t}\r\n\t\treturn $email_addr;\r\n\t}\r\n\r\n\t\t\/\/Verifies that e-mail addresses in arguments match one form of\r\n\t\t\/\/ e-mail via regular expressions\r\n\tfunction verify_email($email_addr){\r\n\t\tif (eregi($this-&gt;regex_email, $email_addr)) return true;\r\n\t\telse if (eregi($this-&gt;regex_email2, $email_addr)) return true;\r\n\t\telse {\r\n\t\t\t$this-&gt;addError(&quot;Invalid E-mail Address: $email_addr&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Verifies that the body portion of message matches a given\r\n\t\t\/\/ regular expression.  Nice for setting a minimum message length.\r\n\tfunction verify_body($body){\r\n\t\tif (eregi($this-&gt;regex_body, $body)) return true;\r\n\t\telse {\r\n\t\t\t$this-&gt;addError(&quot;Invalid Body Content: $body&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\t\t\/\/Verifies that the subject portion of message matches a given\r\n\t\t\/\/ regular expression.  Nice for setting a minimum subject length.\r\n\tfunction verify_subject($subject){\r\n\t\tif (eregi($this-&gt;regex_subject, $subject)) return true;\r\n\t\telse {\r\n\t\t\t$this-&gt;addError(&quot;Invalid Subject Content: $subject&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t}\r\n\r\n\/******************************************************************************\\\r\n\\******************************************************************************\/\r\n\r\n\r\n\t\/\/Close Class Definition\r\n}\r\n<\/pre>\n<p>Usage Examples:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/******************************************************************************\\\r\n--\r\n--\tProgram Use Examples\r\n--\t\tPurpose: The following are a few examples on using the eMail class in\r\n--\t\t\tan application.\r\n--\r\n\\******************************************************************************\/\r\n\/******************************************************************************\\\r\n\r\nExample 1:  Basic Text E-mail\r\n\t\t\/\/Initialize e-mail object\r\n\t$email = new eMail();\r\n\r\n\t\t\/\/Add a To recipient\r\n\t$email-&gt;addTo(&quot;example@example.com&quot;);\r\n\r\n\t\t\/\/Add a subject for the e-mail\r\n\t$email-&gt;subject = &quot;Test E-mail Subject&quot;;\r\n\r\n\t\t\/\/Set the body portion of the e-mail\r\n\t$email-&gt;setBody('\r\n\tThis is a text body in an e-mail.\r\n\tNothing else is required, it can now be sent.\r\n\t');\r\n\r\n\t\t\/\/Send the e-mail\r\n\t$email-&gt;sendMail();\r\n\r\n--------------------------------------------------------------------------------\r\n\r\nExample 2:  Basic Html E-mail\r\n\t\t\/\/Initialize e-mail object\r\n\t$email = new eMail();\r\n\r\n\t\t\/\/Add a To recipient\r\n\t$email-&gt;addTo(&quot;example@example.com&quot;);\r\n\r\n\t\t\/\/Add a subject for the e-mail\r\n\t$email-&gt;subject = &quot;Test E-mail Subject&quot;;\r\n\r\n\t\t\/\/Set the body portion of the e-mail, setting it to HTML with the\r\n\t\t\/\/ second parameter.\r\n\t$email-&gt;setBody('\r\n\tThis is now a html body in an e-mail.&lt;HR&gt;\r\n\tNothing else is required, it can now be sent.\r\n\t', true);\r\n\r\n\t\t\/\/Send the e-mail\r\n\t$email-&gt;sendMail();\r\n\r\n--------------------------------------------------------------------------------\r\n\r\nExample 3:  Basic Attachment Example with an Html body\r\n\t\t\/\/Initialize e-mail object\r\n\t$email = new eMail();\r\n\r\n\t\t\/\/Add a To recipient\r\n\t$email-&gt;addTo(&quot;example@example.com&quot;);\r\n\r\n\t\t\/\/Add a subject for the e-mail\r\n\t$email-&gt;subject = &quot;Test E-mail Subject&quot;;\r\n\r\n\t\t\/\/Set the body portion of the e-mail, setting it to HTML with the\r\n\t\t\/\/ second parameter.\r\n\t$email-&gt;setBody('\r\n\tThis is now a html body in an e-mail.&lt;HR&gt;\r\n\tNothing else is required, it can now be sent.\r\n\t', true);\r\n\r\n\t\t\/\/Add an attachment to the e-mail\r\n\t$email-&gt;addAttachment(&quot;Text in the attachment&quot;,&quot;filename.txt&quot;,&quot;text\/plain&quot;);\r\n\r\n\t\t\/\/Send the e-mail\r\n\t$email-&gt;sendMail();\r\n\r\n--------------------------------------------------------------------------------\r\n\r\nExample 4:  Multiple Attachments\r\n\t\t\/\/Initialize e-mail object\r\n\t$email = new eMail();\r\n\r\n\t\t\/\/Add a To recipient\r\n\t$email-&gt;addTo(&quot;example@example.com&quot;);\r\n\r\n\t\t\/\/Add a subject for the e-mail\r\n\t$email-&gt;subject = &quot;Test E-mail Subject&quot;;\r\n\r\n\t\t\/\/Set the body portion of the e-mail, setting it to HTML with the\r\n\t\t\/\/ second parameter.\r\n\t$email-&gt;setBody('\r\n\tThis is now a html body in an e-mail.&lt;HR&gt;\r\n\tNothing else is required, it can now be sent.\r\n\t', true);\r\n\r\n\t\t\/\/Attach an html file containing the body of the message\r\n\t$email-&gt;addAttachment($email-&gt;body,&quot;email_body.html&quot;,&quot;text\/html&quot;);\r\n\r\n\t\t\/\/Attach a second file\r\n\t$email-&gt;addAttachment(&quot;File Data&quot;,&quot;Attachment01.txt&quot;);\r\n\r\n\t\t\/\/Send the e-mail\r\n\t$email-&gt;sendMail();\r\n\r\n\\******************************************************************************\/\r\n?&gt;\r\n<\/pre>\n<p>Useful Links:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.php.net\/quickref.php\">PHP Quick Reference<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<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!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[26,34,35,36,79],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-php","tag-class","tag-e-mail","tag-email","tag-email-class","tag-php"],"_links":{"self":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":7,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":118,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/posts\/111\/revisions\/118"}],"wp:attachment":[{"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.northatlantawebdesign.com\/index.php\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}