2014-10-03 17:14:22 +04:00
< ? php
/**
2017-04-18 17:11:29 +03:00
* @ copyright Copyright ( c ) 2017 Joas Schilling < coding @ schilljs . com >
2016-07-21 18:07:57 +03:00
* @ copyright Copyright ( c ) 2016 , ownCloud , Inc .
*
2019-12-03 21:57:53 +03:00
* @ author Christoph Wurst < christoph @ winzerhof - wurst . at >
* @ author Daniel Kesselberg < mail @ danielkesselberg . de >
2016-07-21 18:07:57 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2016-05-26 20:56:05 +03:00
* @ author Lukas Reschke < lukas @ statuscode . ch >
2015-03-26 13:44:34 +03:00
* @ author Morris Jobke < hey @ morrisjobke . de >
2019-12-03 21:57:53 +03:00
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2015-03-26 13:44:34 +03:00
*
* @ license AGPL - 3.0
*
* This code is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License , version 3 ,
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License , version 3 ,
2019-12-03 21:57:53 +03:00
* along with this program . If not , see < http :// www . gnu . org / licenses />
2015-03-26 13:44:34 +03:00
*
2014-10-03 17:14:22 +04:00
*/
2015-02-26 13:37:37 +03:00
2019-09-17 17:33:27 +03:00
namespace OCA\Settings\Controller ;
2014-10-03 17:14:22 +04:00
2016-10-05 17:31:28 +03:00
use OCP\AppFramework\Controller ;
2017-04-18 16:44:20 +03:00
use OCP\AppFramework\Http ;
use OCP\AppFramework\Http\DataResponse ;
2014-10-03 17:14:22 +04:00
use OCP\IConfig ;
2019-11-22 22:52:10 +03:00
use OCP\IL10N ;
use OCP\IRequest ;
2016-10-05 17:31:28 +03:00
use OCP\IUserSession ;
2015-02-12 15:53:27 +03:00
use OCP\Mail\IMailer ;
2014-10-03 17:14:22 +04:00
class MailSettingsController extends Controller {
2016-10-05 17:31:28 +03:00
/** @var IL10N */
2014-10-03 17:14:22 +04:00
private $l10n ;
2016-10-05 17:31:28 +03:00
/** @var IConfig */
2014-10-03 17:14:22 +04:00
private $config ;
2016-10-05 17:31:28 +03:00
/** @var IUserSession */
2014-10-03 17:14:22 +04:00
private $userSession ;
2015-02-12 15:53:27 +03:00
/** @var IMailer */
private $mailer ;
2014-10-03 17:14:22 +04:00
/**
* @ param string $appName
* @ param IRequest $request
* @ param IL10N $l10n
* @ param IConfig $config
2016-10-05 17:31:28 +03:00
* @ param IUserSession $userSession
2015-02-12 15:53:27 +03:00
* @ param IMailer $mailer
2014-10-03 17:14:22 +04:00
*/
public function __construct ( $appName ,
IRequest $request ,
IL10N $l10n ,
IConfig $config ,
2016-10-05 17:31:28 +03:00
IUserSession $userSession ,
2018-02-13 23:40:38 +03:00
IMailer $mailer ) {
2014-10-03 17:14:22 +04:00
parent :: __construct ( $appName , $request );
$this -> l10n = $l10n ;
$this -> config = $config ;
$this -> userSession = $userSession ;
2015-02-12 15:53:27 +03:00
$this -> mailer = $mailer ;
2014-10-03 17:14:22 +04:00
}
/**
* Sets the email settings
2016-09-19 19:40:47 +03:00
*
* @ PasswordConfirmationRequired
*
2014-10-03 17:14:22 +04:00
* @ param string $mail_domain
* @ param string $mail_from_address
* @ param string $mail_smtpmode
* @ param string $mail_smtpsecure
* @ param string $mail_smtphost
* @ param string $mail_smtpauthtype
* @ param int $mail_smtpauth
* @ param string $mail_smtpport
2017-04-18 16:44:20 +03:00
* @ return DataResponse
2014-10-03 17:14:22 +04:00
*/
public function setMailSettings ( $mail_domain ,
$mail_from_address ,
$mail_smtpmode ,
$mail_smtpsecure ,
$mail_smtphost ,
$mail_smtpauthtype ,
$mail_smtpauth ,
2018-11-13 01:27:18 +03:00
$mail_smtpport ,
$mail_sendmailmode ) {
2014-10-03 17:14:22 +04:00
$params = get_defined_vars ();
2015-01-23 13:13:47 +03:00
$configs = [];
2020-04-10 15:19:56 +03:00
foreach ( $params as $key => $value ) {
2018-01-27 01:46:40 +03:00
$configs [ $key ] = empty ( $value ) ? null : $value ;
2014-10-03 17:14:22 +04:00
}
// Delete passwords from config in case no auth is specified
2015-01-23 13:13:47 +03:00
if ( $params [ 'mail_smtpauth' ] !== 1 ) {
$configs [ 'mail_smtpname' ] = null ;
$configs [ 'mail_smtppassword' ] = null ;
2014-10-03 17:14:22 +04:00
}
2015-01-23 13:13:47 +03:00
$this -> config -> setSystemValues ( $configs );
2017-04-18 16:44:20 +03:00
return new DataResponse ();
2014-10-03 17:14:22 +04:00
}
/**
* Store the credentials used for SMTP in the config
2016-09-19 19:40:47 +03:00
*
* @ PasswordConfirmationRequired
*
2014-10-03 17:14:22 +04:00
* @ param string $mail_smtpname
* @ param string $mail_smtppassword
2017-04-18 16:44:20 +03:00
* @ return DataResponse
2014-10-03 17:14:22 +04:00
*/
public function storeCredentials ( $mail_smtpname , $mail_smtppassword ) {
2017-04-18 16:44:20 +03:00
if ( $mail_smtppassword === '********' ) {
return new DataResponse ( $this -> l10n -> t ( 'Invalid SMTP password.' ), Http :: STATUS_BAD_REQUEST );
}
2015-01-23 13:13:47 +03:00
$this -> config -> setSystemValues ([
'mail_smtpname' => $mail_smtpname ,
'mail_smtppassword' => $mail_smtppassword ,
]);
2014-10-03 17:14:22 +04:00
2017-04-18 16:44:20 +03:00
return new DataResponse ();
2014-10-03 17:14:22 +04:00
}
/**
* Send a mail to test the settings
2017-04-18 17:08:29 +03:00
* @ return DataResponse
2014-10-03 17:14:22 +04:00
*/
public function sendTestMail () {
$email = $this -> config -> getUserValue ( $this -> userSession -> getUser () -> getUID (), $this -> appName , 'email' , '' );
if ( ! empty ( $email )) {
try {
2017-04-18 22:30:31 +03:00
$displayName = $this -> userSession -> getUser () -> getDisplayName ();
2017-09-04 16:07:19 +03:00
$template = $this -> mailer -> createEMailTemplate ( 'settings.TestEmail' , [
2017-08-30 23:56:14 +03:00
'displayname' => $displayName ,
]);
2017-09-15 11:59:11 +03:00
$template -> setSubject ( $this -> l10n -> t ( 'Email setting test' ));
2017-04-18 22:30:31 +03:00
$template -> addHeader ();
$template -> addHeading ( $this -> l10n -> t ( 'Well done, %s!' , [ $displayName ]));
$template -> addBodyText ( $this -> l10n -> t ( 'If you received this email, the email configuration seems to be correct.' ));
$template -> addFooter ();
2015-02-19 23:55:02 +03:00
$message = $this -> mailer -> createMessage ();
2017-04-18 22:30:31 +03:00
$message -> setTo ([ $email => $displayName ]);
2017-09-15 12:01:21 +03:00
$message -> useTemplate ( $template );
2017-03-17 15:45:25 +03:00
$errors = $this -> mailer -> send ( $message );
if ( ! empty ( $errors )) {
2017-04-26 10:33:18 +03:00
throw new \RuntimeException ( $this -> l10n -> t ( 'Email could not be sent. Check your mail server log' ));
2017-03-17 15:45:25 +03:00
}
2017-04-18 17:08:29 +03:00
return new DataResponse ();
2014-10-03 17:14:22 +04:00
} catch ( \Exception $e ) {
2017-04-18 17:08:29 +03:00
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 );
2014-10-03 17:14:22 +04:00
}
}
2017-04-18 17:08:29 +03:00
return new DataResponse ( $this -> l10n -> t ( 'You need to set your user email before being able to send test emails.' ), Http :: STATUS_BAD_REQUEST );
2014-10-03 17:14:22 +04:00
}
}