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