猿问

邮寄独特的邮件 (Laravel)

告诉我应该用哪种方式思考。

我一次向多个地址发送邮件。我形成一个视图,将其发送到地址。每封电子邮件必须包含从模型中所有用户中选择的唯一用户名。因此,每个用户都应该有自己的视图(带有他们的名字)。

问题是 - 如何为邮件列表中的每封电子邮件创建唯一的视图?

我不能这样写,因为所有用户都会收到包含所有用户名列表的同一封电子邮件:

$foreach ($users as $user)
{{ $user->name }}
@endforeach

以及如何获得每个用户在信中得到他们的名字的结果?


慕工程0101907
浏览 99回答 1
1回答

MMTTMM

您需要单独发送每个用户的电子邮件,将用户数据传递到您创建的可邮寄类。<?phpnamespace App\Mail;use App\User;use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;class MyCustomUserMail extends Mailable{&nbsp; &nbsp; use Queueable, SerializesModels;&nbsp; &nbsp; public $user;&nbsp; &nbsp; public function __construct(User $user)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->user = $user;&nbsp; &nbsp; }&nbsp; &nbsp; public function build()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->view('emails.users.my_custom_user_view');&nbsp; &nbsp; }}在您的自定义视图中:{{ $user->name }}发送电子邮件:<?phpnamespace App\Http\Controllers;use App\Http\Controllers\Controller;use App\Mail\MyCustomUserMail;use App\User;use Illuminate\Http\Request;use Illuminate\Support\Facades\Mail;class UserController extends Controller{&nbsp; &nbsp; public function doSomething()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $users = User::all();&nbsp; &nbsp; &nbsp; &nbsp; $users->each(function ($user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mail::to($user->email)->send(new MyCustomUserMail($user));&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答