{"id":5031,"date":"2013-01-04T11:51:21","date_gmt":"2013-01-04T16:51:21","guid":{"rendered":"https:\/\/www.caskeys.com\/dc\/?p=5031"},"modified":"2017-01-15T15:21:59","modified_gmt":"2017-01-15T20:21:59","slug":"php-e-mail-class","status":"publish","type":"post","link":"https:\/\/www.caskeys.com\/dc\/php-e-mail-class\/","title":{"rendered":"PHP &#8211; E-mail Class"},"content":{"rendered":"<p>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.<\/p>\n<p>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 <a href=\"http:\/\/us2.php.net\/manual\/en\/book.mail.php\" target=\"_blank\">PHP Mail<\/a> function to extend capability in such a way that oft repeated coding and formatting is eliminated. Specific goals are:<\/p>\n<ul>\n<li>Eliminate the need to include the webmaster in any &#8220;To list&#8221;; the webmaster is always sent a Bcc by default.<\/li>\n<li>Eliminate need to add headers for HTML layout with an option to remove if needed.<\/li>\n<li>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.<\/li>\n<li>Provide defaults so the bare minimum of parameters may be used when calling.<\/li>\n<\/ul>\n<h1>Elements<\/h1>\n<h2>Requirements<\/h2>\n<ul>\n<li>PHP 5.3+<\/li>\n<\/ul>\n<h2>Files<\/h2>\n<p>Copy the following into your class file.<\/p>\n<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\nclass class_mail\r\n{    \r\n\r\n\t\/*\r\n\tclass_mail - https:\/\/www.caskeys.com\/dc\/?p=5031\r\n\tDamon Vaughn Caskey\r\n\t2012_12_10\r\n\t\r\n\tMail handler. \r\n\t*\/\t\r\n\t\t\r\n\tconst\tc_bWMAlert\t= TRUE;\t\t\t\t\t\t\t\t\t\t\/\/Send webmaster a blind copy?\r\n\tconst\tc_cEDefMsg\t= &quot;...&quot;;\t\t\t\t\t\t\t\t\t\/\/Default message.\r\n\tconst\tc_cEHead\t= &quot;MIME-Version: 1.0 \\r\\nContent-type: text\/html; charset=iso-8859-1\\r\\n&quot;;\t\/\/Default E-mail headers.\r\n\tconst\tc_cESubject\t= &quot;From EHS Web&quot;;\t\t\t\t\t\t\t\/\/Default outgoing E-mail subject.\r\n\tconst\tc_cEWMIn\t= &quot;dvcask2@uky.edu&quot;;\t\t\t\t\t\t\/\/Default webmaster's incoming E-mail address.\r\n\tconst\tc_cEWMOut\t= &quot;ehs_noreply@uky.edu&quot;;\t\t\t\t\t\/\/Default address when server sends mail.\r\n\t\t\t\t\r\n\tpublic 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)\r\n\t{\t\r\n\t\t\/*\r\n\t\tmail_send\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_28\r\n\t\t\r\n\t\tSend HTML mail with standard defaults.\r\n\t\t\r\n\t\t$cMsg:\t\tBody of E-mail.\r\n\t\t$cSubject:\tSubject line.\r\n\t\t$cTo:\t\tOutgoing address list.\r\n\t\t$cFrom:\t\tReturn address.\r\n\t\t$cBcc:\t\tBlind carbon copy address list.\r\n\t\t$bWMAlert:\tSend Bcc to webmaster.\r\n\t\t$cHeader:\tHeader information.\r\n\t\t$cParams:\tOptional parameters.\r\n\t\t*\/\r\n\t\r\n\t\t$cBody = NULL;\t\/\/Final sting for message body.\r\n\t\r\n\t\t\/*\r\n\t\tInsert From address to header.\r\n\t\t*\/\r\n\t\t$cHeader .= &quot;From: &quot;.$cFrom. &quot;\\r\\n&quot;;\t\t\r\n\t\t\r\n\t\t\/* \r\n\t\tIf Webmaster alert is on, insert address into Bcc and add to header. Otherwise just add Bcc to header as is.\r\n\t\t*\/\r\n\t\tif($bWMAlert===TRUE)\r\n\t\t{\t\t\t\r\n\t\t\t$cHeader .= &quot;Bcc: &quot;.self::c_cEWMIn. &quot;, &quot;.$cBcc.&quot;\\r\\n&quot;;\t\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t$cHeader .= &quot;Bcc: &quot;.$cBcc.&quot;\\r\\n&quot;;\r\n\t\t}\r\n\t\t\r\n\t\t$cHeader .=&quot;\\r\\n&quot;;\r\n\t\t\r\n\t\t\/*\r\n\t\tIf message passed as a key array, break into list and output as table layout.\r\n\t\t*\/\t\t\r\n\t\tif (is_array($cMsg))\r\n\t\t{\r\n\t\t\t\/*\r\n\t\t\tInitial html and table markup.\r\n\t\t\t*\/\r\n\t\t\t$cBody = &quot;&lt;html&gt;\r\n\t\t\t\t\t\t&lt;head&gt;\r\n\t\t\t\t\t\t  &lt;title&gt;&quot;.$cSubject.&quot;&lt;\/title&gt;\r\n\t\t\t\t\t\t&lt;\/head&gt;\r\n\t\t\t\t\t\t&lt;body&gt;\r\n\t\t\t\t\t\t  &lt;h1&gt;&quot;.$cSubject.&quot;&lt;\/h1&gt;\r\n\t\t\t\t\t\t  &lt;table cellpadding='3'&gt;&quot;;\r\n\t\t\t\r\n\t\t\t\/*\r\n\t\t\tGet each item in array and place into two column table row.\r\n\t\t\t*\/\r\n\t\t\tforeach($cMsg as $key =&gt; $value)\r\n\t\t\t{\t\t\t\r\n\t\t\t\t$cBody .= &quot;&lt;tr&gt;&lt;th&gt;&quot;.$key.&quot;:&lt;\/th&gt;&lt;td&gt;&quot;.$value.&quot;&lt;\/td&gt;&lt;\/tr&gt;&quot;;\t\t\t\r\n\t\t\t}\t\r\n\t\t\t\r\n\t\t\t\/*\r\n\t\t\tAdd closing markup.\r\n\t\t\t*\/\r\n\t\t\t$cBody .= &quot;&lt;\/table&gt;\r\n\t\t\t\t\t&lt;\/body&gt;\r\n\t\t\t\t\t&lt;\/html&gt;&quot;;\t\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t\/*\r\n\t\t\tOutput message as is.\r\n\t\t\t*\/\r\n\t\t\t$cBody = $cMsg;\r\n\t\t}\r\n\t\t\t\r\n\t\t\/*\r\n\t\tRun mail function.\r\n\t\t*\/\r\n\t\treturn mail($cTo, $cSubject, $cBody, $cHeader, $cParams);\t\t\r\n\t}\t\r\n}\r\n\r\n<\/pre>\n<p>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.<\/p>\n<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\nrequire_once('mail.php');\t\/\/Path to mail class.\r\n\r\n$oMail = NULL;\t\t\t\t\/\/Initialize class object.\r\n\r\n$oMail = new class_mail();\t\/\/Class object.\r\n<\/pre>\n<h1>Use<\/h1>\n<h2>Constants<\/h2>\n<p>Adjust the constant settings to suit your needs.<\/p>\n<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\nconst\tc_bWMAlert\t= TRUE;\t\t\t\t\t\t\t\t\t\t\/\/Send webmaster a blind copy?\r\nconst\tc_cEDefMsg\t= &quot;...&quot;;\t\t\t\t\t\t\t\t\t\/\/Default message.\r\nconst\tc_cEHead\t= &quot;MIME-Version: 1.0 \\r\\nContent-type: text\/html; charset=iso-8859-1\\r\\n&quot;;\t\/\/Default E-mail headers.\r\nconst\tc_cESubject\t= &quot;From EHS Web&quot;;\t\t\t\t\t\t\t\/\/Default outgoing E-mail subject.\r\nconst\tc_cEWMIn\t= &quot;dvcask2@uky.edu&quot;;\t\t\t\t\t\t\/\/Default webmaster's incoming E-mail address.\r\nconst\tc_cEWMOut\t= &quot;ehs_noreply@uky.edu&quot;;\t\t\t\t\t\/\/Default address when server sends mail.\r\n<\/pre>\n<ul>\n<li>c_bWMAlert: Default option to always send Webmaster a Bcc of the outgoing mail.<\/li>\n<li>c_cEDefMsg: Default message text.<\/li>\n<li>c_cEHead: Default headers. Included in the class are the headers required to send E-mail with HTML formatting (tables, title, etc.).<\/li>\n<li>c_cESubject: Default E-mail subject.<\/li>\n<li>c_cEWMIn: Webmaster&#8217;s incoming address. By default a Bcc of every mail will be sent to this address.<\/li>\n<li>c_cEWMOut: Default &#8220;from&#8221; address.<\/li>\n<\/ul>\n<h2>Functions<\/h2>\n<h3>mail_send<\/h3>\n<p>Primary (and as of this writing the only) function. Sends an E-mail; includes an optional tabled layout.<\/p>\n<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\npublic 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)\r\n\t{\t\r\n\t\t\/*\r\n\t\tmail_send\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_28\r\n\t\t\r\n\t\tSend HTML mail with standard defaults.\r\n\t\t\r\n\t\t$cMsg:\t\tBody of E-mail.\r\n\t\t$cSubject:\tSubject line.\r\n\t\t$cTo:\t\tOutgoing address list.\r\n\t\t$cFrom:\t\tReturn address.\r\n\t\t$cBcc:\t\tBlind carbon copy address list.\r\n\t\t$bWMAlert:\tSend Bcc to webmaster.\r\n\t\t$cHeader:\tHeader information.\r\n\t\t$cParams:\tOptional parameters.\r\n\t\t*\/\r\n\t\r\n\t\t$cBody = NULL;\t\/\/Final sting for message body.\r\n\t\r\n\t\t\/*\r\n\t\tInsert From address to header.\r\n\t\t*\/\r\n\t\t$cHeader .= &quot;From: &quot;.$cFrom. &quot;\\r\\n&quot;;\t\t\r\n\t\t\r\n\t\t\/* \r\n\t\tIf Webmaster alert is on, insert address into Bcc and add to header. Otherwise just add Bcc to header as is.\r\n\t\t*\/\r\n\t\tif($bWMAlert===TRUE)\r\n\t\t{\t\t\t\r\n\t\t\t$cHeader .= &quot;Bcc: &quot;.self::c_cEWMIn. &quot;, &quot;.$cBcc.&quot;\\r\\n&quot;;\t\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t$cHeader .= &quot;Bcc: &quot;.$cBcc.&quot;\\r\\n&quot;;\r\n\t\t}\r\n\t\t\r\n\t\t$cHeader .=&quot;\\r\\n&quot;;\r\n\t\t\r\n\t\t\/*\r\n\t\tIf message passed as a key array, break into list and output as table layout.\r\n\t\t*\/\t\t\r\n\t\tif (is_array($cMsg))\r\n\t\t{\r\n\t\t\t\/*\r\n\t\t\tInitial html and table markup.\r\n\t\t\t*\/\r\n\t\t\t$cBody = &quot;&lt;html&gt;\r\n\t\t\t\t\t\t&lt;head&gt;\r\n\t\t\t\t\t\t  &lt;title&gt;&quot;.$cSubject.&quot;&lt;\/title&gt;\r\n\t\t\t\t\t\t&lt;\/head&gt;\r\n\t\t\t\t\t\t&lt;body&gt;\r\n\t\t\t\t\t\t  &lt;h1&gt;&quot;.$cSubject.&quot;&lt;\/h1&gt;\r\n\t\t\t\t\t\t  &lt;table cellpadding='3'&gt;&quot;;\r\n\t\t\t\r\n\t\t\t\/*\r\n\t\t\tGet each item in array and place into two column table row.\r\n\t\t\t*\/\r\n\t\t\tforeach($cMsg as $key =&gt; $value)\r\n\t\t\t{\t\t\t\r\n\t\t\t\t$cBody .= &quot;&lt;tr&gt;&lt;th&gt;&quot;.$key.&quot;:&lt;\/th&gt;&lt;td&gt;&quot;.$value.&quot;&lt;\/td&gt;&lt;\/tr&gt;&quot;;\t\t\t\r\n\t\t\t}\t\r\n\t\t\t\r\n\t\t\t\/*\r\n\t\t\tAdd closing markup.\r\n\t\t\t*\/\r\n\t\t\t$cBody .= &quot;&lt;\/table&gt;\r\n\t\t\t\t\t&lt;\/body&gt;\r\n\t\t\t\t\t&lt;\/html&gt;&quot;;\t\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\t\/*\r\n\t\t\tOutput message as is.\r\n\t\t\t*\/\r\n\t\t\t$cBody = $cMsg;\r\n\t\t}\r\n\t\t\t\r\n\t\t\/*\r\n\t\tRun mail function.\r\n\t\t*\/\r\n\t\treturn mail($cTo, $cSubject, $cBody, $cHeader, $cParams);\t\t\r\n\t}\r\n<\/pre>\n<h4>Parameters<\/h4>\n<p>Unless noted, parameters are not required. Class defaults will be used for any missing.<\/p>\n<ul>\n<li>$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.<\/li>\n<li>$cSubject: Mail subject.<\/li>\n<li>$cTo: Recipient address(s).<\/li>\n<li>$cFrom: Sender address.<\/li>\n<li>$cBcc: Blind copy address(s).<\/li>\n<li>$bWMAlert: Send a blind copy to webmaster.<\/li>\n<li>$cHeader: Header options.<\/li>\n<li>$cParams: Optional parameters. See <a href=\"http:\/\/us2.php.net\/manual\/en\/book.mail.php\" target=\"_blank\">PHP Mail<\/a> for details.<\/li>\n<\/ul>\n<h4>Examples<\/h4>\n<h5>Send regular E-mail<\/h5>\n<p>This call will send a common E-mail to the designated recipients with the automatic webmaster Bcc turned off:<\/p>\n<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\n$cMsg         = &quot;Hello world!&quot;;\r\n$cSubject    = &quot;Hi&quot;;\r\n$cTo        = &quot;someone@somewhere.com, someoneelse@somewhereelse.com&quot;;\r\n$cFrom        = &quot;me@myplace.com&quot;;\r\n\r\n$oMail-&gt;mail_send($cMsg, $cSubject, $cTo, $cFrom, NULL, FALSE);\r\n<\/pre>\n<h5>Send table E-mail<\/h5>\n<p>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&#8217;s certainly possible to do this by manually coding table markup into the message, but also repetitive and time consuming.<\/p>\n<p>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&#8217;s subject will be added as an attention grabbing header just above the table.<\/p>\n<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\n$cMsg \t= NULL;\r\n\r\n\t\t$cMsg = array(\r\n\t\t\t&quot;Time&quot;\t\t\t\t=&gt;\t$this-&gt;cErrTOE,\r\n\t\t\t&quot;Type&quot;\t\t\t\t=&gt;\t$this-&gt;cErrType,\r\n\t\t\t&quot;IP&quot;\t\t\t\t=&gt;\t$this-&gt;cIP,\r\n\t\t\t&quot;Def. Source File&quot;\t=&gt;\t$this-&gt;cSource,\r\n\t\t\t&quot;Source File&quot;\t\t=&gt;\t$this-&gt;cErrFile,\r\n\t\t\t&quot;Line&quot;\t\t\t\t=&gt;\t$this-&gt;cErrLine,\r\n\t\t\t&quot;State&quot;\t\t\t\t=&gt;\t$this-&gt;cErrState,\r\n\t\t\t&quot;Code&quot;\t\t\t\t=&gt;\t$this-&gt;cErrCode,\r\n\t\t\t&quot;Message&quot;\t\t\t=&gt;\t$this-&gt;cErrMsg,\r\n\t\t\t&quot;Details&quot;\t\t\t=&gt;\t$this-&gt;cErrDetail\r\n\t\t);\r\n\r\n$this-&gt;oMail-&gt;mail_send($cMsg, &quot;Error Report&quot;);\r\n<\/pre>\n<h6>Example output:<\/h6>\n<figure id=\"attachment_5032\" aria-describedby=\"caption-attachment-5032\" style=\"width: 150px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/www.caskeys.com\/dc\/?attachment_id=5032\" rel=\"attachment wp-att-5032\"><img loading=\"lazy\" decoding=\"async\" class=\"size-thumbnail wp-image-5032\" alt=\"In this example, an error alert is sent using table layout.\" src=\"https:\/\/www.caskeys.com\/dc\/wp-content\/uploads\/2013\/01\/E-mail_error_0001-150x150.png\" width=\"150\" height=\"150\" \/><\/a><figcaption id=\"caption-attachment-5032\" class=\"wp-caption-text\">In this example, an error alert is sent using table layout.<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>PHP Class to simplify sending E-mail and E-mail forms.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[71],"tags":[211,234],"class_list":["post-5031","post","type-post","status-publish","format-standard","hentry","category-technology-temerity","tag-coding-object-oriented","tag-coding-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lNM5-1j9","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/comments?post=5031"}],"version-history":[{"count":20,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5031\/revisions"}],"predecessor-version":[{"id":6225,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5031\/revisions\/6225"}],"wp:attachment":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/media?parent=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/categories?post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/tags?post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}