【问题标题】:Yii2 swiftmailer: get generated messageYii2 swiftmailer:获取生成的消息
【发布时间】:2016-05-12 01:51:18
【问题描述】:

我想将发送的电子邮件保存到数据库中。我将 Yii2 与 Swiftmailer 一起使用,我的问题是:有没有办法在发送之后或之前获取生成的 htmlBody(带有变量的模板)?

如果不是,我如何在模型类中将视图文件生成到 $message 变量中?

谢谢。

【问题讨论】:

    标签: php yii2 swiftmailer


    【解决方案1】:

    在发送前保存消息。如果您将内容保存为 .EML 文件,它会完美运行。

    $email = Yii::$app->mailer->compose('simpleEmail')
        ->setFrom(['sender@senderdomain.com' => 'Sender Name'])
        ->setTo($emails_array)
        ->setSubject('subject');
    
    $mailMessage = $email->toString();
    
    $email->send();
    

    【讨论】:

      【解决方案2】:

      是的,您可以轻松保存生成的电子邮件的 Html 正文

      如果您在/controllers/XyzController.php 中有您的电子邮件发送代码:

      步骤:1

          $param = 'some value';
          $path_to_email_template = '@app/mail/layouts/eg';
          $message = $this->renderPartial($path_to_email_template, ['param'=>$param]);
          echo "$message"; // test
          \Yii::$app->mailer->compose()
            ->setTo('sohelahmed@peerbits.com')
            ->setFrom('example@mail.com')
            ->setSubject('Hello')
            ->setHtmlBody($message)
            ->send();
      

      步骤:2

      现在您的电子邮件正文存储在$message 变量中,您可以像这样保存它

      $model = new Somemodel; // consider Somemodel model
      $model->html_content = $message; // consider Somemodel model have attribute 'html_content'
      // assign another properties too, if any
      $model->save(true);
      // Great, email body saved in DataBase, if no validation error occurs. If validation error occurs use print_r($model->errors); to debug
      

      如果你在任何视图文件中发送邮件,那么在/views/xyx/index.php(比如在 index.php 文件中)

      替换STEP:1的第三行

      $message = $this->renderPartial($path_to_email_template, ['param'=>$param]);
      

      通过

      $message = $this->render($path_to_email_template, ['param'=>$param]);
      

      然后继续。 就是这样。

      【讨论】:

        【解决方案3】:

        我认为这是不可能的。但是你可以在邮件组件的配置中设置'useFileTransport' => true。这会将邮件保存在 runtime/mail 文件夹中,而不是发送它们。保存的文件采用 eml 格式,例如可以通过 Thunderbird 查看。要更改文件夹的路径,请使用属性“fileTransportPath”。

        据我所知,您不能同时将它们发送和保存到文件系统。

        希望我能帮助您解答第一个问题。

        【讨论】:

          【解决方案4】:

          您可以阅读有关 fileTransportCallback 的信息:

          http://www.yiiframework.com/doc-2.0/yii-mail-basemailer.html#$fileTransportCallback-detail

          作为回报,您将收到$mailer$message 的回调。 保存到数据库

          【讨论】:

            【解决方案5】:

            您可以将 HTML 保存在视图文件中:

            $message = Yii::$app->mailer->compose('myview', ['param1'=>'param'])
              ->setTo('...')
              ->setFrom('...')
              ->setSubject('');
            
            //And then maybe you can get the textBody
            $body = $message->getTextBody();//I'm not sure for this part
            
            return $message->send();
            

            视图文件 (myview") 应该在 "mail" 文件夹 (/mail/myview.php) 中。 我没有测试它。

            见文档:http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html#composing-mail-content

            【讨论】:

            • 这不起作用:调用未知方法:yii\swiftmailer\Message::getTextBody()
            猜你喜欢
            • 2014-12-20
            • 2017-04-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-19
            • 1970-01-01
            相关资源
            最近更新 更多