diff --git a/app/config/errors.php b/app/config/errors.php index 87ddce6306..634eed71cc 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -524,6 +524,11 @@ return [ 'description' => 'The project key has expired. Please generate a new key using the Appwrite console.', 'code' => 401, ], + Exception::PROJECT_SMTP_CONFIG_INVALID => [ + 'name' => Exception::PROJECT_SMTP_CONFIG_INVALID, + 'description' => 'Provided SMTP config is invalid.', + 'code' => 400, + ], Exception::PROJECT_TEMPLATE_DEFAULT_DELETION => [ 'name' => Exception::PROJECT_TEMPLATE_DEFAULT_DELETION, 'description' => 'The default template for the project cannot be deleted.', diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 09af5daa8a..a83613301c 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -39,6 +39,7 @@ use Utopia\Validator\Text; use Utopia\Validator\WhiteList; use Appwrite\Template\Template; use Utopia\Locale\Locale; +use PHPMailer\PHPMailer\PHPMailer; App::init() ->groups(['projects']) @@ -1634,6 +1635,21 @@ App::patch('/v1/projects/:projectId/smtp') throw new Exception(Exception::PROJECT_NOT_FOUND); } + // validate SMTP settings + $mail = new PHPMailer(true); + $mail->isSMTP(); + $mail->Username = $username; + $mail->Password = $password; + $mail->Host = $host; + $mail->Port = $port; + $mail->SMTPSecure = $secure; + $mail->SMTPAutoTLS = false; + $valid = $mail->SmtpConnect(); + + if(!$valid) { + throw new Exception(Exception::GENERAL_SMTP_DISABLED); + } + $smtp = [ 'enabled' => $enabled, 'sender' => $sender, diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 0f488d96f9..e7e66e1c72 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -164,6 +164,8 @@ class Exception extends \Exception public const PROJECT_RESERVED_PROJECT = 'project_reserved_project'; public const PROJECT_KEY_EXPIRED = 'project_key_expired'; + public const PROJECT_SMTP_CONFIG_INVALID = 'project_smtp_config_invalid'; + public const PROJECT_TEMPLATE_DEFAULT_DELETION = 'project_template_default_deletion'; /** Webhooks */