PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/prolabBeta/includes/smtp.php

https://github.com/lucasgoicoechea/prolab
PHP | 248 lines | 129 code | 32 blank | 87 comment | 22 complexity | 0d3f4dcfbaff80224492a7fc5d45ab76 MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. * smtp.php
  4. * -------------------
  5. * begin : Wed May 09 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : support@phpbb.com
  8. *
  9. * $Id: smtp.php,v 1.16.2.5 2003/06/12 19:20:20 acydburn Exp $
  10. *
  11. ***************************************************************************/
  12. /***************************************************************************
  13. * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  14. *
  15. * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  16. * and debugging completed by the Elite Nukers and site members.
  17. *
  18. * You run this package at your sole risk. Nuke Cops and affiliates cannot
  19. * be held liable if anything goes wrong. You are advised to test this
  20. * package on a development system. Backup everything before implementing
  21. * in a production environment. If something goes wrong, you can always
  22. * backout and restore your backups.
  23. *
  24. * Installing and running this also means you agree to the terms of the AUP
  25. * found at Nuke Cops.
  26. *
  27. * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  28. * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  29. * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  30. * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  31. * invalid_session error message.
  32. ***************************************************************************/
  33. /***************************************************************************
  34. * This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  35. * by Tom Nitzschner (tom@toms-home.com)
  36. * http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  37. *
  38. * As always, make a backup before messing with anything. All code
  39. * release by me is considered sample code only. It may be fully
  40. * functual, but you use it at your own risk, if you break it,
  41. * you get to fix it too. No waranty is given or implied.
  42. *
  43. * Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  44. * then on my site. All original header code and copyright messages will be maintained
  45. * to give credit where credit is due. If you modify this, the only requirement is
  46. * that you also maintain all original copyright messages. All my work is released
  47. * under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  48. *
  49. ***************************************************************************/
  50. /***************************************************************************
  51. *
  52. * This program is free software; you can redistribute it and/or modify
  53. * it under the terms of the GNU General Public License as published by
  54. * the Free Software Foundation; either version 2 of the License, or
  55. * (at your option) any later version.
  56. *
  57. ***************************************************************************/
  58. define('SMTP_INCLUDED', 1);
  59. //
  60. // This function has been modified as provided
  61. // by SirSir to allow multiline responses when
  62. // using SMTP Extensions
  63. //
  64. function server_parse($socket, $response, $line = __LINE__)
  65. {
  66. while (substr($server_response, 3, 1) != ' ')
  67. {
  68. if (!($server_response = fgets($socket, 256)))
  69. {
  70. message_die(GENERAL_ERROR, "Couldn't get mail server response codes", "", $line, __FILE__);
  71. }
  72. }
  73. if (!(substr($server_response, 0, 3) == $response))
  74. {
  75. message_die(GENERAL_ERROR, "Ran into problems sending Mail. Response: $server_response", "", $line, __FILE__);
  76. }
  77. }
  78. // Replacement or substitute for PHP's mail command
  79. function smtpmail($mail_to, $subject, $message, $headers = '')
  80. {
  81. global $board_config;
  82. // Fix any bare linefeeds in the message to make it RFC821 Compliant.
  83. $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
  84. if ($headers != '')
  85. {
  86. if (is_array($headers))
  87. {
  88. if (sizeof($headers) > 1)
  89. {
  90. $headers = join("\n", $headers);
  91. }
  92. else
  93. {
  94. $headers = $headers[0];
  95. }
  96. }
  97. $headers = chop($headers);
  98. // Make sure there are no bare linefeeds in the headers
  99. $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);
  100. // Ok this is rather confusing all things considered,
  101. // but we have to grab bcc and cc headers and treat them differently
  102. // Something we really didn't take into consideration originally
  103. $header_array = explode("\r\n", $headers);
  104. @reset($header_array);
  105. $headers = '';
  106. while(list(, $header) = each($header_array))
  107. {
  108. if (preg_match('#^cc:#si', $header))
  109. {
  110. $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
  111. }
  112. else if (preg_match('#^bcc:#si', $header))
  113. {
  114. $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
  115. $header = '';
  116. }
  117. $headers .= ($header != '') ? $header . "\r\n" : '';
  118. }
  119. $headers = chop($headers);
  120. $cc = explode(', ', $cc);
  121. $bcc = explode(', ', $bcc);
  122. }
  123. if (trim($subject) == '')
  124. {
  125. message_die(GENERAL_ERROR, "No email Subject specified", "", __LINE__, __FILE__);
  126. }
  127. if (trim($message) == '')
  128. {
  129. message_die(GENERAL_ERROR, "Email message was blank", "", __LINE__, __FILE__);
  130. }
  131. // some code was removed here for 2.0.6
  132. // Ok we have error checked as much as we can to this point let's get on
  133. // it already.
  134. if( !$socket = fsockopen($board_config['smtp_host'], 25, $errno, $errstr, 20) )
  135. {
  136. message_die(GENERAL_ERROR, "Could not connect to smtp host : $errno : $errstr", "", __LINE__, __FILE__);
  137. }
  138. // Wait for reply
  139. server_parse($socket, "220", __LINE__);
  140. // Do we want to use AUTH?, send RFC2554 EHLO, else send RFC821 HELO
  141. // This improved as provided by SirSir to accomodate
  142. if( !empty($board_config['smtp_username']) && !empty($board_config['smtp_password']) )
  143. {
  144. fputs($socket, "EHLO " . $board_config['smtp_host'] . "\r\n");
  145. server_parse($socket, "250", __LINE__);
  146. fputs($socket, "AUTH LOGIN\r\n");
  147. server_parse($socket, "334", __LINE__);
  148. fputs($socket, base64_encode($board_config['smtp_username']) . "\r\n");
  149. server_parse($socket, "334", __LINE__);
  150. fputs($socket, base64_encode($board_config['smtp_password']) . "\r\n");
  151. server_parse($socket, "235", __LINE__);
  152. }
  153. else
  154. {
  155. fputs($socket, "HELO " . $board_config['smtp_host'] . "\r\n");
  156. server_parse($socket, "250", __LINE__);
  157. }
  158. // From this point onward most server response codes should be 250
  159. // Specify who the mail is from....
  160. fputs($socket, "MAIL FROM: <" . $board_config['board_email'] . ">\r\n");
  161. server_parse($socket, "250", __LINE__);
  162. // Specify each user to send to and build to header.
  163. $to_header = '';
  164. // Add an additional bit of error checking to the To field.
  165. $mail_to = (trim($mail_to) == '') ? 'Undisclosed-recipients:;' : trim($mail_to);
  166. if (preg_match('#[^ ]+\@[^ ]+#', $mail_to))
  167. {
  168. fputs($socket, "RCPT TO: <$mail_to>\r\n");
  169. server_parse($socket, "250", __LINE__);
  170. }
  171. // Ok now do the CC and BCC fields...
  172. @reset($bcc);
  173. while(list(, $bcc_address) = each($bcc))
  174. {
  175. // Add an additional bit of error checking to bcc header...
  176. $bcc_address = trim($bcc_address);
  177. if (preg_match('#[^ ]+\@[^ ]+#', $bcc_address))
  178. {
  179. fputs($socket, "RCPT TO: <$bcc_address>\r\n");
  180. server_parse($socket, "250", __LINE__);
  181. }
  182. }
  183. @reset($cc);
  184. while(list(, $cc_address) = each($cc))
  185. {
  186. // Add an additional bit of error checking to cc header
  187. $cc_address = trim($cc_address);
  188. if (preg_match('#[^ ]+\@[^ ]+#', $cc_address))
  189. {
  190. fputs($socket, "RCPT TO: <$cc_address>\r\n");
  191. server_parse($socket, "250", __LINE__);
  192. }
  193. }
  194. // Ok now we tell the server we are ready to start sending data
  195. fputs($socket, "DATA\r\n");
  196. // This is the last response code we look for until the end of the message.
  197. server_parse($socket, "354", __LINE__);
  198. // Send the Subject Line...
  199. fputs($socket, "Subject: $subject\r\n");
  200. // Now the To Header.
  201. fputs($socket, "To: $mail_to\r\n");
  202. // Now any custom headers....
  203. fputs($socket, "$headers\r\n\r\n");
  204. // Ok now we are ready for the message...
  205. fputs($socket, "$message\r\n");
  206. // Ok the all the ingredients are mixed in let's cook this puppy...
  207. fputs($socket, ".\r\n");
  208. server_parse($socket, "250", __LINE__);
  209. // Now tell the server we are done and close the socket...
  210. fputs($socket, "QUIT\r\n");
  211. fclose($socket);
  212. return TRUE;
  213. }
  214. ?>