如果使用队列作业 Laravel 将图像发送到电子邮件,图像不会出现在 blade 页面中?
images not appear in the blade page if send it to email by using queue job Laravel?
我向用户发送了一封电子邮件进行验证,通过在注册后使用队列作业,作业 运行 成功但是发送给用户的(blade 页面没有显示图像,虽然如果我发送 blade 页面而不通过队列作业发送,图像显示正常!?
所以问题是:
- 用户邮件收件箱blade页面中的图片,如果我使用队列作业发送,则不会出现,但如果我不使用队列作业直接发送,它看起来很好。
image'URL 如果我使用队列作业发送:
http:///localhost/assets/img/logo.png
image'URL 如果我没有使用队列作业发送它:
http://localhost:8000/assets/img/logo.png
Blade 页面
<!doctype html>
<html lang="en"><head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="{{asset('assets/css/bootstrap.css')}}">
<style type="text/css">
@import url({{asset('assets/css/style.css')}});
body {
}
</style>
<title>login Page</title>
</head>
<body>
<section class="row">
<div class="col col-sm ">
</div>
<div class="col col-sm ml-5 pl-5" id="col1" >
<div class="container">
<div class="row mt-5 pl-5 mb-5 pb-5">
<img src="{{asset('assets/img/logo.png')}}" width="80" height="80" alt=""/>
</div>
<div class="row mt-5 mb-5 pb-5 ">
<img src="{{asset('assets/img/Sign in ~ Register Illustration.png')}}" width="263" height="241" alt=""/>
</div>
<h1>Hello, </h1>
<h2>Verification Code </h2>
<div class="row mt-5 mb-5 pb-5 pr-5">
<p class="font-style-normall font-size-18 line-spacing-33 font-family-cairo text-muted" id="text1">1Me will Keep your Contacts Secured in our Data base</p>
</div>
</div>
</div>
</section>
<script src="{{asset('js/bootstrap.js')}}"></script>
</body>
</html>
路线:
Route::group(['middleware'=>'guest:web'], function(){
Route::get('/register', [registerController::class,'register'])->name('site.register');
Route::match(['get','post'],'/register-create', [registerController::class,'create'])->name('site.register.create');
});
控制器:
public function create(RegisterRequest $request)
{
$user = User::create([
'firstName' => $request->firstName,
'middleName' => $request->middleName,
'lastName' => $request->lastName,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$on = \Carbon\Carbon::now()->addSecond(10);
dispatch(new VerifyEmailJob($user))->delay($on);
return redirect()->route('landingPage')->with(['success'=>'We sent verifying email check it']);
}
队列作业:
<?php
namespace App\Jobs;
use App\Mail\VerifyEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class VerifyEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public $user;
public function __construct($user)
{
$this->user= $user;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mail::to($this->user->email)->send(new VerifyEmail($this->user));
}
}
邮件class:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class VerifyEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $user;
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$user = $this->user;
return $this->subject('Mail from Oneme')
->view('site.auth.verifyEmail',compact('user'));
}
}
任何帮助
我以前遇到过这个问题。对于电子邮件,所有路径都必须是完全合格的 URL。因为队列作业无法巧妙地计算出应用程序的基础 url 应该是什么。
https://example.com/static/logo.png
是完全合格的 URL,但 /static/logo.png
不是。为此,我使用了 APP_URL
环境密钥。
当没有 http 请求的情况下执行操作时(如在队列系统中),laravel 无法将 /static/logo.png
转换为 https://example.com/static/logo.png
在 .env
文件上,我会做类似
的事情
APP_URL = "https://example.com" #change this with your application url
在视图文件上,我会做类似的事情
<img src={{ env('APP_URL') . '/static/logo.png' }} />
我向用户发送了一封电子邮件进行验证,通过在注册后使用队列作业,作业 运行 成功但是发送给用户的(blade 页面没有显示图像,虽然如果我发送 blade 页面而不通过队列作业发送,图像显示正常!?
所以问题是:
- 用户邮件收件箱blade页面中的图片,如果我使用队列作业发送,则不会出现,但如果我不使用队列作业直接发送,它看起来很好。
image'URL 如果我使用队列作业发送:
http:///localhost/assets/img/logo.png
image'URL 如果我没有使用队列作业发送它:
http://localhost:8000/assets/img/logo.png
Blade 页面
<!doctype html>
<html lang="en"><head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="{{asset('assets/css/bootstrap.css')}}">
<style type="text/css">
@import url({{asset('assets/css/style.css')}});
body {
}
</style>
<title>login Page</title>
</head>
<body>
<section class="row">
<div class="col col-sm ">
</div>
<div class="col col-sm ml-5 pl-5" id="col1" >
<div class="container">
<div class="row mt-5 pl-5 mb-5 pb-5">
<img src="{{asset('assets/img/logo.png')}}" width="80" height="80" alt=""/>
</div>
<div class="row mt-5 mb-5 pb-5 ">
<img src="{{asset('assets/img/Sign in ~ Register Illustration.png')}}" width="263" height="241" alt=""/>
</div>
<h1>Hello, </h1>
<h2>Verification Code </h2>
<div class="row mt-5 mb-5 pb-5 pr-5">
<p class="font-style-normall font-size-18 line-spacing-33 font-family-cairo text-muted" id="text1">1Me will Keep your Contacts Secured in our Data base</p>
</div>
</div>
</div>
</section>
<script src="{{asset('js/bootstrap.js')}}"></script>
</body>
</html>
路线:
Route::group(['middleware'=>'guest:web'], function(){
Route::get('/register', [registerController::class,'register'])->name('site.register');
Route::match(['get','post'],'/register-create', [registerController::class,'create'])->name('site.register.create');
});
控制器:
public function create(RegisterRequest $request)
{
$user = User::create([
'firstName' => $request->firstName,
'middleName' => $request->middleName,
'lastName' => $request->lastName,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$on = \Carbon\Carbon::now()->addSecond(10);
dispatch(new VerifyEmailJob($user))->delay($on);
return redirect()->route('landingPage')->with(['success'=>'We sent verifying email check it']);
}
队列作业:
<?php
namespace App\Jobs;
use App\Mail\VerifyEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class VerifyEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public $user;
public function __construct($user)
{
$this->user= $user;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mail::to($this->user->email)->send(new VerifyEmail($this->user));
}
}
邮件class:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class VerifyEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $user;
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$user = $this->user;
return $this->subject('Mail from Oneme')
->view('site.auth.verifyEmail',compact('user'));
}
}
任何帮助
我以前遇到过这个问题。对于电子邮件,所有路径都必须是完全合格的 URL。因为队列作业无法巧妙地计算出应用程序的基础 url 应该是什么。
https://example.com/static/logo.png
是完全合格的 URL,但 /static/logo.png
不是。为此,我使用了 APP_URL
环境密钥。
当没有 http 请求的情况下执行操作时(如在队列系统中),laravel 无法将 /static/logo.png
转换为 https://example.com/static/logo.png
在 .env
文件上,我会做类似
APP_URL = "https://example.com" #change this with your application url
在视图文件上,我会做类似的事情
<img src={{ env('APP_URL') . '/static/logo.png' }} />