繁体   English   中英

如果使用队列作业 Laravel 将其发送到 email,图像不会出现在刀片页面中?

[英]images not appear in the blade page if send it to email by using queue job Laravel?

我向用户发送了一个 email 进行验证,通过在注册后使用队列作业,作业成功运行,但发送给用户的(刀片页面)不显示图像,虽然如果我发送刀片页面而不发送它排队工作,图像看起来很好??

所以问题是:

  • 用户 email 收件箱中刀片页面中的图像,如果我使用队列作业发送,则不会出现,尽管如果我直接发送而不使用队列作业,它看起来很好。

image'URL 如果我使用队列作业发送它:

http://localhost/assets/img/logo.png

在此处输入图像描述

如果我在不使用队列作业的情况下发送图像'URL:

http://localhost:8000/assets/img/logo.png

在此处输入图像描述

刀片页面

    <!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');
});

Controller:

    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 env 键。

当在没有 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' }} />

暂无
暂无

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

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