2014-10-03 09:14:22 -04:00
< ? php
2025-06-30 09:04:05 -04:00
2014-10-03 09:14:22 -04:00
/**
2024-06-03 04:23:34 -04:00
* SPDX - FileCopyrightText : 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2014-10-03 09:14:22 -04:00
*/
2019-09-17 10:33:27 -04:00
namespace OCA\Settings\Controller ;
2014-10-03 09:14:22 -04:00
2024-07-25 07:14:49 -04:00
use OCA\Settings\Settings\Admin\Overview ;
2016-10-05 10:31:28 -04:00
use OCP\AppFramework\Controller ;
2017-04-18 09:44:20 -04:00
use OCP\AppFramework\Http ;
2024-07-25 07:14:49 -04:00
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting ;
use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired ;
2017-04-18 09:44:20 -04:00
use OCP\AppFramework\Http\DataResponse ;
2014-10-03 09:14:22 -04:00
use OCP\IConfig ;
2019-11-22 14:52:10 -05:00
use OCP\IL10N ;
use OCP\IRequest ;
2022-02-03 05:43:17 -05:00
use OCP\IURLGenerator ;
2016-10-05 10:31:28 -04:00
use OCP\IUserSession ;
2015-02-12 07:53:27 -05:00
use OCP\Mail\IMailer ;
2025-12-07 04:11:58 -05:00
use Psr\Log\LoggerInterface ;
2014-10-03 09:14:22 -04:00
class MailSettingsController extends Controller {
/**
* @ param string $appName
* @ param IRequest $request
* @ param IL10N $l10n
* @ param IConfig $config
2016-10-05 10:31:28 -04:00
* @ param IUserSession $userSession
2022-02-03 05:43:17 -05:00
* @ param IURLGenerator $urlGenerator ,
2015-02-12 07:53:27 -05:00
* @ param IMailer $mailer
2014-10-03 09:14:22 -04:00
*/
2024-10-18 06:04:22 -04:00
public function __construct (
$appName ,
2023-11-23 04:22:34 -05:00
IRequest $request ,
2024-10-18 06:04:22 -04:00
private IL10N $l10n ,
private IConfig $config ,
private IUserSession $userSession ,
private IURLGenerator $urlGenerator ,
private IMailer $mailer ,
2025-12-07 04:11:58 -05:00
private LoggerInterface $logger ,
2024-10-18 06:04:22 -04:00
) {
2014-10-03 09:14:22 -04:00
parent :: __construct ( $appName , $request );
}
/**
* Sets the email settings
*/
2024-07-25 07:14:49 -04:00
#[AuthorizedAdminSetting(settings: Overview::class)]
#[PasswordConfirmationRequired]
2024-12-17 03:49:49 -05:00
public function setMailSettings (
string $mail_domain ,
string $mail_from_address ,
string $mail_smtpmode ,
string $mail_smtpsecure ,
string $mail_smtphost ,
2026-01-14 06:34:41 -05:00
? bool $mail_smtpauth ,
2024-12-17 03:49:49 -05:00
string $mail_smtpport ,
string $mail_sendmailmode ,
2026-01-14 09:43:40 -05:00
? bool $mail_noverify = null ,
2024-12-17 03:49:49 -05:00
) : 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 17:46:40 -05:00
$configs [ $key ] = empty ( $value ) ? null : $value ;
2014-10-03 09:14:22 -04:00
}
2026-01-14 09:43:40 -05:00
if ( $mail_noverify !== null ) {
$options = $this -> config -> getSystemValue ( 'mail_smtpstreamoptions' , []);
$options [ 'ssl' ] ? ? = [];
$options [ 'ssl' ][ 'allow_self_signed' ] = $mail_noverify ;
$options [ 'ssl' ][ 'verify_peer' ] = ! $mail_noverify ;
$options [ 'ssl' ][ 'verify_peer_name' ] = ! $mail_noverify ;
$configs [ 'mail_smtpstreamoptions' ] = $options ;
}
2014-10-03 09:14:22 -04:00
// Delete passwords from config in case no auth is specified
2024-12-17 03:49:49 -05:00
if ( ! $mail_smtpauth ) {
2015-01-23 05:13:47 -05:00
$configs [ 'mail_smtpname' ] = null ;
$configs [ 'mail_smtppassword' ] = null ;
2014-10-03 09:14:22 -04:00
}
2015-01-23 05:13:47 -05:00
$this -> config -> setSystemValues ( $configs );
2022-02-03 05:43:17 -05:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
2017-04-18 09:44:20 -04:00
return new DataResponse ();
2014-10-03 09:14:22 -04:00
}
/**
* Store the credentials used for SMTP in the config
*/
2024-07-25 07:14:49 -04:00
#[AuthorizedAdminSetting(settings: Overview::class)]
#[PasswordConfirmationRequired]
2026-01-14 06:34:41 -05:00
public function storeCredentials ( string $mail_smtpname , ? string $mail_smtppassword ) : DataResponse {
2017-04-18 09:44:20 -04:00
if ( $mail_smtppassword === '********' ) {
return new DataResponse ( $this -> l10n -> t ( 'Invalid SMTP password.' ), Http :: STATUS_BAD_REQUEST );
}
2026-01-14 06:34:41 -05:00
if ( $mail_smtppassword !== null ) {
$this -> config -> setSystemValue ( 'mail_smtppassword' , $mail_smtppassword );
}
$this -> config -> setSystemValue ( 'mail_smtpname' , $mail_smtpname );
2022-02-03 05:43:17 -05:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
2017-04-18 09:44:20 -04:00
return new DataResponse ();
2014-10-03 09:14:22 -04:00
}
/**
* Send a mail to test the settings
2017-04-18 10:08:29 -04:00
* @ return DataResponse
2014-10-03 09:14:22 -04:00
*/
2024-07-25 07:14:49 -04:00
#[AuthorizedAdminSetting(settings: Overview::class)]
2014-10-03 09: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 15:30:31 -04:00
$displayName = $this -> userSession -> getUser () -> getDisplayName ();
2017-09-04 09:07:19 -04:00
$template = $this -> mailer -> createEMailTemplate ( 'settings.TestEmail' , [
2017-08-30 16:56:14 -04:00
'displayname' => $displayName ,
]);
2017-09-15 04:59:11 -04:00
$template -> setSubject ( $this -> l10n -> t ( 'Email setting test' ));
2017-04-18 15:30:31 -04: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 15:55:02 -05:00
$message = $this -> mailer -> createMessage ();
2017-04-18 15:30:31 -04:00
$message -> setTo ([ $email => $displayName ]);
2017-09-15 05:01:21 -04:00
$message -> useTemplate ( $template );
2017-03-17 08:45:25 -04:00
$errors = $this -> mailer -> send ( $message );
if ( ! empty ( $errors )) {
2022-02-03 05:43:17 -05:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
2017-04-26 03:33:18 -04:00
throw new \RuntimeException ( $this -> l10n -> t ( 'Email could not be sent. Check your mail server log' ));
2017-03-17 08:45:25 -04:00
}
2022-02-03 05:43:17 -05:00
// Store the successful config in the app config
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '1' );
2017-04-18 10:08:29 -04:00
return new DataResponse ();
2014-10-03 09:14:22 -04:00
} catch ( \Exception $e ) {
2022-02-03 05:43:17 -05:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
2025-12-07 07:06:22 -05:00
$this -> logger -> error ( 'Failed sending test email: ' . $e -> getMessage (), [ 'exception' => $e ]);
2017-04-18 10:08:29 -04: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 09:14:22 -04:00
}
}
2022-02-03 05:43:17 -05:00
$this -> config -> setAppValue ( 'core' , 'emailTestSuccessful' , '0' );
2025-12-07 04:11:58 -05:00
$this -> logger -> error ( 'Failed sending test email: User ' . $this -> userSession -> getUser () -> getUID () . ' has no email address configured in their account settings' );
2022-09-21 11:44:32 -04: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 09:14:22 -04:00
}
}