我正在尝试修改我在旧网站上发送电子邮件的模板,运行Laravel 5.4
我最终计划更新到至少Laravel 5.5,可能还有Laravel 5.7 - 但我现在不想这样做,除非严格必要(这会对我的一些控制器和一些额外的控制器进行一些重大的重写)测试)
我跑了:
php artisan vendor:publish --tag=laravel-mail
这在resources/views/vendor/mail
中创建了文件
然后我编辑了这些文件并尝试发送消息。没变。
然后我编辑了vendor/laravel/framework/src/Illuminate/Mail/resources/views/
中的文件并发送了一条消息 - 新模板出现了。
因此,尽管存在resources/views/vendor/mail
文件夹,Laravel仍然在运行vendor/
后从php artisan vendor:publish
文件夹中读取。我该如何解决?我究竟做错了什么?
一些额外的信息,以防它有帮助。这是我的邮件模板(resources/views/mail/email-a-friend.blade.php
):
@component('mail::message')
Your friend, {{ $senderName }}, has sent you information about a property they feel you might be interested in.
This property is listed by {{ config('app.name') }}. To view this property and more like it, please click the link below.
@if($agent->id !== $property->agent->id)
[{{ url($property->url()) }}?agent={{ $agent->first_name }}-{{ $agent->last_name }}]({{ url($property->url()) }}?agent={{ $agent->first_name }}-{{ $agent->last_name }})
@else
[{{ url($property->url()) }}]({{ url($property->url()) }})
@endif
@if($text != "")
They also sent this message:
@component('mail::panel')
{{ $text }}
@endcomponent
@endif
@endcomponent
这是将电子邮件排队的控制器(app/http/Controllers/AjaxController.php
- 只是相关功能):
public function emailAFriend(Request $request)
{
$property = \App\Models\Property\Property::find($request->input('property-id'));
$agent = $property->agent;
if ($request->input('agent-id') !== $agent->id) {
$agent = \App\User::find($request->input('agent-id'));
}
Mail::to($request->input('send-to'))
->queue(new \App\Mail\EmailAFriend($property, $agent, $request->input('name'), $request->input('reply-to'), $request->input('text')));
return Response::json("success", 200);
}
这是Mailable(app/Mail/EmailAFriend.php
):
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Property\Property;
use App\User;
class EmailAFriend extends Mailable
{
use Queueable, SerializesModels;
public $subject = "Someone sent you a property!";
public $property;
public $agent;
public $senderName;
public $senderEmail;
public $text;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Property $property, User $agent, $name, $email, $text)
{
$this->subject = "$name sent you information about a property";
$this->property = $property;
$this->agent = $agent;
$this->senderName = $name;
$this->senderEmail = $email;
$this->text = $text;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.email-a-friend')
->replyTo($this->senderEmail, $this->senderName)
->attachData(
$this->property->generatePdf(['agent' => $this->agent])->inline(),
"{$this->property->details->lot_size} acres in {$this->property->location->county} county.pdf",
[
'mime' => 'application/pdf'
]
);
}
}
出于测试目的,我正在使用sync
QueueDriver,因此在发出AJAX请求时立即发送。在制作中我使用database
QueueDriver。
组件:
resources/views/vendor/mail/html/message.blade.php
:
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
<img src="{{ url('/img/layout/logo.png') }}" alt="{{ config('app.name') }}" />
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}
{{-- Subcopy --}}
@if (isset($subcopy))
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endif
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
@endcomponent
@endslot
@endcomponent
resources/views/vendor/mail/markdown/message.blade.php
:
@component('mail::layout')
{{-- Header --}}
@slot('header')
@component('mail::header', ['url' => config('app.url')])
![{{ config('app.name') }}]({{ url('/img/layout/logo.png') }})
@endcomponent
@endslot
{{-- Body --}}
{{ $slot }}
{{-- Subcopy --}}
@if (isset($subcopy))
@slot('subcopy')
@component('mail::subcopy')
{{ $subcopy }}
@endcomponent
@endslot
@endif
{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} {{ config('app.name') }}. All rights reserved.
@endcomponent
@endslot
@endcomponent
这两者之间的差异和默认组件(vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/message.blade.php
和markdown等价物)在标题中:
{{ config('app.name') }}
replaced with:
<img src="{{ url('/img/layout/logo.png') }}" alt="{{ config('app.name') }}" />
我试图用他们的徽标替换公司名称。当我进入vendor/laravel/framework/src/Illuminate/Mail/resources/views/markdown/message.blade.php
并直接编辑此文件时,我确实在生成的电子邮件中看到了徽标。因此,尽管存在已发布的组件,它仍然从vendor/
目录中读取(并且编辑vendor/
目录并不好,因为这样的更改将不会在生产中持续存在)
因此,在通过Laravel源挖掘超过一个小时之后,我终于弄明白了。
componentPaths
变量加载组件componentPaths
变量由loadComponentFrom()
设定loadComponentsFrom
在Markdown渲染器的构造函数中调用并通过$options['paths']
知道这一点后,我开始研究“Laravel降价选项路径”并发现以下内容:https://stackoverflow.com/a/44264874/436976
我更新了config/mail.php
并添加了推荐的线条,它完美无缺!我觉得vendor:publish
应该为我做这件事,或者至少应该在official Laravel documentation中提到这一步,但幸运的是我在一天之内想到了这一点 - 所以总是很好
事实证明,Laravel官方文档中提到了这一点,而不是我所期望的。
我的网站最初是Laravel 5.1网站,升级到5.2,然后升级到5.3,最后升级到5.4,然后才上线(我从未更新到5.5,因为一旦网站上线,我想最小化对底层框架的更改)
每次Laravel升级时,我都会继续推进config/
目录中的旧文件,显然我很难跟上升级指南,因为它们很清楚:
https://laravel.com/docs/5.4/upgrade
New Configuration Options
为了为Laravel 5.4的新Markdown邮件组件提供支持,您应该将以下配置块添加到邮件配置文件的底部:
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
如果我按照指示更新了我的配置文件,我将永远不会遇到这些问题。