PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/email.inc.php

https://github.com/mconway/NanobyteCMS
PHP | 202 lines | 152 code | 28 blank | 22 comment | 29 complexity | 9035bf6de66c0c00c3251eac773f53b2 MD5 | raw file
  1. <?php
  2. /*
  3. *Copyright (c) 2009, Michael Conway
  4. *All rights reserved.
  5. *Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. *Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. *Neither the name of the Nanobyte CMS nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10. */
  11. class Email{
  12. private $html;
  13. private $body;
  14. private $dbh;
  15. public function __construct(){
  16. $this->dbh = DbCreator::GetDBObject();
  17. $this->headers = array();
  18. $this->from = EMAIL_FROM;
  19. $this->clearAllRecipients();
  20. $this->subject = EMAIL_SUBJECT;
  21. $this->body = "";
  22. $this->smtp_server = SMTP_SERVER;
  23. $this->smtp_port = SMTP_PORT;
  24. $this->smtp_auth = SMTP_AUTH;
  25. $this->isHTML = EMAIL_IS_HTML;
  26. $this->wordwrap = 70;
  27. $this->addHeader('X-Mailer: Nanobyte CMS Mailer');
  28. }
  29. public function addHeader($header){
  30. array_push($this->headers, $header);
  31. }
  32. public function setFrom($from){
  33. $this->from = $from;
  34. }
  35. public function addRecipient($recipient){
  36. array_push($this->recipients,$recipient);
  37. }
  38. public function addCC($cc){
  39. array_push($recipient,$this->cc);
  40. }
  41. public function addBCC($bcc){
  42. array_push($recipient,$this->bcc);
  43. }
  44. public function setBody($body){
  45. $this->body = wordwrap(trim($body),$this->wordwrap);
  46. // if($this->isHTML)
  47. // $this->body = nl2br($this->body);
  48. }
  49. public function clearAllRecipients(){
  50. $this->recipients = array();
  51. $this->cc = array();
  52. $this->bcc = array();
  53. }
  54. public function setSubject($subject){
  55. $this->subject = $subject;
  56. }
  57. public function sendMessage(){
  58. if($this->html){
  59. $this->addHeader("Content-type: text/html; charset=iso-8859-1");
  60. }else{
  61. $this->addHeader("Content-type: text/plain; charset=iso-8859-1");
  62. }
  63. $this->addHeader("From: ".EMAIL_FROM);
  64. if(!empty($this->cc)){
  65. $this->addHeader("CC: ".implode(', ',$this->cc));
  66. }
  67. if(!empty($this->bcc)){
  68. $this->addHeader("BCC: ".implode(', ',$this->bcc));
  69. }
  70. //maybe change this:
  71. $this->recipients = implode(', ',$this->recipients);
  72. $this->subject = trim($this->subject);
  73. $this->headers = implode("\r\n",$this->headers);
  74. $this->sendSMTP();
  75. // mail(implode(', ',$this->recipients), trim($this->subject), $this->body,implode("\r\n",$this->headers));
  76. }
  77. public function setHTML($html){
  78. $this->isHTML = $html;
  79. }
  80. private function sendSMTP(){ //todo : all failed messages to log if available
  81. // Open an SMTP connection
  82. $this->smtp_socket = fsockopen ($this->smtp_server, $this->smtp_port, $errno, $errstr, 1);
  83. if (!$this->smtp_socket){
  84. Core::SetMessage("Failed to even make an SMTP connection",'error');
  85. return false;
  86. }
  87. $res=fgets($this->smtp_socket,256);
  88. if(substr($res,0,3) != "220"){
  89. Core::SetMessage("Failed to connect",'error');
  90. return false;
  91. }
  92. // Say hello...
  93. fputs($this->smtp_socket, "HELO ".$this->smtp_server."\r\n");
  94. $res=fgets($this->smtp_socket,256);
  95. if(substr($res,0,3) != "250"){
  96. Core::SetMessage("Failed to Introduce",'error');
  97. return false;
  98. }
  99. if($this->smtp_auth=='1'){
  100. // perform authentication
  101. fputs($this->smtp_socket, "auth login\r\n");
  102. $res=fgets($this->smtp_socket,256);
  103. if(substr($res,0,3) != "334"){
  104. Core::SetMessage("Failed to Initiate Authentication");
  105. return false;
  106. }
  107. fputs($this->smtp_socket, base64_encode(Core::DecodeConfParams(SMTP_USER))."\r\n");
  108. $res=fgets($this->smtp_socket,256);
  109. if(substr($res,0,3) != "334"){
  110. Core::SetMessage("Failed to Provide Username for Authentication");
  111. }
  112. fputs($this->smtp_socket, base64_encode(Core::DecodeConfParams(SMTP_PASS))."\r\n");
  113. $res=fgets($this->smtp_socket,256);
  114. if(substr($res,0,3) != "235"){
  115. Core::SetMessage("Failed to Authenticate");
  116. }
  117. }
  118. // Mail from...
  119. fputs($this->smtp_socket, "MAIL FROM: <$this->from>\r\n");
  120. $res=fgets($this->smtp_socket,256);
  121. if(substr($res,0,3) != "250"){
  122. Core::SetMessage("MAIL FROM failed");
  123. }
  124. // Rcpt to...
  125. fputs($this->smtp_socket, "RCPT TO: <$this->recipients>\r\n");
  126. $res=fgets($this->smtp_socket,256);
  127. if(substr($res,0,3) != "250"){
  128. Core::SetMessage("RCPT TO failed");
  129. }
  130. // Data...
  131. fputs($this->smtp_socket, "DATA\r\n");
  132. $res=fgets($this->smtp_socket,256);
  133. if(substr($res,0,3) != "354"){
  134. Core::SetMessage("DATA failed");
  135. }
  136. // Send To:, From:, Subject:, other headers, blank line, message, and finish
  137. // with a period on its own line (for end of message)
  138. fputs($this->smtp_socket, "To: $this->recipients\r\nFrom: $this->from\r\nSubject: $this->subject\r\n$this->headers\r\n\r\n$this->body\r\n.\r\n");
  139. $res=fgets($this->smtp_socket,256);
  140. if(substr($res,0,3) != "250"){
  141. Core::SetMessage("Message Body Failed");
  142. }
  143. // ...And time to quit...
  144. fputs($this->smtp_socket,"QUIT\r\n");
  145. $res=fgets($this->smtp_socket,256);
  146. if(substr($res,0,3) != "221"){
  147. Core::SetMessage("QUIT failed");
  148. }
  149. }
  150. private function useSMTP($smtp){
  151. $this->smtp = $smtp;
  152. }
  153. public function getEmailData($function){
  154. $query = $this->dbh->prepare("SELECT * FROM ".DB_PREFIX."_email WHERE function=:function");
  155. $query->execute(array(':function'=>$function));
  156. return $query->fetch(PDO::FETCH_ASSOC);
  157. }
  158. public function setEmailData($data){
  159. $bind = array(':sub'=>$data['subject'],':body'=>$data['body'],':func'=>$data['function']);
  160. if(isset($data['id']) && !empty($data['id'])){
  161. $query = $this->dbh->prepare('UPDATE '.DB_PREFIX.'_email SET subject=:sub, body=:body, function=:func WHERE id=:id');
  162. $bind[':id'] = $data['id'];
  163. }else{
  164. $query = $this->dbh->prepare('INSERT INTO '.DB_PREFIX.'_email SET subject=:sub, body=:body, function=:func');
  165. }
  166. $query->execute($bind);
  167. if($query->rowCount()==1){
  168. return true;
  169. }else{
  170. return false;
  171. }
  172. }
  173. }
  174. ?>