email.test.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Emailing: helper for the admins to troubleshoot email issues
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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 SetupPage('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) || 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(SetupPage $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(SetupPage $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. $iRes = $oEmail->Send($aIssues, true /* force synchronous exec */);
  150. switch ($iRes)
  151. {
  152. case EMAIL_SEND_OK:
  153. $oP->ok("The email has been sent, you may now check that the email will arrive...");
  154. break;
  155. case EMAIL_SEND_PENDING:
  156. $oP->ok("Email queued");
  157. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  158. break;
  159. case EMAIL_SEND_ERROR:
  160. foreach ($aIssues as $sError)
  161. {
  162. $oP->error($sError);
  163. }
  164. $oP->add("<button onClick=\"window.history.back();\"><< Back</button>\n");
  165. break;
  166. }
  167. }
  168. /**
  169. * Main program
  170. */
  171. // #@# Init default timezone -> do not get a notice... to be improved !!!
  172. // duplicated from 'attributedef.class.inc.php', needed here because mail() does
  173. // generate a notice
  174. date_default_timezone_set('Europe/Paris');
  175. try
  176. {
  177. switch($sOperation)
  178. {
  179. case 'step1':
  180. DisplayStep1($oP);
  181. break;
  182. case 'step2':
  183. $oP->no_cache();
  184. $sTo = Utils::ReadParam('to', '', false, 'raw_data');
  185. $sFrom = Utils::ReadParam('from', '', false, 'raw_data');
  186. if (strlen($sFrom) == 0)
  187. {
  188. $sFrom = $sTo;
  189. }
  190. DisplayStep2($oP, $sFrom, $sTo);
  191. break;
  192. default:
  193. $oP->error("Error: unsupported operation '$sOperation'");
  194. }
  195. }
  196. catch(Exception $e)
  197. {
  198. $oP->error("Error: '".$e->getMessage()."'");
  199. }
  200. catch(CoreException $e)
  201. {
  202. $oP->error("Error: '".$e->getHtmlDesc()."'");
  203. }
  204. $oP->output();
  205. ?>