email.test.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Emailing: helper for the admins to troubleshoot email issues
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * Wizard to configure and initialize the iTop application
  26. */
  27. require_once('../approot.inc.php');
  28. require_once(APPROOT.'/application/utils.inc.php');
  29. require_once(APPROOT.'/core/email.class.inc.php');
  30. require_once('./setuppage.class.inc.php');
  31. $sOperation = Utils::ReadParam('operation', 'step1');
  32. $oP = new SetupWebPage('iTop email test utility');
  33. /**
  34. * Helper to check server setting required to send an email
  35. */
  36. function CheckEmailSetting($oP)
  37. {
  38. $bRet = true;
  39. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  40. {
  41. $sPhpIniFile = php_ini_loaded_file();
  42. }
  43. else
  44. {
  45. $sPhpIniFile = 'php.ini';
  46. }
  47. $bIsWindows = (array_key_exists('WINDIR', $_SERVER));
  48. if ($bIsWindows)
  49. {
  50. $sSmtpServer = ini_get('SMTP');
  51. if (empty($sSmtpServer))
  52. {
  53. $oP->error("The SMTP server is not defined. Please add the 'SMTP' directive into $sPhpIniFile");
  54. $bRet = false;
  55. }
  56. else if (strcasecmp($sSmtpServer, 'localhost') == 0)
  57. {
  58. $oP->warning("Your SMTP server is configured to 'localhost'. You might want to set or change the 'SMTP' directive into $sPhpIniFile");
  59. }
  60. else
  61. {
  62. $oP->info("Your SMTP server: <strong>$sSmtpServer</strong>. To change this value, modify the 'SMTP' directive into $sPhpIniFile");
  63. }
  64. $iSmtpPort = (int) ini_get('smtp_port');
  65. if (empty($iSmtpPort))
  66. {
  67. $oP->info("The SMTP port is not defined. Please add the 'smtp_port' directive into $sPhpIniFile");
  68. $bRet = false;
  69. }
  70. else if ($iSmtpPort = 25)
  71. {
  72. $oP->info("Your SMTP port is configured to the default value: 25. You might want to set or change the 'smtp_port' directive into $sPhpIniFile");
  73. }
  74. else
  75. {
  76. $oP->info("Your SMTP port is configured to $iSmtpPort. You might want to set or change the 'smtp_port' directive into $sPhpIniFile");
  77. }
  78. }
  79. else
  80. {
  81. // Not a windows system
  82. $sSendMail = ini_get('sendmail_path');
  83. if (empty($sSendMail))
  84. {
  85. $oP->error("The command to send mail is not defined. Please add the 'sendmail_path' directive into $sPhpIniFile. A recommended setting is <em>sendmail_path=sendmail -t -i</em>");
  86. $bRet = false;
  87. }
  88. else
  89. {
  90. $oP->info("The command to send mail: <strong>$sSendMail</strong>. To change this value, modify the 'sendmail_path' directive into $sPhpIniFile");
  91. }
  92. }
  93. if ($bRet)
  94. {
  95. $oP->ok("PHP settings are ok to proceed with a test of the email");
  96. }
  97. return $bRet;
  98. }
  99. /**
  100. * Display the form for the first step of the test wizard
  101. * which consists in a basic check of the configuration and display of a form for testing
  102. */
  103. function DisplayStep1(SetupWebPage $oP)
  104. {
  105. $sNextOperation = 'step2';
  106. $oP->add("<h1>iTop email test</h1>\n");
  107. $oP->add("<h2>Checking prerequisites</h2>\n");
  108. if (CheckEmailSetting($oP))
  109. {
  110. $sRedStar = '<span class="hilite">*</span>';
  111. $oP->add("<h2>Try to send an email</h2>\n");
  112. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Sending an email...', 10)\">\n");
  113. // Form goes here
  114. $oP->add("<fieldset><legend>Test configuration</legend>\n");
  115. $aForm = array();
  116. $aForm[] = array(
  117. 'label' => "To$sRedStar:",
  118. 'input' => "<input id=\"to\" type=\"text\" name=\"to\" value=\"\">",
  119. 'help' => ' pure email address (john.foo@worldcompany.com)',
  120. );
  121. $aForm[] = array(
  122. 'label' => "From:",
  123. 'input' => "<input id=\"from\" type=\"text\" name=\"from\" value=\"\">",
  124. 'help' => ' defaults to \'To\'',
  125. );
  126. $oP->form($aForm);
  127. $oP->add("</fieldset>\n");
  128. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  129. $oP->add("<button type=\"submit\">Next >></button>\n");
  130. $oP->add("</form>\n");
  131. }
  132. }
  133. /**
  134. * Display the form for the second step of the configuration wizard
  135. * which consists in sending an email, which maybe a problem under Windows
  136. */
  137. function DisplayStep2(SetupWebPage $oP, $sFrom, $sTo)
  138. {
  139. //$sNextOperation = 'step3';
  140. $oP->add("<h1>iTop configuration wizard</h1>\n");
  141. $oP->add("<h2>Step 2: send an email</h2>\n");
  142. $oP->add("<p>Sending an email to '$sTo'... (From: '$sFrom')</p>\n");
  143. $oP->add("<form method=\"post\">\n");
  144. $oEmail = new Email();
  145. $oEmail->SetRecipientTO($sTo);
  146. $oEmail->SetRecipientFrom($sFrom);
  147. $oEmail->SetSubject("Test iTop");
  148. $oEmail->SetBody("<p>Hello,</p><p>The email function is now working fine.</p><p>You may now be able to use the notification function.</p><p>iTop</p>");
  149. $aIssues = $oEmail->send();
  150. if (count($aIssues) > 0)
  151. {
  152. foreach ($aIssues as $sError)
  153. {
  154. $oP->error($sError);
  155. }
  156. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  157. }
  158. else
  159. {
  160. $oP->ok("The email has been sent, you may now check that the email will arrive...");
  161. }
  162. }
  163. /**
  164. * Main program
  165. */
  166. // #@# Init default timezone -> do not get a notice... to be improved !!!
  167. // duplicated from 'attributedef.class.inc.php', needed here because mail() does
  168. // generate a notice
  169. date_default_timezone_set('Europe/Paris');
  170. try
  171. {
  172. switch($sOperation)
  173. {
  174. case 'step1':
  175. DisplayStep1($oP);
  176. break;
  177. case 'step2':
  178. $oP->no_cache();
  179. $sTo = Utils::ReadParam('to');
  180. $sFrom = Utils::ReadParam('from');
  181. if (strlen($sFrom) == 0)
  182. {
  183. $sFrom = $sTo;
  184. }
  185. DisplayStep2($oP, $sFrom, $sTo);
  186. break;
  187. default:
  188. $oP->error("Error: unsupported operation '$sOperation'");
  189. }
  190. }
  191. catch(Exception $e)
  192. {
  193. $oP->error("Error: '".$e->getMessage()."'");
  194. }
  195. catch(CoreException $e)
  196. {
  197. $oP->error("Error: '".$e->getHtmlDesc()."'");
  198. }
  199. $oP->output();
  200. ?>