2014-10-03 13:14:22 +00:00
< ? php
/**
2024-06-03 08:23:34 +00:00
* SPDX - FileCopyrightText : 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2014-10-03 13:14:22 +00:00
*/
2019-09-17 14:33:27 +00:00
namespace OCA\Settings\Controller ;
2014-10-03 13:14:22 +00:00
2024-07-25 11:14:49 +00:00
use OCA\Settings\Settings\Admin\Overview ;
2016-10-05 14:31:28 +00:00
use OCP\AppFramework\Controller ;
2017-04-18 13:44:20 +00:00
use OCP\AppFramework\Http ;
2024-07-25 11:14:49 +00:00
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting ;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired ;
2017-04-18 13:44:20 +00:00
use OCP\AppFramework\Http\DataResponse ;
2014-10-03 13:14:22 +00:00
use OCP\IConfig ;
2019-11-22 19:52:10 +00:00
use OCP\IL10N ;
use OCP\IRequest ;
2022-02-03 10:43:17 +00:00
use OCP\IURLGenerator ;
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
class MailSettingsController extends Controller {
/**
* @ param string $appName
* @ param IRequest $request
* @ param IL10N $l10n
* @ param IConfig $config
2016-10-05 14:31:28 +00:00
* @ param IUserSession $userSession
2022-02-03 10:43:17 +00:00
* @ param IURLGenerator $urlGenerator ,
2015-02-12 12:53:27 +00:00
* @ param IMailer $mailer
2014-10-03 13:14:22 +00:00
*/
2024-10-18 10:04:22 +00:00
public function __construct (
$appName ,
2014-10-03 13:14:22 +00:00
IRequest $request ,
2024-10-18 10:04:22 +00:00
private IL10N $l10n ,
private IConfig $config ,
private IUserSession $userSession ,
private IURLGenerator $urlGenerator ,
private IMailer $mailer ,
) {
2014-10-03 13:14:22 +00:00
parent :: __construct ( $appName , $request );
}
/**
* Sets the email settings
*/
2024-07-25 11:14:49 +00:00
#[AuthorizedAdminSetting(settings: Overview::class)]
#[PasswordConfirmationRequired]
2024-12-17 08:49:49 +00:00
public function setMailSettings (
string $mail_domain ,
string $mail_from_address ,
string $mail_smtpmode ,
string $mail_smtpsecure ,
string $mail_smtphost ,
? string $mail_smtpauth ,
string $mail_smtpport ,
string $mail_sendmailmode ,
) : DataResponse {
$mail_smtpauth = $mail_smtpauth == '1' ;
$configs = [
'mail_domain' => $mail_domain ,
'mail_from_address' => $mail_from_address ,
'mail_smtpmode' => $mail_smtpmode ,
'mail_smtpsecure' => $mail_smtpsecure ,
'mail_smtphost' => $mail_smtphost ,
'mail_smtpauth' => $mail_smtpauth ,
'mail_smtpport' => $mail_smtpport ,
'mail_sendmailmode' => $mail_sendmailmode ,
];
foreach ( $configs 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
2024-12-17 08:49:49 +00:00
if ( ! $mail_smtpauth ) {
2015-01-23 10:13:47 +00:00
$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 );
2022-02-03 10:43:17 +00:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
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
*
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
*/
2024-07-25 11:14:49 +00:00
#[AuthorizedAdminSetting(settings: Overview::class)]
#[PasswordConfirmationRequired]
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 ([
2020-10-05 13:12:57 +00:00
'mail_smtpname' => $mail_smtpname ,
'mail_smtppassword' => $mail_smtppassword ,
2015-01-23 10:13:47 +00:00
]);
2014-10-03 13:14:22 +00:00
2022-02-03 10:43:17 +00:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
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
*/
2024-07-25 11:14:49 +00:00
#[AuthorizedAdminSetting(settings: Overview::class)]
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 )) {
2022-02-03 10:43:17 +00:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
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
}
2022-02-03 10:43:17 +00:00
// Store the successful config in the app config
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '1' );
2017-04-18 14:08:29 +00:00
return new DataResponse ();
2014-10-03 13:14:22 +00:00
} catch ( \Exception $e ) {
2022-02-03 10:43:17 +00:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
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
}
}
2022-02-03 10:43:17 +00:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
2022-09-21 15:44:32 +00:00
return new DataResponse ( $this -> l10n -> t ( 'You need to set your account email before being able to send test emails. Go to %s for that.' , [ $this -> urlGenerator -> linkToRouteAbsolute ( 'settings.PersonalSettings.index' )]), Http :: STATUS_BAD_REQUEST );
2014-10-03 13:14:22 +00:00
}
}