Yii2: Using Swiftmailer Plugin (Openbuildings \ Swiftmailer \ CssInlinerPlugin)

My yii2 app successfully sends emails with swiftmailer yii2 extension using given default layout in \app\mail\layouts

with name html.php

like

Yii::$app->mailer->compose('@app/mail/templates/myTemplate', [
    'param1' => $param1, 'param2' => $param2
])->setFrom($senderAdress)->setTo($reveiverAdress)->setSubject('Subject')->send();

      

Unfortunately this way of css in style tags in header is not included in html.php

as suggested by the official yii2 guide yiiframework.com/doc-2.0/guide-tutorial-mailing.html (example provided:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    <style type="text/css">
        .heading {...}
        .list {...}
        .footer {...}
    </style>
    <?php $this->head() ?>
</head>

      

So I tried to enable OpenBuildings swiftmailer css-inliner-plugin by installing it with composer and including it as suggested in this tutorial , expanding mine web.php

to:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'transport' => [
        'class'      => 'Swift_SmtpTransport', 
        'host' => 'smtp.xyz.com',
        'username'   => 'username',
        'password' => 'password',
        'port' => '587',
        'encryption' => 'tls',
        'plugins'    => [
            [
                'class' => 'Openbuildings\Swiftmailer\CssInlinerPlugin',
            ],
        ],
    ],
],

      

Mail is still sent without tag content <style>

. Anyone come across including plugins in the yii2 swiftmailer extension? Or is there another way to use the extension to avoid using swiftmailer plugins at all?

+3


source to share


1 answer


A bit late, but it might help someone find this later ...

I used the same code to enable the CssInlinerPlugin plugin and it worked right away. I have registered some css in my mailbox, like this.

$this->registerCss("
    p, td, th {text-align: left; font-size: 12px; }
    .classname {color: blue; }
"); 

      



... both in the mail that was sent and all html tags now have the same styling. So the plugin seems to work just fine.

Regarding your last question. I haven't found a better way to quickly deal with this for Yii2 than using this plugin. The plugin is based on the tijsverkoyen / css-to-inline-styles package , which is also used in other frameworks.

It is noticed in the source code for TijsVerkoyen \ CssToInlineStyles that the css-classnames are removed from the style tag in the post, but the css content is being passed to the correct tag. Maybe this can explain some of what you saw? (This behavior is configurable, of course.)

+1


source







All Articles