Is there a way to send HTML emails?

The toolkit docs say you can create your own adapter. I believe this is the only way to bypass the default header. You can try copying the default adapter and changing the header in the new service.

http://getkirby.com/docs/toolkit/sending-email#how-to-write-your-own-adaptor

email::$services['html_email'] = function($email) {

  $headers = array(
    'From: ' . $email->from,
    'Reply-To: ' . $email->replyTo,
    'Return-Path: ' . $email->replyTo,
    'Message-ID: <' . time() . '-' . $email->from . '>',
    'X-Mailer: PHP v' . phpversion(),
    'Content-Type: text/html; charset=utf-8',
    'Content-Transfer-Encoding: 8bit',
  );

  ini_set('sendmail_from', $email->from);
  $send = mail($email->to, str::utf8($email->subject), str::utf8($email->body), implode(PHP_EOL, $headers));
  ini_restore('sendmail_from');

  if(!$send) {
    throw new Error('The email could not be sent');
  }

};

Now, I’m not sure if this works when added in a plugin, to a page controller, or if it has to be added to the core e-mail file. The docs don’t specify. Perhaps @bastianallgeier or someone else can specify.

To specify the service try

$email = new Email(array(
  'to'      => 'mail@example.com',
  'from'    => 'john@doe.com',
  'service' => 'html_email',
  '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();
}
4 Likes