1
0

mailsettingscontroller.php 4.1 KB

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