Yii2高级电子邮件模板

时间:2016-01-19 12:24:23

标签: email yii2 components html-email yii2-advanced-app

我正在使用Yii2高级版。我已在common\config\main-local.php中配置了电子邮件配置,如下所示:

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',           
            'viewPath' => '@common/mail',
            'useFileTransport' => false,
            'transport' => [
             'class' => 'Swift_SmtpTransport',
             'host' => 'smtp.gmail.com',
             'username' => 'XXXXX',
             'password' => 'XXXXXX',
             'port' => '587',
             'encryption' => 'tls',
         ],
        ],

我在common\components\EmailComponent.php下创建了一个电子邮件组件 其中包含发送电子邮件的代码:

protected function sendEmail($email, $subject, $message, $options= array()) {
            $email = 'myemail@myemail.com';
           **$emailSend = Yii::$app->mailer->compose(['html' =>'layouts/html'],['content' => $message])**
                    ->setFrom(["myemail@myemail.com"])
                    ->setTo($email)
                    ->setSubject($subject);
        return $emailSend->send();
    }

我在

下创建了两个布局文件

1)common\mail\layouts\html.php

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
    </head>
    <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
        <table id="Table_01" border="0" cellpadding="0" cellspacing="0" align="center" height="411" width="600" style="font-family:arial; font-size:14px;">
            <tbody>      
<tr>
                **<td colspan="4" style="border-bottom:1px solid #bc2744; height:1px">First</td>**
            </tr>  
                <tr>
                    <td colspan="2">
                       <?php
                       echo $content;                   
                       ?>
                    </td>
                </tr>
                **<td colspan="4" style="border-bottom:1px solid #bc2744; height:1px">Second</td>**
            </tbody>
        </table>   
    </body>
    </html>

2)common\mail\layouts\text.php

<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
<?=  $content; ?>
<?php $this->endBody() ?>
<?php $this->endPage() ?>

当我从任何部分发送电子邮件然后我收到一封电子邮件,但它显示“第一”文本两次然后我的内容将来,然后再次显示“第二”文本两次来自电子邮件html.php文件。 我不知道为什么会这样,我搜索了很多但没有运气。

3 个答案:

答案 0 :(得分:2)

更改模板的路径: 例如.service('FeedbackFactory',['$resource','baseURL',function($resource,baseURL){ this.putFeedback = function(){ return $resource(baseURL+"feedback/",{'update':{method:'POST'}}); }; } ]) ; common\mail\layouts\html.php

答案 1 :(得分:0)

尝试设置htmlBody

protected function sendEmail($email, $subject, $message, $options= array()) {

  $email = 'myemail@myemail.com';
  $emailSend = Yii::$app->mailer->compose()
   ->setFrom('myemail@myemail.com')
   ->setTo($email)
   ->setSubject($subject)
   ->setHtmlBody($message);

   return $emailSend->send();
}

答案 2 :(得分:0)

Yii::$app->mailer->compose('layouts/html', ['content' => $message])
->setFrom('sender@email.com')
->setTo("receiver@email.com")
->setSubject("subject")
->send();

您必须拥有/common/mail/layouts/html.php