Skip to content

Commit d8ca54b

Browse files
authored
Merge pull request #1610 from marcellintacite/improve-error-messages
refactor(backend): improve error messages to be more usefull
2 parents a842d83 + 81671b9 commit d8ca54b

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

modules/smtp/hm-smtp.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function capabilities($ehlo_response) {
264264
function connect() {
265265
$certfile = false;
266266
$certpass = false;
267-
$result = 'An error occurred connecting to the SMTP server';
267+
$result = "We couldn't connect to the SMTP server. Please check your internet connection or server settings, and try again.";
268268
$server = $this->server;
269269

270270
if ($this->tls) {
@@ -283,7 +283,9 @@ function connect() {
283283
else {
284284
$this->debug[] = 'Could not connect to the SMTP server';
285285
$this->debug[] = 'fsockopen errors #'.$errorno.'. '.$errorstr;
286-
$result = 'Could not connect to the configured SMTP server';
286+
// Log technical details for debugging
287+
error_log("SMTP connection failed to {$this->server}:{$this->port} - Error #{$errorno}: {$errorstr}");
288+
$result = "Unable to connect to the SMTP server. Please check your internet connection or server settings, and try again.";
287289
}
288290
$this->banner = $this->get_response();
289291
$command = 'EHLO '.$this->hostname;
@@ -295,7 +297,9 @@ function connect() {
295297
$this->send_command($command);
296298
$response = $this->get_response();
297299
if ($this->compare_response($response, '220') != 0) {
298-
$result = 'An error occurred during the STARTTLS command';
300+
// Log technical details for debugging
301+
error_log("SMTP STARTTLS command failed. Expected 220, got: " . print_r($response, true));
302+
$result = "We couldn't secure the connection to the SMTP server (STARTTLS failed). Please try again later.";
299303
}
300304
if(isset($certfile) && $certfile) {
301305
stream_context_set_option($this->handle, 'tls', 'local_cert', $certfile);
@@ -310,7 +314,9 @@ function connect() {
310314
$this->capabilities($response);
311315
}
312316
if($this->compare_response($response,'250') != 0) {
313-
$result = 'An error occurred during the EHLO command';
317+
// Log technical details for debugging
318+
error_log("SMTP EHLO command failed. Expected 250, got: " . print_r($response, true));
319+
$result = "We couldn't complete the connection to the SMTP server (EHLO command failed). Please try again.";
314320
}
315321
else {
316322
if($this->auth) {
@@ -352,7 +358,7 @@ function authenticate($username, $password, $mech) {
352358
if ($result) {
353359
return 'Authentication successful';
354360
}
355-
return 'Authentication failed';
361+
return "Login to the email server failed. Please check your username and password";
356362
} else {
357363
switch ($mech) {
358364
case 'external':
@@ -419,13 +425,15 @@ function authenticate($username, $password, $mech) {
419425
}
420426
}
421427
if (!isset($result)) {
422-
$result = 'An error occurred authenticating to the SMTP server';
428+
$result = "We couldn't log in to the SMTP server. Please check your username and password.";
423429
$res = $this->get_response();
424430
if ($this->compare_response($res, '235') == 0) {
425431
$this->state = 'authed';
426432
$result = false;
427433
} else {
428-
$result = 'Authorization failure';
434+
// Log technical details for debugging
435+
error_log("SMTP authentication failed. Expected 235, got: " . print_r($res, true));
436+
$result = "Login to the SMTP server was not authorized. Please check your username and password, and try again.";
429437
if (isset($res[0][1])) {
430438
$result .= ': '.implode(' ', $res[0][1]);
431439
}
@@ -568,7 +576,7 @@ function send_message($from, $recipients, $message, $from_params = '', $recipien
568576
$this->send_command($command);
569577
$res = $this->get_response();
570578
$bail = false;
571-
$result = 'An error occurred sending the message';
579+
$result = "Sorry, we couldn't send your message through the SMTP server right now. Please check your connection and try again.";
572580
if(is_array($recipients)) {
573581
if ($recipients_params) {
574582
$recipients_params = ' ' . $recipients_params;
@@ -598,7 +606,9 @@ function send_message($from, $recipients, $message, $from_params = '', $recipien
598606
$this->send_command($command);
599607
$res = $this->get_response();
600608
if ($this->compare_response($res, '354') != 0) {
601-
$result = 'An error occurred during the DATA command';
609+
// Log technical details for debugging
610+
error_log("SMTP DATA command failed. Expected 354, got: " . print_r($res, true));
611+
$result = "Sorry, we couldn't send your message right now. The SMTP server didn't accept the message for delivery (DATA command failed). Please try again later.";
602612
}
603613
else {
604614
$this->send_command($message);
@@ -610,12 +620,16 @@ function send_message($from, $recipients, $message, $from_params = '', $recipien
610620
$result = false;
611621
}
612622
else {
613-
$result = 'An error occurred sending the message DATA';
623+
// Log technical details for debugging
624+
error_log("SMTP message delivery failed. Expected 250, got: " . print_r($res, true));
625+
$result = "Your message could not be sent. The SMTP server did not confirm delivery. Please try again later.";
614626
}
615627
}
616628
}
617629
else {
618-
$result = 'An error occurred during the RCPT command';
630+
// Log technical details for debugging
631+
error_log("SMTP RCPT command failed for one or more recipients");
632+
$result = "There was an error sending your message. One or more of the recipient addresses may be invalid (RCPT command failed). Please check the email addresses and try again.";
619633
}
620634
return $result;
621635
}

0 commit comments

Comments
 (0)