MailTemplateService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Services\Msg;
  3. use App\Helpers\File;
  4. use App\Services\CallApiService;
  5. use Illuminate\Support\Facades\Mail;
  6. use Illuminate\Support\Str;
  7. class MailTemplateService
  8. {
  9. private $callApiService;
  10. public function __construct(CallApiService $callApiService)
  11. {
  12. $this->callApiService = $callApiService;
  13. }
  14. public function testSend($component, $data, $toMail, $subject)
  15. {
  16. Mail::send($component, ['data' => $data],
  17. function ($message) use ($toMail, $subject) {
  18. $message->to($toMail);
  19. $message->subject($subject);
  20. }
  21. );
  22. if (count(Mail::failures()) > 0){
  23. return false;
  24. }
  25. return true;
  26. }
  27. public function send($component, $data, $toMail, $subject, $gateToken = null)
  28. {
  29. $data = $this->getSetup($data, $gateToken);
  30. Mail::send($component, ['data' => $data],
  31. function ($message) use ($toMail, $subject) {
  32. $message->to($toMail);
  33. $message->subject($subject);
  34. }
  35. );
  36. if (count(Mail::failures()) > 0){
  37. return false;
  38. }
  39. return true;
  40. }
  41. public function putTemplate($filePath, $message, $theme)
  42. {
  43. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  44. if ($theme) {
  45. $fullFilePath = public_path("themes/{$theme}/resources/views/msg/dabory/pro/{$countryCode}/email/{$filePath}.blade.php");
  46. } else {
  47. $fullFilePath = resource_path("views/msg/dabory/pro/{$countryCode}/email/{$filePath}.blade.php");
  48. }
  49. // $exists = file_exists($fullFilePath);
  50. // $message = Str::replace('{{%24', '{{$', $message);
  51. // $message = Str::replace('{{msset(%24', '{{msset($', $message);
  52. return File::forceFilePutContents($fullFilePath, $message);
  53. }
  54. private function getSetup($data, $gateToken)
  55. {
  56. $headers = [];
  57. if ($gateToken) {
  58. $headers = [
  59. 'GateToken' => $gateToken
  60. ];
  61. }
  62. $setup = $this->callApiService->callApi([
  63. 'url' => 'setup-get',
  64. 'data' => [
  65. 'SetupCode' => 'email-header-footer',
  66. ],
  67. 'headers' => $headers
  68. ]);
  69. return array_merge([
  70. 'C1' => $setup['Logo'],
  71. 'C2' => $setup['Url'],
  72. 'C3' => $setup['Name'],
  73. 'C4' => $setup['Footer'],
  74. ], $data);
  75. }
  76. }