Skip to content

fix: prevent fatal error when mail is not configured in Career Portal#820

Open
ocjorge wants to merge 3 commits into
opencats:masterfrom
ocjorge:fix/careerportal-mailer-exception
Open

fix: prevent fatal error when mail is not configured in Career Portal#820
ocjorge wants to merge 3 commits into
opencats:masterfrom
ocjorge:fix/careerportal-mailer-exception

Conversation

@ocjorge

@ocjorge ocjorge commented Jul 7, 2026

Copy link
Copy Markdown

Problem

CareerPortal::sendEmail() throws an uncaught PHPMailer exception when
no mail server is configured. This causes a fatal HTTP 500 error that
prevents candidates from completing their application through the
Career Portal — the application is saved to the database but the
candidate sees an error page instead of a confirmation.

Solution

Wrap the mail send call in try/catch to allow the application flow to
continue normally when mail is unavailable.

The catch block:

  • Logs the exception class and code via error_log() for debugging
  • Avoids logging $e->getMessage() to prevent exposing sensitive SMTP
    configuration details in logs
  • Sets $mailerStatus = false for deterministic status handling

Testing

Tested on PHP 8.4.21 + MariaDB 10.11 + Debian 12 without a configured
mail server. The candidate application flow completes successfully and
the confirmation page is shown. The error is logged to the PHP error log.

Context

This fix was suggested in the review of PR #792 by @anonymoususer72041:

"The CareerPortal mailer try/catch is not covered here or in #797.
I agree it is a useful fix... a small targeted PR for that one would
probably be easiest to review."

Without this change, CareerPortal::sendEmail() throws an uncaught
PHPMailer exception when no mail server is configured, causing a
fatal 500 error that prevents candidates from completing their
application through the Career Portal.

Wrapping the mail send in try/catch allows the application flow to
continue normally when mail is unavailable, while still logging the
error class and code for debugging without exposing sensitive SMTP
configuration details in logs.

Tested on PHP 8.4.21 + Debian 12 without a configured mail server.
Copilot AI review requested due to automatic review settings July 7, 2026 05:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents candidate-facing HTTP 500 errors in the Career Portal when outgoing email is not configured by handling PHPMailer exceptions inside CareerPortal::sendEmail() so the application flow can complete and still show the confirmation page.

Changes:

  • Wrap Mailer::sendToOne() in try/catch to prevent uncaught mail exceptions from aborting the request.
  • Log minimal exception metadata (class + code) and force a deterministic $mailerStatus = false on failure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/CareerPortal.php Outdated
Comment on lines +461 to +476
try {
$mailer = new Mailer($this->_siteID, $userID);
$mailerStatus = $mailer->sendToOne(
array($destination, ''),
$subject,
$body,
true
);
} catch (Exception $e) {
// Mail not configured or unavailable - log and continue
error_log(
'OpenCATS CareerPortal mail error (site=' . $this->_siteID . '): '
. get_class($e) . ' [' . $e->getCode() . ']'
);
$mailerStatus = false;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in commit 9de0586:

  • Changed catch to \PHPMailer\PHPMailer\Exception specifically to avoid
    swallowing unrelated application errors
  • Updated brace style to Allman to match existing code conventions

- Catch \PHPMailer\PHPMailer\Exception instead of base Exception
  to avoid swallowing unrelated application errors
- Use Allman brace style to match existing code conventions
@anonymoususer72041

Copy link
Copy Markdown
Contributor

Can you please address Codacy's warning?

Copilot AI review requested due to automatic review settings July 9, 2026 01:27
@ocjorge

ocjorge commented Jul 9, 2026

Copy link
Copy Markdown
Author

Addressed in commit e668f2f — removed the unused $mailerStatus variable.
Since sendEmail() returns void and the status is not used by any caller,
the assignment was unnecessary.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants