email.test.php 6.4 KB

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