MailSettingsController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Settings\Controller;
  25. use OCP\AppFramework\Controller;
  26. use OCP\IRequest;
  27. use OCP\IL10N;
  28. use OCP\IConfig;
  29. use OCP\IUserSession;
  30. use OCP\Mail\IMailer;
  31. /**
  32. * @package OC\Settings\Controller
  33. */
  34. class MailSettingsController extends Controller {
  35. /** @var IL10N */
  36. private $l10n;
  37. /** @var IConfig */
  38. private $config;
  39. /** @var IUserSession */
  40. private $userSession;
  41. /** @var IMailer */
  42. private $mailer;
  43. /** @var string */
  44. private $defaultMailAddress;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param IL10N $l10n
  49. * @param IConfig $config
  50. * @param IUserSession $userSession
  51. * @param IMailer $mailer
  52. * @param string $fromMailAddress
  53. */
  54. public function __construct($appName,
  55. IRequest $request,
  56. IL10N $l10n,
  57. IConfig $config,
  58. IUserSession $userSession,
  59. IMailer $mailer,
  60. $fromMailAddress) {
  61. parent::__construct($appName, $request);
  62. $this->l10n = $l10n;
  63. $this->config = $config;
  64. $this->userSession = $userSession;
  65. $this->mailer = $mailer;
  66. $this->defaultMailAddress = $fromMailAddress;
  67. }
  68. /**
  69. * Sets the email settings
  70. *
  71. * @PasswordConfirmationRequired
  72. *
  73. * @param string $mail_domain
  74. * @param string $mail_from_address
  75. * @param string $mail_smtpmode
  76. * @param string $mail_smtpsecure
  77. * @param string $mail_smtphost
  78. * @param string $mail_smtpauthtype
  79. * @param int $mail_smtpauth
  80. * @param string $mail_smtpport
  81. * @return array
  82. */
  83. public function setMailSettings($mail_domain,
  84. $mail_from_address,
  85. $mail_smtpmode,
  86. $mail_smtpsecure,
  87. $mail_smtphost,
  88. $mail_smtpauthtype,
  89. $mail_smtpauth,
  90. $mail_smtpport) {
  91. $params = get_defined_vars();
  92. $configs = [];
  93. foreach($params as $key => $value) {
  94. $configs[$key] = (empty($value)) ? null : $value;
  95. }
  96. // Delete passwords from config in case no auth is specified
  97. if ($params['mail_smtpauth'] !== 1) {
  98. $configs['mail_smtpname'] = null;
  99. $configs['mail_smtppassword'] = null;
  100. }
  101. $this->config->setSystemValues($configs);
  102. return array('data' =>
  103. array('message' =>
  104. (string) $this->l10n->t('Saved')
  105. ),
  106. 'status' => 'success'
  107. );
  108. }
  109. /**
  110. * Store the credentials used for SMTP in the config
  111. *
  112. * @PasswordConfirmationRequired
  113. *
  114. * @param string $mail_smtpname
  115. * @param string $mail_smtppassword
  116. * @return array
  117. */
  118. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  119. $this->config->setSystemValues([
  120. 'mail_smtpname' => $mail_smtpname,
  121. 'mail_smtppassword' => $mail_smtppassword,
  122. ]);
  123. return array('data' =>
  124. array('message' =>
  125. (string) $this->l10n->t('Saved')
  126. ),
  127. 'status' => 'success'
  128. );
  129. }
  130. /**
  131. * Send a mail to test the settings
  132. * @return array
  133. */
  134. public function sendTestMail() {
  135. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  136. if (!empty($email)) {
  137. try {
  138. $message = $this->mailer->createMessage();
  139. $message->setTo([$email => $this->userSession->getUser()->getDisplayName()]);
  140. $message->setFrom([$this->defaultMailAddress]);
  141. $message->setSubject($this->l10n->t('test email settings'));
  142. $message->setPlainBody('If you received this email, the settings seem to be correct.');
  143. $this->mailer->send($message);
  144. } catch (\Exception $e) {
  145. return [
  146. 'data' => [
  147. 'message' => (string) $this->l10n->t('A problem occurred while sending the email. Please revise your settings. (Error: %s)', [$e->getMessage()]),
  148. ],
  149. 'status' => 'error',
  150. ];
  151. }
  152. return array('data' =>
  153. array('message' =>
  154. (string) $this->l10n->t('Email sent')
  155. ),
  156. 'status' => 'success'
  157. );
  158. }
  159. return array('data' =>
  160. array('message' =>
  161. (string) $this->l10n->t('You need to set your user email before being able to send test emails.'),
  162. ),
  163. 'status' => 'error'
  164. );
  165. }
  166. }