Browse Source

feat:email

igb 3 months ago
parent
commit
14d1257370

+ 9 - 7
app/Http/Controllers/ContactController.php

@@ -46,12 +46,14 @@ class ContactController extends Controller
     {
         try {
 
-            // 验证 CSRF Token
-            if (!$request->has('_token') || $request->input('_token') !== csrf_token()) {
-                return response()->json([
-                    'status' => 'error',
-                    'message' => 'Invalid token.',
-                ], 403); // 返回 HTTP 403 错误
+            // 如果请求中有 Token,则进行验证
+            if ($request->has('_token')) {
+                if ($request->input('_token') !== csrf_token()) {
+                    return response()->json([
+                        'status' => 'error',
+                        'message' => 'Invalid token.',
+                    ], 403); // 返回 HTTP 403 错误
+                }
             }
 
             // 从请求中获取所有数据
@@ -83,10 +85,10 @@ class ContactController extends Controller
                 'referer_url' => $distInquiry->referer_url,
             ];
 
+
             // 使用 MailService 静态方法发送邮件
             $mailSent = MailService::sendInquiryMail($inquiryData);
 
-
             // 成功时的响应
             $response = [
                 'status' => 'success'

+ 1 - 0
app/Http/Middleware/VerifyCsrfToken.php

@@ -13,5 +13,6 @@ class VerifyCsrfToken extends Middleware
      */
     protected $except = [
         //
+        'contact'
     ];
 }

+ 39 - 0
app/Mail/InquiryMail.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Mail;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Queue\SerializesModels;
+
+class InquiryMail extends Mailable
+{
+    use Queueable, SerializesModels;
+
+    public $inquiryData;
+
+    /**
+     * Create a new message instance.
+     *
+     * @param  array  $inquiryData
+     * @return void
+     */
+    public function __construct($inquiryData)
+    {
+        $this->inquiryData = $inquiryData;
+    }
+
+    /**
+     * Build the message.
+     *
+     * @return $this
+     */
+    public function build()
+    {
+        return $this->subject('新的询盘邮件') // 设置邮件主题
+        ->view('emails.inquiry') // 指定视图文件
+        ->with(
+         $this->inquiryData, // 传递数据到视图
+        );
+    }
+}