2014-10-03 13:14:22 +00:00
< ? php
/**
2017-04-18 14:11:29 +00:00
* @ copyright Copyright ( c ) 2017 Joas Schilling < coding @ schilljs . com >
2016-07-21 15:07:57 +00:00
* @ copyright Copyright ( c ) 2016 , ownCloud , Inc .
*
* @ author Joas Schilling < coding @ schilljs . com >
2016-05-26 17:56:05 +00:00
* @ author Lukas Reschke < lukas @ statuscode . ch >
2015-03-26 10:44:34 +00:00
* @ author Morris Jobke < hey @ morrisjobke . de >
*
* @ 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 ,
* along with this program . If not , see < http :// www . gnu . org / licenses />
*
2014-10-03 13:14:22 +00:00
*/
2015-02-26 10:37:37 +00:00
2014-10-03 13:14:22 +00:00
namespace OC\Settings\Controller ;
2016-10-05 14:31:28 +00:00
use OCP\AppFramework\Controller ;
2017-04-18 13:44:20 +00:00
use OCP\AppFramework\Http ;
use OCP\AppFramework\Http\DataResponse ;
2014-10-03 13:14:22 +00:00
use OCP\IRequest ;
use OCP\IL10N ;
use OCP\IConfig ;
2016-10-05 14:31:28 +00:00
use OCP\IUserSession ;
2015-02-12 12:53:27 +00:00
use OCP\Mail\IMailer ;
2014-10-03 13:14:22 +00:00
/**
* @ package OC\Settings\Controller
*/
class MailSettingsController extends Controller {
2016-10-05 14:31:28 +00:00
/** @var IL10N */
2014-10-03 13:14:22 +00:00
private $l10n ;
2016-10-05 14:31:28 +00:00
/** @var IConfig */
2014-10-03 13:14:22 +00:00
private $config ;
2016-10-05 14:31:28 +00:00
/** @var IUserSession */
2014-10-03 13:14:22 +00:00
private $userSession ;
2015-02-12 12:53:27 +00:00
/** @var IMailer */
private $mailer ;
2014-10-03 13:14:22 +00:00
/**
* @ param string $appName
* @ param IRequest $request
* @ param IL10N $l10n
* @ param IConfig $config
2016-10-05 14:31:28 +00:00
* @ param IUserSession $userSession
2015-02-12 12:53:27 +00:00
* @ param IMailer $mailer
2014-10-03 13:14:22 +00:00
*/
public function __construct ( $appName ,
IRequest $request ,
IL10N $l10n ,
IConfig $config ,
2016-10-05 14:31:28 +00:00
IUserSession $userSession ,
2018-02-13 20:40:38 +00:00
IMailer $mailer ) {
2014-10-03 13:14:22 +00:00
parent :: __construct ( $appName , $request );
$this -> l10n = $l10n ;
$this -> config = $config ;
$this -> userSession = $userSession ;
2015-02-12 12:53:27 +00:00
$this -> mailer = $mailer ;
2014-10-03 13:14:22 +00:00
}
/**
* Sets the email settings
2016-09-19 16:40:47 +00:00
*
* @ PasswordConfirmationRequired
*
2014-10-03 13:14:22 +00: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 13:44:20 +00:00
* @ return DataResponse
2014-10-03 13:14:22 +00:00
*/
public function setMailSettings ( $mail_domain ,
$mail_from_address ,
$mail_smtpmode ,
$mail_smtpsecure ,
$mail_smtphost ,
$mail_smtpauthtype ,
$mail_smtpauth ,
2018-11-12 22:27:18 +00:00
$mail_smtpport ,
$mail_sendmailmode ) {
2014-10-03 13:14:22 +00:00
$params = get_defined_vars ();
2015-01-23 10:13:47 +00:00
$configs = [];
2014-10-03 13:14:22 +00:00
foreach ( $params as $key => $value ) {
2018-01-26 22:46:40 +00:00
$configs [ $key ] = empty ( $value ) ? null : $value ;
2014-10-03 13:14:22 +00:00
}
// Delete passwords from config in case no auth is specified
2015-01-23 10:13:47 +00:00
if ( $params [ 'mail_smtpauth' ] !== 1 ) {
$configs [ 'mail_smtpname' ] = null ;
$configs [ 'mail_smtppassword' ] = null ;
2014-10-03 13:14:22 +00:00
}
2015-01-23 10:13:47 +00:00
$this -> config -> setSystemValues ( $configs );
2017-04-18 13:44:20 +00:00
return new DataResponse ();
2014-10-03 13:14:22 +00:00
}
/**
* Store the credentials used for SMTP in the config
2016-09-19 16:40:47 +00:00
*
* @ PasswordConfirmationRequired
*
2014-10-03 13:14:22 +00:00
* @ param string $mail_smtpname
* @ param string $mail_smtppassword
2017-04-18 13:44:20 +00:00
* @ return DataResponse
2014-10-03 13:14:22 +00:00
*/
public function storeCredentials ( $mail_smtpname , $mail_smtppassword ) {
2017-04-18 13:44:20 +00:00
if ( $mail_smtppassword === '********' ) {
return new DataResponse ( $this -> l10n -> t ( 'Invalid SMTP password.' ), Http :: STATUS_BAD_REQUEST );
}
2015-01-23 10:13:47 +00:00
$this -> config -> setSystemValues ([
'mail_smtpname' => $mail_smtpname ,
'mail_smtppassword' => $mail_smtppassword ,
]);
2014-10-03 13:14:22 +00:00
2017-04-18 13:44:20 +00:00
return new DataResponse ();
2014-10-03 13:14:22 +00:00
}
/**
* Send a mail to test the settings
2017-04-18 14:08:29 +00:00
* @ return DataResponse
2014-10-03 13:14:22 +00:00
*/
public function sendTestMail () {
$email = $this -> config -> getUserValue ( $this -> userSession -> getUser () -> getUID (), $this -> appName , 'email' , '' );
if ( ! empty ( $email )) {
try {
2017-04-18 19:30:31 +00:00
$displayName = $this -> userSession -> getUser () -> getDisplayName ();
2017-09-04 13:07:19 +00:00
$template = $this -> mailer -> createEMailTemplate ( 'settings.TestEmail' , [
2017-08-30 20:56:14 +00:00
'displayname' => $displayName ,
]);
2017-09-15 08:59:11 +00:00
$template -> setSubject ( $this -> l10n -> t ( 'Email setting test' ));
2017-04-18 19:30:31 +00: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 20:55:02 +00:00
$message = $this -> mailer -> createMessage ();
2017-04-18 19:30:31 +00:00
$message -> setTo ([ $email => $displayName ]);
2017-09-15 09:01:21 +00:00
$message -> useTemplate ( $template );
2017-03-17 12:45:25 +00:00
$errors = $this -> mailer -> send ( $message );
if ( ! empty ( $errors )) {
2017-04-26 07:33:18 +00:00
throw new \RuntimeException ( $this -> l10n -> t ( 'Email could not be sent. Check your mail server log' ));
2017-03-17 12:45:25 +00:00
}
2017-04-18 14:08:29 +00:00
return new DataResponse ();
2014-10-03 13:14:22 +00:00
} catch ( \Exception $e ) {
2017-04-18 14:08:29 +00: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 13:14:22 +00:00
}
}
2017-04-18 14:08:29 +00: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 13:14:22 +00:00
}
}