Sending html emails with toolkit Email

I’ve setup email to send from the mailgun service, and I get it to work, but it only sends it as plain text.
How would I do it to get it to send html mail?

$mail = new Email(array(
	"to" => "John Doe <>",
	"from" => "Jan Doe <>",
	"subject" => $subject,
	"body" => $template,
	"service" => "mailgun",
	"options" => array(
		"key" => "MAILGUN_API",
		"domain" => "MAILGUN_DOMAIN"

When I’ve used the PEAR Mail.php script I’ve declared it as text/html and it works

The default implementations of the Kirby Email services don’t support HTML emails, but you can create your own Mailgun service driver that does support HTML inside a plugin:

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

  if(empty($email->options['key']))    throw new Error('Missing Mailgun API key');
  if(empty($email->options['domain'])) throw new Error('Missing Mailgun API domain');

  $url  = '' . $email->options['domain'] . '/messages';
  $auth = base64_encode('api:' . $email->options['key']);

  $headers = array(
    'Accept: application/json',
    'Authorization: Basic ' . $auth

  $data = array(
    'from'       => $email->from,
    'to'         => $email->to,
    'subject'    => $email->subject,
    'text'       => $email->body,
    'h:Reply-To' => $email->replyTo,
  if($email->html()) $data['html'] = $email->html;

  $email->response = remote::post($url, array(
    'data'    => $data,
    'headers' => $headers

  if($email->response->code() != 200) {
    throw new Error('The mail could not be sent!');


You can then pass html additionally to the text body when creating the email. Alternatively you can of course only use HTML without a plain text body by replacing text with html in the $data array definition.

This works in a similar way for other email services.

I have created a feature request issue for native support in the future here.

1 Like

Thank you, you’re a life saver.
I couldn’t get PEAR Mail.php to work with funny letters like Ñ, so I had to go with a service like mailgun.
But it’s for the best anyways, reduces to load on my server.