MailSettingsController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 OC\User\Session;
  26. use \OCP\AppFramework\Controller;
  27. use OCP\IRequest;
  28. use OCP\IL10N;
  29. use OCP\IConfig;
  30. use OCP\Mail\IMailer;
  31. /**
  32. * @package OC\Settings\Controller
  33. */
  34. class MailSettingsController extends Controller {
  35. /** @var \OCP\IL10N */
  36. private $l10n;
  37. /** @var \OCP\IConfig */
  38. private $config;
  39. /** @var Session */
  40. private $userSession;
  41. /** @var \OC_Defaults */
  42. private $defaults;
  43. /** @var IMailer */
  44. private $mailer;
  45. /** @var string */
  46. private $defaultMailAddress;
  47. /**
  48. * @param string $appName
  49. * @param IRequest $request
  50. * @param IL10N $l10n
  51. * @param IConfig $config
  52. * @param Session $userSession
  53. * @param \OC_Defaults $defaults
  54. * @param IMailer $mailer
  55. * @param string $defaultMailAddress
  56. */
  57. public function __construct($appName,
  58. IRequest $request,
  59. IL10N $l10n,
  60. IConfig $config,
  61. Session $userSession,
  62. \OC_Defaults $defaults,
  63. IMailer $mailer,
  64. $defaultMailAddress) {
  65. parent::__construct($appName, $request);
  66. $this->l10n = $l10n;
  67. $this->config = $config;
  68. $this->userSession = $userSession;
  69. $this->defaults = $defaults;
  70. $this->mailer = $mailer;
  71. $this->defaultMailAddress = $defaultMailAddress;
  72. }
  73. /**
  74. * Sets the email settings
  75. * @param string $mail_domain
  76. * @param string $mail_from_address
  77. * @param string $mail_smtpmode
  78. * @param string $mail_smtpsecure
  79. * @param string $mail_smtphost
  80. * @param string $mail_smtpauthtype
  81. * @param int $mail_smtpauth
  82. * @param string $mail_smtpport
  83. * @return array
  84. */
  85. public function setMailSettings($mail_domain,
  86. $mail_from_address,
  87. $mail_smtpmode,
  88. $mail_smtpsecure,
  89. $mail_smtphost,
  90. $mail_smtpauthtype,
  91. $mail_smtpauth,
  92. $mail_smtpport) {
  93. $params = get_defined_vars();
  94. $configs = [];
  95. foreach($params as $key => $value) {
  96. $configs[$key] = (empty($value)) ? null : $value;
  97. }
  98. // Delete passwords from config in case no auth is specified
  99. if ($params['mail_smtpauth'] !== 1) {
  100. $configs['mail_smtpname'] = null;
  101. $configs['mail_smtppassword'] = null;
  102. }
  103. $this->config->setSystemValues($configs);
  104. return array('data' =>
  105. array('message' =>
  106. (string) $this->l10n->t('Saved')
  107. ),
  108. 'status' => 'success'
  109. );
  110. }
  111. /**
  112. * Store the credentials used for SMTP in the config
  113. * @param string $mail_smtpname
  114. * @param string $mail_smtppassword
  115. * @return array
  116. */
  117. public function storeCredentials($mail_smtpname, $mail_smtppassword) {
  118. $this->config->setSystemValues([
  119. 'mail_smtpname' => $mail_smtpname,
  120. 'mail_smtppassword' => $mail_smtppassword,
  121. ]);
  122. return array('data' =>
  123. array('message' =>
  124. (string) $this->l10n->t('Saved')
  125. ),
  126. 'status' => 'success'
  127. );
  128. }
  129. /**
  130. * Send a mail to test the settings
  131. * @return array
  132. */
  133. public function sendTestMail() {
  134. $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
  135. if (!empty($email)) {
  136. try {
  137. $message = $this->mailer->createMessage();
  138. $message->setTo([$email => $this->userSession->getUser()->getDisplayName()]);
  139. $message->setFrom([$this->defaultMailAddress]);
  140. $message->setSubject($this->l10n->t('test email settings'));
  141. $message->setPlainBody('If you received this email, the settings seem to be correct.');
  142. $this->mailer->send($message);
  143. } catch (\Exception $e) {
  144. return [
  145. 'data' => [
  146. 'message' => (string) $this->l10n->t('A problem occurred while sending the email. Please revise your settings. (Error: %s)', [$e->getMessage()]),
  147. ],
  148. 'status' => 'error',
  149. ];
  150. }
  151. return array('data' =>
  152. array('message' =>
  153. (string) $this->l10n->t('Email sent')
  154. ),
  155. 'status' => 'success'
  156. );
  157. }
  158. return array('data' =>
  159. array('message' =>
  160. (string) $this->l10n->t('You need to set your user email before being able to send test emails.'),
  161. ),
  162. 'status' => 'error'
  163. );
  164. }
  165. }