简体   繁体   English

Laravel 发送自定义邮件

[英]Laravel sending custom email

I don't know if this is possible我不知道这是否可能

I am trying to send custom email all the users from my users table.我正在尝试向我的用户表中的所有用户发送自定义电子邮件。 I am using the mail fascades present in laravel.我正在使用 laravel 中的邮件门面。

There is a blade template like this :有一个这样的刀片模板:

@component('mail::message')
# New Book Lunch
 
This is the body of the email.

@component('mail::button', ['url' => $url ?? ''])
View Order
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

My method to send mail looks like this我发送邮件的方法是这样的

class NewsLetterController extends Controller
{

    public function newsletter(Request $request)
    {
        $subject = $request->name;
        $message = $request->message;

        $email = User::pluck('email');
        Mail::to($email)->(Add my custom $message and $subject)->send(new NewsLetter());
    }


}

Here I want to add a custom message and subject my filling a form.在这里,我想添加一条自定义消息,并将我填写的表单作为主题。 How can that be done so I can add custom message?如何做到这一点,以便我可以添加自定义消息?

Looking at the docs: https://laravel.com/docs/8.x/mail#sending-mail查看文档: https : //laravel.com/docs/8.x/mail#sending-mail

You can pass the variables to the Mailable via the constructor.您可以通过构造函数将变量传递给 Mailable。

Mail::to($request->user())->send(new NewsLetter($subject, $message));

And in the Mailable:在 Mailable 中:

class NewsLetter extends Mailable
{
    // ...

    public function __construct(string $subject, string $message) 
    {
       $this->subject = $subject;
       $this->message = $message;
    }
}

If you want to pass the message to the view, your build could look like:如果您想将消息传递给视图,您的build可能如下所示:

class NewsLetter extends Mailable
{
    // ...

    public function build()
    {
        return $this->view('emails.newsletter')
            ->with([
                'message' => $this->message,
            ]);
    }
}

And finally, you add the message in blade:最后,您在刀片中添加消息:

<p>{{ $message }}</p>

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

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