mailsettingscontroller.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @author Joas Schilling
  4. * @author Lukas Reschke
  5. * @copyright 2014 Joas Schilling nickvergessen@owncloud.com
  6. *
  7. * This file is licensed under the Affero General Public License version 3 or
  8. * later.
  9. * See the COPYING-README file.
  10. */
  11. namespace OC\Settings\Controller;
  12. use OC\User\Session;
  13. use \OCP\AppFramework\Controller;
  14. use OCP\IRequest;
  15. use OCP\IL10N;
  16. use OCP\IConfig;
  17. /**
  18. * @package OC\Settings\Controller
  19. */
  20. class MailSettingsController extends Controller {
  21. /** @var \OCP\IL10N */
  22. private $l10n;
  23. /** @var \OCP\IConfig */
  24. private $config;
  25. /** @var Session */
  26. private $userSession;
  27. /** @var \OC_Defaults */
  28. private $defaults;
  29. /** @var \OC_Mail */
  30. private $mail;
  31. /** @var string */
  32. private $defaultMailAddress;
  33. /**
  34. * @param string $appName
  35. * @param IRequest $request
  36. * @param IL10N $l10n
  37. * @param IConfig $config
  38. * @param Session $userSession
  39. * @param \OC_Defaults $defaults
  40. * @param \OC_Mail $mail
  41. * @param string $defaultMailAddress
  42. */
  43. public function __construct($appName,
  44. IRequest $request,
  45. IL10N $l10n,
  46. IConfig $config,
  47. Session $userSession,
  48. \OC_Defaults $defaults,
  49. \OC_Mail $mail,
  50. $defaultMailAddress) {
  51. parent::__construct($appName, $request);
  52. $this->l10n = $l10n;
  53. $this->config = $config;
  54. $this->userSession = $userSession;
  55. $this->defaults = $defaults;
  56. $this->mail = $mail;
  57. $this->defaultMailAddress = $defaultMailAddress;
  58. }
  59. /**
  60. * Sets the email settings
  61. * @param string $mail_domain
  62. * @param string $mail_from_address
  63. * @param string $mail_smtpmode
  64. * @param string $mail_smtpsecure
  65. * @param string $mail_smtphost
  66. * @param string $mail_smtpauthtype
  67. * @param int $mail_smtpauth
  68. * @param string $mail_smtpport
  69. * @return array
  70. */
  71. public function setMailSettings($mail_domain,
  72. $mail_from_address,
  73. $mail_smtpmode,
  74. $mail_smtpsecure,
  75. $mail_smtphost,
  76. $mail_smtpauthtype,
  77. $mail_smtpauth,
  78. $mail_smtpport) {
  79. $params = get_defined_vars();
  80. foreach($params as $key => $value) {
  81. if(empty($value)) {
  82. $this->config->deleteSystemValue($key);
  83. } else {
  84. $this->config->setSystemValue($key, $value);
  85. }
  86. }
  87. // Delete passwords from config in case no auth is specified
  88. if($params['mail_smtpauth'] !== 1) {
  89. $this->config->deleteSystemValue('mail_smtpname');
  90. $this->config->deleteSystemValue('mail_smtppassword');
  91. }
  92. return array('data' =>
  93. array('message' =>
  94. (string) $this->l10n->t('Saved')
  95. ),
  96. 'status' => 'success'
  97. );
  98. }
  99. /**
  100. * Store the credentials used for SMTP in the config
  101. * @param string $mail_smtpname
  102. * @param string $mail_smtppassword
  103. * @return array
  104. */
  105. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  106. $this->config->setSystemValue('mail_smtpname', $mail_smtpname);
  107. $this->config->setSystemValue('mail_smtppassword', $mail_smtppassword);
  108. return array('data' =>
  109. array('message' =>
  110. (string) $this->l10n->t('Saved')
  111. ),
  112. 'status' => 'success'
  113. );
  114. }
  115. /**
  116. * Send a mail to test the settings
  117. * @return array
  118. */
  119. public function sendTestMail() {
  120. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  121. if (!empty($email)) {
  122. try {
  123. $this->mail->send($email, $this->userSession->getUser()->getDisplayName(),
  124. $this->l10n->t('test email settings'),
  125. $this->l10n->t('If you received this email, the settings seem to be correct.'),
  126. $this->defaultMailAddress,
  127. $this->defaults->getName()
  128. );
  129. } catch (\Exception $e) {
  130. return array('data' =>
  131. array('message' =>
  132. (string) $this->l10n->t('A problem occurred while sending the email. Please revise your settings.'),
  133. ),
  134. 'status' => 'error'
  135. );
  136. }
  137. return array('data' =>
  138. array('message' =>
  139. (string) $this->l10n->t('Email sent')
  140. ),
  141. 'status' => 'success'
  142. );
  143. }
  144. return array('data' =>
  145. array('message' =>
  146. (string) $this->l10n->t('You need to set your user email before being able to send test emails.'),
  147. ),
  148. 'status' => 'error'
  149. );
  150. }
  151. }