How to handle Email Templates

Is there a good way to handle Email Templates ?

I want to sens an email in a controller.

I found the following solution, but i am not sure where to put the template.txt (I put it in the controller-folder)

and the line seams to $body = strtr(file_get_contents('template.txt'), $vars); seams to break the controller:

Whoops \ Exception \ ErrorException (E_WARNING)
Cannot modify header information - headers already sent by (output started at 

I’d put it somewhere into the assets folder:

  $vars = array('firstname' => 'John', 'lastname' => 'Doe');
  $body = str::template(file_get_contents(kirby()->roots()->assets() . '/template.txt'), $vars);
  dump($body);

I am trying to send the email inside the try statement of a controller and get a wired error:

Whoops \ Exception \ ErrorException (E_WARNING)
Cannot modify header information - headers already sent by (output started at /var/www/host/html/app/kirby/vendor/getkirby/toolkit/helpers.php:112)

I have a similar controller like the one from this cookbook:
https://getkirby.com/docs/cookbook/creating-pages-from-frontend
(look for the section ‘The event.php controller’)
(And I have set the from-email in the configs file)

 try {

	$newRegistration = $page->find('registrations')->children()->create(str::slug($data['lastname'] . '-' . $data['firstname'] . '-' . time()) , 'register', $data);

	// right here i try to send an email:
	$vars = array( 'title' => $page->title() );
	$body = str::template(file_get_contents(kirby()->roots()->assets() . '/template.txt'), $vars);

	$email = email(array(
		  'to'      => 'mail@send.to',
		  'subject' => 'send an email',
		  'body'    => dump($body)
	));

	if( $email->send() ) {
		// echo 'The email has been sent';
	} else {
		// echo $email->error();
	}

Ah I think i got it working. now I am not using dumb($body)

I am just using $body like that:

	$email = email(array(
		  'to'      => 'mail@send.to',
		  'subject' => 'send an email',
		  'body'    => $body
	));

but for what should dumb($body) be used?

For nothing, just for testing if you get the desired result.

I have one more question, I want to have an emails-folder inside the asset-folder.

what would be the correct path to the template file?

// first
 $body = str::template(file_get_contents(kirby()->roots()->assets('emails') . '/template.txt'), $vars);

// or second
 $body = str::template(file_get_contents(kirby()->roots()->assets() . '/emails/template.txt'), $vars);

thanks for the deluxe :wink: support, that makes kirby very special :heart:️.

The second option!

:heart: