doumeikuan6834 2017-03-31 20:54
浏览 95
已采纳

laravel mailables - >在渲染视图后替换标记

following thing:

I read posts from a wordpress database. The idea is that the posts act as master.

E.g. we have a wordpress post with post content:

Hi XX_USERNAME_XX,

nice to meet you.

XX_LARAVEL_CONTENT_XX

thanks for your donation of XX_DONATION_AMOUNT_XX

bye

I now want to send a normal laravel mailable that when sending out, does the replacements. Now the thing is the views in laravel typically act as master.

in this case though, I want the wordpress post content to be the master, and insert the laravel view into the wordpress content (XX_LARAVEL_CONTENT_XX). And while doing that, replace additional tokens (XX_DONATION_AMOUNT_XX).

How can I do stuff, just before the laravel mailable is rendered? e.g. before the output of the mailable is passed to the mail service, i want to hook in my own filter, take the output from the mailable and put that output into the wordpress post. Is there something supporting this?

  • 写回答

1条回答 默认 最新

  • dongzhao1930 2017-04-01 08:44
    关注

    not quite clear to me why you choose to compose your views this way, but in order to achieve this your way, you could use the callbacks variable in the Mailable class. They will be called upon the message right after it has been composed from your view

    Class MyMail extends Mailable
    {
        use Queueable, SerializesModels;
        public $master;
        publiс $placeholder = 'XX_LARAVEL_CONTENT_XX';
    
        public function __construct($master)
        {
            $this->master = $master; // 'BEFORE + XX_LARAVEL_CONTENT_XX + AFTER';
        }
    
        public function build()
        {
            $this->callbacks[] = function ($message) {
                $oldBody = $message->getBody();
                $newBody = str_replace($this->placeholder, $oldBody, $this->master);
                $message->setBody($newBody);
            };
    
            return $this->view('my-laravel-mail-view') // your normal message setup
                ->to('test@example.com')
                ->subject('Hello');
        }
    }
    

    And if you want to replace multiple variables, remember that str_replace supports arrays as search and replacement:

    str_replace(['aa', 'bb'], ['AA', 'BB'], $subject);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题