瀏覽代碼

Lesson 26: Sending email.

Frederic G. MARAND 7 年之前
父節點
當前提交
c9ef9b4183
共有 5 個文件被更改,包括 64 次插入14 次删除
  1. 3 0
      app/Http/Controllers/RegistrationController.php
  2. 46 0
      app/Mail/Welcome.php
  3. 2 2
      config/mail.php
  4. 13 0
      resources/views/emails/welcome.blade.php
  5. 0 12
      routes/web.php

+ 3 - 0
app/Http/Controllers/RegistrationController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers;
 
+use App\Mail\Welcome;
 use App\User;
 use Illuminate\Http\Request;
 
@@ -33,6 +34,8 @@ class RegistrationController extends Controller
         // Sign user in.
         auth()->login($user);
 
+        \Mail::to($user)->send(new Welcome($user));
+
         // Redirect to home page.
         return redirect()->home();
     }

+ 46 - 0
app/Mail/Welcome.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace App\Mail;
+
+use App\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Contracts\Queue\ShouldQueue;
+
+class Welcome extends Mailable
+{
+    use Queueable, SerializesModels;
+
+    /**
+     * With Mailable descendents, any public property is available to view().
+     *
+     * Or you could pass them manually.
+     *
+     * @var User
+     *
+     * @see Mailable::buildViewData()
+     */
+    public $user;
+
+    /**
+     * Create a new message instance.
+     */
+    public function __construct(User $user)
+    {
+        $this->user = $user;
+    }
+
+    /**
+     * Build the message.
+     *
+     * @return $this
+     */
+    public function build()
+    {
+        return $this->view('emails.welcome')
+            // Not needed because $this->user is public.
+            // ->with(['user' => $this->user])
+            ;
+    }
+}

+ 2 - 2
config/mail.php

@@ -56,8 +56,8 @@ return [
     */
 
     'from' => [
-        'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
-        'name' => env('MAIL_FROM_NAME', 'Example'),
+        'address' => env('MAIL_FROM_ADDRESS', 'support@osinet.fr'),
+        'name' => env('MAIL_FROM_NAME', 'OSInet support'),
     ],
 
     /*

+ 13 - 0
resources/views/emails/welcome.blade.php

@@ -0,0 +1,13 @@
+<!doctype html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport"
+          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
+    <meta http-equiv="X-UA-Compatible" content="ie=edge">
+    <title>Document</title>
+</head>
+<body>
+    <h1>Welcome to Laracasts, {{ $user->name }}</h1>
+</body>
+</html>

+ 0 - 12
routes/web.php

@@ -20,18 +20,6 @@
 |--------------------------------------------------------------------------
 */
 
-use App\Billing\Stripe;
-use App\Providers\AppServiceProvider;
-
-// Can use either the class name or a readable service name, thanks to aliasing.
-$stripe = App::make(AppServiceProvider::STRIPE);
-$stripeAgain = app(AppServiceProvider::STRIPE);
-
-$stripeAlso = resolve(Stripe::class);
-$stripeToo = app()->make(Stripe::class);
-
-dd($stripe, $stripeAgain, $stripeAlso, $stripeToo);
-
 /** @var \Illuminate\Routing\Router $this */
 $this->get('/', 'PostsController@index')
     ->name('home');