email.test.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Wizard to configure and initialize the iTop application
  4. */
  5. require_once('../application/utils.inc.php');
  6. require_once('../core/email.class.inc.php');
  7. require_once('./setuppage.class.inc.php');
  8. $sOperation = Utils::ReadParam('operation', 'step1');
  9. $oP = new SetupWebPage('iTop email test utility');
  10. /**
  11. * Helper to check server setting required to send an email
  12. */
  13. function CheckEmailSetting($oP)
  14. {
  15. $bRet = true;
  16. if (function_exists('php_ini_loaded_file')) // PHP >= 5.2.4
  17. {
  18. $sPhpIniFile = php_ini_loaded_file();
  19. }
  20. else
  21. {
  22. $sPhpIniFile = 'php.ini';
  23. }
  24. $bIsWindows = (array_key_exists('WINDIR', $_SERVER));
  25. if ($bIsWindows)
  26. {
  27. $sSmtpServer = ini_get('SMTP');
  28. if (empty($sSmtpServer))
  29. {
  30. $oP->error("The SMTP server is not defined. Please add the 'SMTP' directive into $sPhpIniFile");
  31. $bRet = false;
  32. }
  33. else if (strcasecmp($sSmtpServer, 'localhost') == 0)
  34. {
  35. $oP->warning("Your SMTP server is configured to 'localhost'. You might want to set or change the 'SMTP' directive into $sPhpIniFile");
  36. }
  37. else
  38. {
  39. $oP->info("Your SMTP server: <strong>$sSmtpServer</strong>. To change this value, modify the 'SMTP' directive into $sPhpIniFile");
  40. }
  41. $iSmtpPort = (int) ini_get('smtp_port');
  42. if (empty($iSmtpPort))
  43. {
  44. $oP->info("The SMTP port is not defined. Please add the 'smtp_port' directive into $sPhpIniFile");
  45. $bRet = false;
  46. }
  47. else if ($iSmtpPort = 25)
  48. {
  49. $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");
  50. }
  51. else
  52. {
  53. $oP->info("Your SMTP port is configured to $iSmtpPort. You might want to set or change the 'smtp_port' directive into $sPhpIniFile");
  54. }
  55. }
  56. else
  57. {
  58. // Not a windows system
  59. $sSendMail = ini_get('sendmail_path');
  60. if (empty($sSendMail))
  61. {
  62. $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>");
  63. $bRet = false;
  64. }
  65. else
  66. {
  67. $oP->info("The command to send mail: <strong>$sSendMail</strong>. To change this value, modify the 'sendmail_path' directive into $sPhpIniFile");
  68. }
  69. }
  70. if ($bRet)
  71. {
  72. $oP->ok("PHP settings are ok to proceed with a test of the email");
  73. }
  74. return $bRet;
  75. }
  76. /**
  77. * Display the form for the first step of the test wizard
  78. * which consists in a basic check of the configuration and display of a form for testing
  79. */
  80. function DisplayStep1(SetupWebPage $oP)
  81. {
  82. $sNextOperation = 'step2';
  83. $oP->add("<h1>iTop email test</h1>\n");
  84. $oP->add("<h2>Checking prerequisites</h2>\n");
  85. if (CheckEmailSetting($oP))
  86. {
  87. $sRedStar = '<span class="hilite">*</span>';
  88. $oP->add("<h2>Try to send an email</h2>\n");
  89. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Sending an email...', 10)\">\n");
  90. // Form goes here
  91. $oP->add("<fieldset><legend>Test configuration</legend>\n");
  92. $aForm = array();
  93. $aForm[] = array(
  94. 'label' => "To$sRedStar:",
  95. 'input' => "<input id=\"to\" type=\"text\" name=\"to\" value=\"\">",
  96. 'help' => ' pure email address (john.foo@worldcompany.com)',
  97. );
  98. $aForm[] = array(
  99. 'label' => "From:",
  100. 'input' => "<input id=\"from\" type=\"text\" name=\"from\" value=\"\">",
  101. 'help' => ' defaults to \'To\'',
  102. );
  103. $oP->form($aForm);
  104. $oP->add("</fieldset>\n");
  105. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  106. $oP->add("<button type=\"submit\">Next >></button>\n");
  107. $oP->add("</form>\n");
  108. }
  109. }
  110. /**
  111. * Display the form for the second step of the configuration wizard
  112. * which consists in sending an email, which maybe a problem under Windows
  113. */
  114. function DisplayStep2(SetupWebPage $oP, $sFrom, $sTo)
  115. {
  116. //$sNextOperation = 'step3';
  117. $oP->add("<h1>iTop configuration wizard</h1>\n");
  118. $oP->add("<h2>Step 2: send an email</h2>\n");
  119. $oP->add("<p>Sending an email to '$sTo'... (From: '$sFrom')</p>\n");
  120. $oP->add("<form method=\"post\">\n");
  121. $oEmail = new Email();
  122. $oEmail->SetRecipientTO($sTo);
  123. $oEmail->SetRecipientFrom($sFrom);
  124. $oEmail->SetSubject("Test iTop");
  125. $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>");
  126. $aIssues = $oEmail->send();
  127. if (count($aIssues) > 0)
  128. {
  129. foreach ($aIssues as $sError)
  130. {
  131. $oP->error($sError);
  132. }
  133. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  134. }
  135. else
  136. {
  137. $oP->ok("The email has been sent, you may now check that the email will arrive...");
  138. }
  139. }
  140. /**
  141. * Main program
  142. */
  143. // #@# Init default timezone -> do not get a notice... to be improved !!!
  144. // duplicated from 'attributedef.class.inc.php', needed here because mail() does
  145. // generate a notice
  146. date_default_timezone_set('Europe/Paris');
  147. try
  148. {
  149. switch($sOperation)
  150. {
  151. case 'step1':
  152. DisplayStep1($oP);
  153. break;
  154. case 'step2':
  155. $oP->no_cache();
  156. $sTo = Utils::ReadParam('to');
  157. $sFrom = Utils::ReadParam('from');
  158. if (strlen($sFrom) == 0)
  159. {
  160. $sFrom = $sTo;
  161. }
  162. DisplayStep2($oP, $sFrom, $sTo);
  163. break;
  164. default:
  165. $oP->error("Error: unsupported operation '$sOperation'");
  166. }
  167. }
  168. catch(Exception $e)
  169. {
  170. $oP->error("Error: '".$e->getMessage()."'");
  171. }
  172. catch(CoreException $e)
  173. {
  174. $oP->error("Error: '".$e->getHtmlDesc()."'");
  175. }
  176. $oP->output();
  177. ?>