参数#1($ mailable)必须是类型的Illuminate \ Contracts \ Contracts \ Mail \ Mailable,字符串给定

发布于 2025-02-02 16:09:07 字数 590 浏览 4 评论 0原文

我尝试发送带有新创建帖子的电子邮件。但是我遇到了这个错误,但我不明白怎么了?

Illuminate \ Mail \ PendingMail :: send():参数#1($ mailable)必须是类型Illuminate \ Contracts \ Contracts \ Contracts \ Mail \ Mail \ mail \ crings,fess in/users/users/mattiasandersson/documents/utveckling/utveckling/tester/tester/event_listener/app/app/app/app/app/app/app/app/app/app/app/听众/notifypostCreated.php在第37行

public function handle(PostCreated $event)

{

    $users = User::all();

    foreach($users as $user) {

        Mail::to($user->email)->send('emails.post_created', $event->post);

    }

}

I try to send an email with a new created post. But I get this error, but I don't understand what is wrong?

Illuminate\Mail\PendingMail::send(): Argument #1 ($mailable) must be of type Illuminate\Contracts\Mail\Mailable, string given, called in /Users/mattiasandersson/Documents/Utveckling/TESTER/Event_Listener/app/Listeners/NotifyPostCreated.php on line 37

public function handle(PostCreated $event)

{

    $users = User::all();

    foreach($users as $user) {

        Mail::to($user->email)->send('emails.post_created', $event->post);

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无戏配角 2025-02-09 16:09:07
The value in send() should extend mailable
Assuming you have a PostMail class  as shown below
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PostMail extends Mailable 
{
    use Queueable, SerializesModels;


    public $post;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($post)
    {
        //
        $this->post=$post;
    }

    /**
     * Build the message.
     *
     * @return $this`enter code here`
     */
    public function build()
    {
        return $this->subject("Mail from posts")->view('mail.Test_mail');
    }
}

然后,在您的手柄方法上,您可以

public function handle()
    {
        $data = User::all();
        $post=$this->post;
       
        
        
        $postdata = new PostMail($post);
        foreach ($data as $key => $value) {
            $input['email'] = $value->email;
            $input['name'] = $value->name;
           
            Mail::to($input['email'])->send($postdata);
        }
    }

注意:邮件:: to($ input ['email']) - &gt; send($ postData); $ postdata是邮政运输类型,可扩展可邮寄。
希望这会有所帮助

The value in send() should extend mailable
Assuming you have a PostMail class  as shown below
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PostMail extends Mailable 
{
    use Queueable, SerializesModels;


    public $post;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($post)
    {
        //
        $this->post=$post;
    }

    /**
     * Build the message.
     *
     * @return $this`enter code here`
     */
    public function build()
    {
        return $this->subject("Mail from posts")->view('mail.Test_mail');
    }
}

Then on your handle method you can

public function handle()
    {
        $data = User::all();
        $post=$this->post;
       
        
        
        $postdata = new PostMail($post);
        foreach ($data as $key => $value) {
            $input['email'] = $value->email;
            $input['name'] = $value->name;
           
            Mail::to($input['email'])->send($postdata);
        }
    }

Note:Mail::to($input['email'])->send($postdata); $postdata is of type PostMail which extends Mailable.
Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文