在电子邮件中显示Laravel发送的图像,可以通过以下步骤实现:
Mail
类来发送邮件。在发送邮件时,可以通过attach
方法将图像附加到邮件中。例如:use Illuminate\Support\Facades\Mail;
use App\Mail\MyEmail;
$imagePath = public_path('images/myimage.jpg');
$mailData = [
'title' => 'My Email',
'content' => 'This is an email with an image',
'image' => $imagePath,
];
Mail::to('recipient@example.com')->send(new MyEmail($mailData));
MyEmail
,继承自Illuminate\Mail\Mailable
类,并在build
方法中设置邮件内容和附件。例如:namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyEmail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
public function __construct($mailData)
{
$this->mailData = $mailData;
}
public function build()
{
return $this->view('emails.myemail')
->with($this->mailData)
->attach($this->mailData['image'], [
'as' => 'myimage.jpg',
'mime' => 'image/jpeg',
]);
}
}
resources/views/emails
目录下创建一个名为myemail.blade.php
的邮件模板文件,用于设置邮件的HTML内容。在模板中,可以使用<img>
标签来显示图像。例如:<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
<h1>{{ $title }}</h1>
<p>{{ $content }}</p>
<img src="{{ $message->embed($image) }}" alt="My Image">
</body>
</html>
$message->embed()
方法生成一个可访问的URL。这样,你就可以在电子邮件中显示Laravel发送的图像了。
领取专属 10元无门槛券
手把手带您无忧上云