在 Laravel 中将变量传递到电子邮件页脚

发布于 2025-01-10 18:18:52 字数 1868 浏览 0 评论 0原文

我想在页脚中显示免责声明:

此电子邮件已发送至 {recipients email address}。

但我不知道如何将电子邮件地址放入标准页脚刀片中。

这是我所拥有的: 在我的通知组件中,我使用具有收件人电子邮件地址的 $rcp_email 进行以下调用:

return (new MailMessage)
            ->subject('Contact Form Submitted')
            ->markdown('emails.generic.contactform', [
                'first_name' => $this->first_name,
                'message' => $this->message,
                'rcp_email' => $this->rcp_email,
                'url_security_issue' => $url_security_issue,
            ]);

emails.generic.contactform 看起来像这样

@extends('emails.main')
@section('content')
(body of email)
@endsection

emails.main 看起来像这样

@component('mail::message')
@yield('content')
@if (isset($unsubscribe_url))
(html code for unsubscribe)
@endif
@endcomponent

标准供应商\mail\html\message 刀片看起来像这样:

@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.front_end_url'), 'logo' => asset('logo-image-white-circular-background.png')])
{{ config('app.name') }}
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset

{{-- Footer --}}
@slot('footer')
@component('mail::footer')
 © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
@endcomponent
@endslot
@endcomponent

I跟踪了刀片的每个级别,并且 $rcp_email 首先在消息刀片中不可用。我尝试通过将 emails.main 的第一行更改为来传递它,

@component('mail::message', ['rcp_email' => $rcp_email])

但在任何一种情况下,当我尝试使用消息刀片中的变量时,我都会收到错误未定义变量:rcp_email

在理想情况下,我想添加html 代码并使用实际页脚刀片中的变量,但我不知道这是否可能。

如果您可以分享任何提示/任何文档或文章来深入描述其工作原理,我将不胜感激?

谢谢, 戈皮

I would like to show a disclaimer in the footer saying

This email was sent to {recipients email address}.

but i can't figure out how i can get the email address into the standard footer blade.

Here is what I have:
In my notification component I make the following call with $rcp_email having the email address of the recipient:

return (new MailMessage)
            ->subject('Contact Form Submitted')
            ->markdown('emails.generic.contactform', [
                'first_name' => $this->first_name,
                'message' => $this->message,
                'rcp_email' => $this->rcp_email,
                'url_security_issue' => $url_security_issue,
            ]);

The emails.generic.contactform looks like this

@extends('emails.main')
@section('content')
(body of email)
@endsection

emails.main looks like this

@component('mail::message')
@yield('content')
@if (isset($unsubscribe_url))
(html code for unsubscribe)
@endif
@endcomponent

the standard vendor\mail\html\message blade looks like this:

@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.front_end_url'), 'logo' => asset('logo-image-white-circular-background.png')])
{{ config('app.name') }}
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endisset

{{-- Footer --}}
@slot('footer')
@component('mail::footer')
 © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
@endcomponent
@endslot
@endcomponent

I traced each level of blade and $rcp_email is first of all not availble in the message blade. I tried to pass it on by changing the first line of emails.main to

@component('mail::message', ['rcp_email' => $rcp_email])

but in either case when i try to use the variable in the message blade, i get the error Undefined variable: rcp_email

In an ideal case, I would want to add the html code and make use of the variable in the actual footer blade, but I don't know if that's possible.

I would much appreciate if there is any hints/any docus or articles you can share that describes in depth how this works?

Thanks,
Goppi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2025-01-17 18:18:52

希望这对其他有问题的人有帮助
根据我的做法,当您使用 @component('mail::message', ['rcp_email' => $rcp_email]) 时,它将传递变量 $rcp_email到消息刀片。

此处将消息刀片 @component('mail::footer') 更改为 @component('mail::footer', ['rcp_email' => $rcp_email]) 将其再次传递到页脚刀片。

在页脚刀片中访问变量,例如 {{$rcp_email}}
因此示例将

sent to {{$rcp_email}}

另外作为注释,在传递像 ['name' =>; 这样的变量时$variable] 它使用名称作为下一个刀片中的变量。所以在这里你可以像 $name 一样访问它

hopefully this helps for others with issue
From what I did, when you use @component('mail::message', ['rcp_email' => $rcp_email]) it will pass the variable $rcp_email to the message blade.

Here change your message blade @component('mail::footer') to @component('mail::footer', ['rcp_email' => $rcp_email]) to pass it again to your footer blade.

In footer blade access the variable like {{$rcp_email}}
so example would be <p>sent to {{$rcp_email}}</p>

Also as a note, when passing the variable like ['name' => $variable] it uses the name as the variable in the next blade. so here you would access it like $name

夜雨飘雪 2025-01-17 18:18:52

谢谢@Jack C - 你的解决方案当时对我不起作用,正如你从我原来的帖子中看到的那样。我尝试使用将 rcp_email 传递到消息刀片

@component('mail::message', ['rcp_email' => $rcp_email])

,当我在消息刀片中测试 $rcp_email 时,它返回了未定义的变量。也就是说,我不明白为什么它不起作用,但我将 @component 与其他刀片的变量一起使用,没有出现问题。也许由于缓存问题而不起作用。

当我决定不使用标准电子邮件模板而是设计自己的电子邮件模板时,我当时提出的解决方案就开放了。
emails.generic.contactform 更改如下:

@extends('emails.layout.layout')
@section('content')
{{-- body of email --}}
@endsection

emails.layout.layout 刀片看起来像这样:

@component('mail::layout')
{{-- Content --}}
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
{{ $recipient ?? '' }}
@endcomponent
@endslot

总而言之,我对如何在刀片之间传递变量的理解如下:

  1. extend - 扩展允许布局刀片使用所有与扩展刀片同名的变量。

    @extends('布局')
    
  2. 组件无法访问父刀片的变量。相反,它们需要被传承。有两种方法可以传递它们:作为一个或多个变量或作为一个槽。在下面的示例中,$name 在组件页脚中可用。

    @component('页脚', ['名称' => $rcp_email])
    @end组件
    
  3. 组件 - 第二种方式是作为插槽。在下面的示例中,变量 $slot 将包含电子邮件地址。

    <前><代码>@component('页脚')
    {{ $rcp_email }}
    @end组件

Thanks @Jack C - your solution didn't work for me back then as you can see from my original post. I tried to pass rcp_email on to the message blade using

@component('mail::message', ['rcp_email' => $rcp_email])

and when I tested $rcp_email within the message blade, it came back with undefined variable. That said, I didn't figure out why it wasn't working, but I used @component with variables for other blades without issues. Perhaps it didn't work due to caching issue.

The solution that I came up with back then opened itself up once I decided not to use the standard email template, but design my own.
The emails.generic.contactform changed as follows:

@extends('emails.layout.layout')
@section('content')
{{-- body of email --}}
@endsection

The emails.layout.layout blade looks like this:

@component('mail::layout')
{{-- Content --}}
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
{{ $recipient ?? '' }}
@endcomponent
@endslot

to summarize, my understanding on how to pass variables between blades is as follows:

  1. extend - extends allows the layout blade to use all variables with the same names as the extended blade.

    @extends('layout')
    
  2. components have no access to the variables of the parent blade. Instead they need to be passed on. There are two ways to pass them on: as one or more variables or as a slot. In the example below $name is available within the component footer.

    @component('footer', ['name' => $rcp_email])
    @endcomponent
    
  3. component - the second way is as a slot. In the example below the variable $slot would contain the email address.

    @component('footer')
    {{ $rcp_email }}
    @endcomponent
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文