MailSendController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\Api23GateTokenService;
  5. use App\Services\Msg\MailTemplateService;
  6. use Illuminate\Http\Request;
  7. use App\Services\CallApiService;
  8. use Exception;
  9. use Illuminate\Support\Facades\Mail;
  10. class MailSendController extends Controller
  11. {
  12. private $callApiService;
  13. private $mailTemplateService;
  14. private $api23GateTokenService;
  15. public function __construct(
  16. CallApiService $callApiService,
  17. MailTemplateService $mailTemplateService,
  18. Api23GateTokenService $api23GateTokenService
  19. )
  20. {
  21. $this->callApiService = $callApiService;
  22. $this->mailTemplateService = $mailTemplateService;
  23. $this->api23GateTokenService = $api23GateTokenService;
  24. }
  25. public function store(Request $request)
  26. {
  27. try {
  28. $result = $this->api23GateTokenService->getToken($request->header('Api23Key'));
  29. if ($result['error']) {
  30. return response()->json($result['message'], 401);
  31. }
  32. $request = json_decode($request->getContent(), true);
  33. if (! $this->mailTemplateService->send($request['Component'], $request['Data'],
  34. $request['ToMail'], $request['Subject'], $result['data'])) {
  35. return response()->json([ 'error' => true, 'message' => 'Failed to send password reset email, please try again.']);
  36. }
  37. } catch (Exception $e) {
  38. return response()->json(['error' => true, 'message' => 'Unauthorized']);
  39. }
  40. return response()->json(['error' => false, 'message' => 'Success']);
  41. }
  42. public function testSend(Request $request)
  43. {
  44. try {
  45. $request = json_decode($request->getContent(), true);
  46. if (! $this->mailTemplateService->testSend($request['Component'], $request['Data'],
  47. $request['ToMail'], $request['Subject'])) {
  48. return response()->json([ 'error' => true, 'message' => 'Failed to send password reset email, please try again.']);
  49. }
  50. } catch (Exception $e) {
  51. return response()->json(['error' => true, 'message' => 'Unauthorized']);
  52. }
  53. return response()->json(['error' => false, 'message' => 'Success']);
  54. }
  55. }