email.test.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. }
  60. if ($bRet)
  61. {
  62. $oP->ok("PHP settings are ok to proceed with a test of the email");
  63. }
  64. return $bRet;
  65. }
  66. /**
  67. * Display the form for the first step of the test wizard
  68. * which consists in a basic check of the configuration and display of a form for testing
  69. */
  70. function DisplayStep1(SetupWebPage $oP)
  71. {
  72. $sNextOperation = 'step2';
  73. $oP->add("<h1>iTop email test</h1>\n");
  74. $oP->add("<h2>Checking prerequisites</h2>\n");
  75. if (CheckEmailSetting($oP))
  76. {
  77. $sRedStar = '<span class="hilite">*</span>';
  78. $oP->add("<h2>Try to send an email</h2>\n");
  79. $oP->add("<form method=\"post\" onSubmit=\"return DoSubmit('Sending an email...', 10)\">\n");
  80. // Form goes here
  81. $oP->add("<fieldset><legend>Test configuration</legend>\n");
  82. $aForm = array();
  83. $aForm[] = array(
  84. 'label' => "To$sRedStar:",
  85. 'input' => "<input id=\"to\" type=\"text\" name=\"to\" value=\"\">",
  86. 'help' => ' pure email address (john.foo@worldcompany.com)',
  87. );
  88. $aForm[] = array(
  89. 'label' => "From:",
  90. 'input' => "<input id=\"from\" type=\"text\" name=\"from\" value=\"\">",
  91. 'help' => ' defaults to \'To\'',
  92. );
  93. $oP->form($aForm);
  94. $oP->add("</fieldset>\n");
  95. $oP->add("<input type=\"hidden\" name=\"operation\" value=\"$sNextOperation\">\n");
  96. $oP->add("<button type=\"submit\">Next >></button>\n");
  97. $oP->add("</form>\n");
  98. }
  99. }
  100. /**
  101. * Display the form for the second step of the configuration wizard
  102. * which consists in sending an email, which maybe a problem under Windows
  103. */
  104. function DisplayStep2(SetupWebPage $oP, $sFrom, $sTo)
  105. {
  106. //$sNextOperation = 'step3';
  107. $oP->add("<h1>iTop configuration wizard</h1>\n");
  108. $oP->add("<h2>Step 2: send an email</h2>\n");
  109. $oP->add("<p>Sending an email to '$sTo'... (From: '$sFrom')</p>\n");
  110. $oP->add("<form method=\"post\">\n");
  111. $oEmail = new Email();
  112. $oEmail->SetRecipientTO($sTo);
  113. $oEmail->SetRecipientFrom($sFrom);
  114. $oEmail->SetSubject("Test iTop");
  115. $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>");
  116. $aIssues = $oEmail->send();
  117. if (count($aIssues) > 0)
  118. {
  119. foreach ($aIssues as $sError)
  120. {
  121. $oP->error($sError);
  122. }
  123. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  124. }
  125. else
  126. {
  127. $oP->ok("The email has been sent, you may now check that the email will arrive...");
  128. }
  129. }
  130. /**
  131. * Main program
  132. */
  133. // #@# Init default timezone -> do not get a notice... to be improved !!!
  134. // duplicated from 'attributedef.class.inc.php', needed here because mail() does
  135. // generate a notice
  136. date_default_timezone_set('Europe/Paris');
  137. try
  138. {
  139. switch($sOperation)
  140. {
  141. case 'step1':
  142. DisplayStep1($oP);
  143. break;
  144. case 'step2':
  145. $oP->no_cache();
  146. $sTo = Utils::ReadParam('to');
  147. $sFrom = Utils::ReadParam('from');
  148. if (strlen($sFrom) == 0)
  149. {
  150. $sFrom = $sTo;
  151. }
  152. DisplayStep2($oP, $sFrom, $sTo);
  153. break;
  154. default:
  155. $oP->error("Error: unsupported operation '$sOperation'");
  156. }
  157. }
  158. catch(Exception $e)
  159. {
  160. $oP->error("Error: '".$e->getMessage()."'");
  161. }
  162. catch(CoreException $e)
  163. {
  164. $oP->error("Error: '".$e->getHtmlDesc()."'");
  165. }
  166. $oP->output();
  167. ?>