Давайте посмотрим, как работают буферы вывода:
// Works
send_test_email( 122, 'joe@example.com' );
/*
When you have executed send_test_email() above the method generate_html() is
executed and ob_start and ob_end_clean() is executed. This means that the output
buffer is initiated and stopped with ob_end_clean().
When output buffer is ended it is sent to the client (browser).
*/
//If you do the same again...
send_test_email( 122, 'joe@example.com' );
/*
...this won't work because output buffer is sent to the browser. After this is done
you are not able to modify header information. Because you cannot modify header
information you can not start a new output buffer either.
That is why you get the error below:
ob_end_clean(): failed to delete buffer. No buffer to delete in
Cannot modify header information - headers already sent by ...
*/
Чтобы решить эту проблему, я бы попробовал что-то вроде этого:
Убрать ob_start()
и ob_end_clean()
вне класса и удалите их из метода generate_html()
protected function generate_html() {
$template_path = 'path/to/email-template.php';
extract( $this->_data );
include $template_path;
$this->_html = ob_get_contents();
}
Тестирование вашего кода:
ob_start();
send_test_email( 122, 'joe@example.com' );
foreach ( $users as $user ) {
send_test_email( $user->id, $user->email );
}
ob_end_clean();
ОБНОВЛЕНИЕ Вы можете решить эту проблему не возиться с буферами вывода, что-то вроде:
protected function generate_html() {
$template_path = 'path/to/email-template.php';
extract( $this->_data );
//requires the template file to have a return to able to store the
//content in a variable
//
//Look at $foo = include 'return.php';
//at https://www.php.net/manual/en/function.include.php
$this->_html = include $template_path;
}