简体   繁体   English

laravel mailables->在呈现视图后替换令牌

[英]laravel mailables -> replace tokens after view rendered

following thing: 以下内容:

I read posts from a wordpress database. 我从wordpress数据库中读取帖子。 The idea is that the posts act as master. 想法是,这些职位担当主人。

Eg we have a wordpress post with post content: 例如,我们有一个WordPress帖子,其中包含帖子内容:

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. 我现在想发送一个普通的laravel可邮寄邮件,在寄出时进行替换。 Now the thing is the views in laravel typically act as master. 现在,问题是laravel中的视图通常充当主视图。

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). 但是,在这种情况下,我希望将wordpress发布内容作为主体,然后将laravel视图插入wordpress内容(XX_LARAVEL_CONTENT_XX)。 And while doing that, replace additional tokens (XX_DONATION_AMOUNT_XX). 并且在执行此操作时,请替换其他令牌(XX_DONATION_AMOUNT_XX)。

How can I do stuff, just before the laravel mailable is rendered? 在laravel mailable呈现之前,我该怎么做? eg 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. 例如,在将mailable的输出传递到邮件服务之前,我想钩住自己的过滤器,从mailable中获取输出,然后将该输出放入wordpress帖子中。 Is there something supporting this? 有什么支持吗?

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. 我不太清楚为什么您选择以这种方式构成视图,但是为了实现这种方式,可以使用Mailable类中的callbacks变量。 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支持数组作为搜索和替换:

str_replace(['aa', 'bb'], ['AA', 'BB'], $subject);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM