Use Mailer

Windwalker provides an interface to integrate different mail packages, currently we support Swift Mailer as default, you must install "swiftmailer/swiftmailer": "~5.0" first.

Then add your mail server information to etc/secret.yml:

# ...

        name: Sender Name
        transport: smtp
        security: tls
        port: 2525
        username: *****
        password: *****

        # Verify SSL
        verify: false

        # Local Domain

    # Sendmail command if use this
    sendmail: /usr/sbin/sendmail

Currently available transports:

  • smtp (SMTP server)
  • mail (PHP default mail() function)
  • sendmail (Use system sendmail)

Create Message

use Windwalker\Core\Mailer\Mailer;

/** @var  $message \Windwalker\Core\Mailer\MailMessage */
$message = Mailer::createMessage('Subject');

    ->from('') // If you don't set from, Mailer will use the `from` info in config.yml
    ->html(true) // Default is true
    ->body('Mail body');

// Send it

Add Mail By Array

You can use array as a set of mail to to(), from(), cc() and bcc().

$message->to(['', '']);

// OR

    '' => 'Name Foo',
    '' => 'Name Bar',

Render Body From View

Use view to render mail in controller:

$view = $this->getView('Mail');

$view['user'] = User::get();
$view['link'] = $link;
$view['content'] = $content;


Or use message built-in render method:

// ...

$message->renderBody('mail.notify', $data, 'edge');



Use file path.

$message->attach('/path/to/file2.pdf', , 'downloaded2.pdf', 'application/pdf');


Use MailAttachment class:

use Windwalker\Core\Mailer\MailAttachment;

$message->attach(new MailAttachment('/path/to/fil.pdf'));

// Or set custom body
$attachment = new MailAttachment;

$message->attach($attachment, 'test.html');

Pre-Defined Messages

You can declare some message classes to quickly re-use them.

namespace Flower\Mail;

// ...

class CheckoutMessage extends MailMessage
    public static function create(User $user = null, Data $product = null)
        // Prepare default data for test
        $user = $user ? : User::get();
        $product = $product ? : new Data;

        return (new static('You checkout a product'))
            ->to($user->email, $user->name)
            ->renderBody('checkout', [
                'user' => $user,
                'product' => $product

Now just create this instance to send mail:

use Flower\Mail\CheckoutMessage;

Mailer::send(CheckoutMessage::create($user, $product));

Style Inliner

Windwalker Includes a simple CSS inliner to help us compile CSS to inline styles that makes our email show normally with some email clients which does not support outside CSS.

To enable CSS inliner, you must install "tijsverkoyen/css-to-inline-styles": "~2.0" first, and add this listener to etc/app/windwalker.php:

// etc/app/windwalker.php or web.php

    'listeners' => [
        'inliner' => \Windwalker\Core\Mailer\Listener\MailInlinerListener::class

Now write your mail template with some styles and send mail:


<!-- Your base mail styles -->
@php( $asset->addCSS('css/mail-style.css') )

    /* Some inline styles */

    h1, h2, h3 {
        color: #444444;

    p {
        line-height: 1.5

    .btn {
        padding: 5px 7px;

<!-- Mail body -->

<a class="btn" href="#">Readmore</a>

This mail template will be compiled to:

<div id="my-mail-wrapper" style="...">
    <h1 style="color: #444444;">Hello</h1>
    <p style="line-height: 1.5;">
    <a style="padding: 5px 7px;" class="btn" href="#">Readmore</a>

You must use $asset to include outside CSS and write in-page CSS in <style> tag. Do not use <link> tag to include CSS.

Due to the performance reason, please don't include whole CSS framework like bootstrap or foundation, Try to write your own mail style to make sure the compiler fast enough.

If you found a typo or error, please help us improve this document.