# Laravel - Mail > 在文件中有提供一些 mail service 可以用但目前先以 mailtrap 為例, mailtrap 是一個 dummpy 的 mailbox 意思是說你會看到在這個信箱中是有郵件的但實際上是沒有送出信 ## env 設定 在.env 中做以下設定 ``` MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=xxxxx MAIL_PASSWORD=xxxxx MAIL_ENCRYPTION=tls ``` ## 建立 mailable class > 在 laravel 中是用 mailable 的 class 是操控郵 以下指令會產生 mailable class 建立後檔案是在目錄 `app/Mail` ``` php artisan make:mail OrderShipped ``` ## mailable class methods ### from - 從哪邊寄出 #### local ```php= public function build() { return $this->from('example@example.com') ->view('emails.orders.shipped'); } ``` #### global ```php= 'from' => ['address' => 'example@example.com', 'name' => 'App Name'], ``` ### view - 信件的 blade ```php= public function build() { return $this->view('emails.orders.shipped'); } ``` 另外如果要在 blade 中帶一些自己的參數有兩種方法 第一種是 constructor ```php= // mailable class public $order; /** * Create a new message instance. * * @param \App\Models\Order $order * @return void */ public function __construct(Order $order) { $this->order = $order; } //blade <div> Price: {{ $order->price }} </div> ``` 第二種是 with ```php= // mailable class return $this->view('emails.orders.shipped') ->with([ 'orderName' => $this->order->name, 'orderPrice' => $this->order->price, ]); // blade <div> Price: {{ $orderPrice }} </div> ``` ### to - 設定要送到的地方 / send 送出郵件 ```php= Mail::to($request->user())->send(new OrderShipped($order)); ``` ## 其他 ### 其它 drivers 設定 - [Mailgun Driver](https://laravel.com/docs/8.x/mail#mailgun-driver) - [Postmark Driver](https://laravel.com/docs/8.x/mail#postmark-driver) - [SES Driver](https://laravel.com/docs/8.x/mail#ses-driver) ### 建立 markdown 的 template ``` php artisan make:mail OrderShipped --markdown=emails.orders.shipped ``` ###### tags: `2021` `laravel` `mail` `需要修正` {%hackmd BJrTq20hE %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up