Sending email templates

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 emails I have tested go to my spam folder. Do I need to go through a service like mailchimp? Or is there a way I can I can use the email API to create a template?

This is rather a problem with the PHP mail function and how it’s sending emails than with the content. Thus, you might try to send emails via an external mail sending service’s API. Some services are already supported by the Kirby toolkit, so integration should be very easy.

If you’d still like to use email templates, which I think is a good idea to keep the code clean, you might use something as simple as reading the contents of a text file and replacing some placeholders…

template.txt

Dear {{firstname}} {{lastname}},

how are you?

Love.
$vars = array('firstname' => 'John', 'lastname' => 'Doe');
$body = strtr(file_get_contents('template.txt'), $vars);
1 Like

I like the template idea. but where do you place the template.txt?

I have a controller in which I send the email. would the correct folder be the controller folder?

Answered here: How to handle Email Templates

@xpnimi Please do not post duplicates.