xpnimi
March 15, 2018, 11:37am
1
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)
I am currently trying to send an email to a user after they create an account and for other actions throughout the site. I am using
$email = new Email(array(
'to' => 'mail@example.com',
'from' => 'john@doe.com',
'subject' => 'Yay, Kirby sends mails',
'body' => 'Hey, this is a test email!'
));
if($email->send()) {
echo 'The email has been sent';
} else {
echo $email->error()->message();
}
And for the body I would like to have an separate template since it seems that the …
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);
xpnimi
March 15, 2018, 1:53pm
3
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();
}
xpnimi
March 15, 2018, 1:59pm
4
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.
xpnimi
March 15, 2018, 2:11pm
6
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 support, that makes kirby very special ️.