Exception when sending email in KernelEvents::TERMINATE event

Created on 29 June 2024, 8 months ago

Problem/Motivation

Hello, when sending mail in the KernelEvents::TERMINATE event you will receive an exception from NativeSessionStorage with the message "Failed to start the session because headers have already been sent by ..." this happens after updating to version 1.3 since the method \Drupal\smtp\ Plugin\Mail\SMTPMailSystem::debug() tries to determine whether debug is enabled by checking the value from the session.

Sending mail in the KernelEvents::TERMINATE event is a fairly common solution if the sending status is not important to the user. This allows you to avoid slowing down page loading for the user by sending mail, since the event is executed after the response is sent.

Steps to reproduce

Add a subscriber to the KernelEvents::TERMINATE event and send an email from it.

Proposed resolution

Use any other storage for the global debug indicator for the current request.
It is advisable to use something that is not sent to the client. For example, a static class variable or the request itself.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024