Skip to content

Emails

Kirby has a built-in email engine, which can be used to send notifications to your users, replies to filled in contact forms and more.

Simple

try {
  $kirby->email([
    'from' => 'welcome@supercompany.com',
    'replyTo' => 'no-reply@supercompany.com',
    'to' => 'someone@gmail.com',
    'cc' => 'anotherone@gmail.com',
    'bcc' => 'secret@gmail.com',
    'subject' => 'Welcome!',
    'body'=> 'It\'s great to have you with us',
  ]);
} catch (Exception $error) {
  echo $error;
}

// errors will be thrown as Exceptions. If you want to notice/output
// errors, always use try-catch

With multiple recipients:

$kirby->email([
  'from' => 'welcome@supercompany.com',
  'to' => [
    'someone@gmail.com',
    'numbertwo@gmail.com'
  ],
  'subject' => 'Welcome!',
  'body'=> 'It\'s great to have you with us',
]);

// or with a collection of users
$kirby->email([
  'to' => $kirby->users()->filterBy('role', 'newbies'),
  …
]);

Plain Text

$kirby->email([
  'to' => 'peter@lustig.de',
  'template' => 'my-email',
  'data' => [
    'num' => '2'
  ]
]);
/site/templates/emails/my-email.php
Welcome to Newsletter No. <?= $num ?>!

HTML & Plain Text

/site/templates/emails/media.html.php
<h1>Hi <?= $name ?></h1>
<p><?= $text ?></p>
in site/templates/emails/media.text.php
Hi <?= $name ?>,

<?= $text ?>
$kirby->email([
  'template' => 'media',
  'data' => [
    'name' => 'Peter',
    'text' => 'welcome to our wonderful email list'
  ]
]);

Personalized emails to multiple users

foreach ($users as $user) {
  try {
    $kirby->email('management', [
      'to' => $user,
      'template' => 'user-specific',
      'data' => [
        'user' => $user
      ]
    ]);
  } catch (Exception $error) {
    echo $error;
  }
}
/site/templates/emails/user-specific.php
Welcome, <?= $user->name() ?>!
Great to have you with us since <?= $user->yearJoinedCompany() ?>.

Sending Email with attachments

$kirby->email([
  'from' => 'welcome@supercompany.com',
  'to' => 'someone@gmail.com',
  'subject' => 'Welcome!',
  'body' => 'Here are some attachments',
  'attachments' => [
    $page->file('somefile.jpg'),
    $page->file('someotherfile.jpg')
  ]
]);

Email presets

/site/config/config.php
return [
  'email' => [
    'presets' => [
      'contact' => [
        'from'    => 'no-reply@supercompany.com',
        'subject' => 'Thank you for your contact request',
        'cc'      => 'marketing@supercompany.com',
        'body'    => 'We will never reply'
      ]
    ]
  ]
];
$kirby->email('contact', [
  'to' => 'peter@lustig.de'
]);

Transport configuration

/site/config/config.php
return [
  'email' => [
    'transport' => [
      'type' => 'smtp',
      'host' => 'smtp.company.com',
      'port' => 465,
      'security' => true
    ]
  ]
];

With authentication

/site/config/config.php
return [
  'email' => [
    'transport' => [
      'type' => 'smtp',
      'host' => 'smtp.server.com',
      'port' => 465,
      'security' => true,
      'auth' => true,
      'username' => '...',
      'password' => '...',
    ]
  ]
];