将样式应用于YII2中的电子邮件(Gmail不允许我)

时间:2016-04-01 18:49:32

标签: php email yii2

下午好!如何为此邮件添加样式?我使用YII2和swiftmailer库。这是我的例子:

$a = Yii::$app -> mailer -> compose()
-> setFrom('')
-> setTo('')
-> setSubject('My title')
-> setTextBody('Plain text content')
-> setHtmlBody('My text')
-> send();

邮件/布局/ html.php

<?php
use yii\helpers\Html;

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    <style type="text/css">
    .tg  {border-collapse:collapse;border-spacing:0;border-color:#ccc;}
    .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;border-top-width:1px;border-bottom-width:1px;}
    .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;border-top-width:1px;border-bottom-width:1px;}
    .tg .tg-yw4l{vertical-align:top}
    .tg .tg-b7b8{background-color:#f9f9f9;vertical-align:top}
    </style>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
    <?php $this->beginBody() ?>
    <?= $content ?>
    <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

Greethings!

2 个答案:

答案 0 :(得分:0)

在您的项目脚手架中,您应该有一个带

的邮件目录

mail / layout / html.php

在此内容中

  <?php
  use yii\helpers\Html;

  /* @var $this \yii\web\View view component instance */
  /* @var $message \yii\mail\MessageInterface the message being composed */
  /* @var $content string main view render result */
  ?>
  <?php $this->beginPage() ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
      <title><?= Html::encode($this->title) ?></title>
      <?php $this->head() ?>
  </head>
  <body>
      <?php $this->beginBody() ?>
      <?= $content ?>
      <?php $this->endBody() ?>
  </body>
  </html>
  <?php $this->endPage() ?>

我建议您使用此区域的格式和样式

答案 1 :(得分:0)

gmail和其他基于Web的客户端会删除外部样式表和/或头标记。 (见https://litmus.com/blog/understanding-gmail-and-css-part-1

您最好的选择是将所有样式直接内嵌到HTML元素中