Ver código fonte

fix: sendgrid mail service

M1Screw 2 anos atrás
pai
commit
f7f2b1f09f

+ 5 - 17
src/Services/Mail/Mailgun.php

@@ -6,35 +6,23 @@ namespace App\Services\Mail;
 
 use App\Models\Setting;
 use Exception;
-use Mailgun\Mailgun as MailgunService;
+use Mailgun\Mailgun as MG;
 use Psr\Http\Client\ClientExceptionInterface;
 use function basename;
 
 final class Mailgun extends Base
 {
-    private array $config;
-    private MailgunService $mg;
+    private MG $mg;
     private mixed $domain;
     private mixed $sender;
 
     public function __construct()
-    {
-        $this->config = $this->getConfig();
-        $this->mg = MailgunService::create($this->config['key']);
-        $this->domain = $this->config['domain'];
-        $this->sender = $this->config['sender_name'] . ' <' . $this->config['sender'] . '>';
-    }
-
-    public function getConfig(): array
     {
         $configs = Setting::getClass('mailgun');
 
-        return [
-            'key' => $configs['mailgun_key'],
-            'domain' => $configs['mailgun_domain'],
-            'sender' => $configs['mailgun_sender'],
-            'sender_name' => $configs['mailgun_sender_name'],
-        ];
+        $this->mg = MG::create($configs['mailgun_key']);
+        $this->domain = $configs['mailgun_domain'];
+        $this->sender = $configs['mailgun_sender_name'] . ' <' . $configs['mailgun_sender'] . '>';
     }
 
     /**

+ 5 - 17
src/Services/Mail/Postal.php

@@ -12,30 +12,18 @@ use function mime_content_type;
 
 final class Postal extends Base
 {
-    private array $config;
     private Client $client;
     private Message $message;
 
     public function __construct()
-    {
-        $this->config = $this->getConfig();
-        $this->client = new Client($this->config['host'], $this->config['key']);
-        $this->message = new Message();
-        $this->message->sender($this->config['sender']); # 发件邮箱
-        $this->message->from($this->config['name']. ' <' . $this->config['sender'] . '>'); # 发件人
-        $this->message->replyTo($this->config['sender']);
-    }
-
-    public function getConfig(): array
     {
         $configs = Setting::getClass('postal');
 
-        return [
-            'host' => $configs['postal_host'],
-            'key' => $configs['postal_key'],
-            'sender' => $configs['postal_sender'],
-            'name' => $configs['postal_name'],
-        ];
+        $this->client = new Client($configs['postal_host'], $configs['postal_key']);
+        $this->message = new Message();
+        $this->message->sender($configs['postal_sender']); # 发件邮箱
+        $this->message->from($configs['postal_name'] . ' <' . $configs['postal_sender'] . '>'); # 发件人
+        $this->message->replyTo($configs['postal_sender']);
     }
 
     public function send($to, $subject, $text, $files): void

+ 8 - 19
src/Services/Mail/SendGrid.php

@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace App\Services\Mail;
 
 use App\Models\Setting;
+use SendGrid as SG;
 use SendGrid\Mail\Mail;
 use SendGrid\Mail\TypeException;
 use function base64_encode;
@@ -13,30 +14,19 @@ use function file_get_contents;
 
 final class SendGrid extends Base
 {
-    private array $config;
-    private \SendGrid $sg;
-    private mixed $sender;
-    private mixed $name;
+    private SG $sg;
     private Mail $email;
 
+    /**
+     * @throws TypeException
+     */
     public function __construct()
-    {
-        $this->config = $this->getConfig();
-        $this->sg = new \SendGrid($this->config['key']);
-        $this->sender = $this->config['sender'];
-        $this->name = $this->config['name'];
-        $this->email = new Mail();
-    }
-
-    public function getConfig(): array
     {
         $configs = Setting::getClass('sendgrid');
 
-        return [
-            'key' => $configs['sendgrid_key'],
-            'sender' => $configs['sendgrid_sender'],
-            'name' => $configs['sendgrid_name'],
-        ];
+        $this->sg = new SG($configs['sendgrid_key']);
+        $this->email = new Mail();
+        $this->email->setFrom($configs['sendgrid_sender'], $configs['sendgrid_name']);
     }
 
     /**
@@ -44,7 +34,6 @@ final class SendGrid extends Base
      */
     public function send($to, $subject, $text, $files): void
     {
-        $this->email->setFrom($this->sender, $this->name);
         $this->email->setSubject($subject);
         $this->email->addTo($to);
         $this->email->addContent('text/html', $text);

+ 1 - 2
src/Services/Mail/Ses.php

@@ -30,14 +30,13 @@ final class Ses extends Base
     public function send($to, $subject, $text, $files): void
     {
         $ses = $this->ses;
-        $configs = Setting::getClass('aws_ses');
         $char_set = 'UTF-8';
 
         $ses->sendEmail([
             'Destination' => [
                 'ToAddresses' => [$to],
             ],
-            'Source' => $configs['aws_ses_sender'],
+            'Source' => Setting::obtain('aws_ses_sender'),
             'Message' => [
                 'Body' => [
                     'Html' => [

+ 1 - 2
src/Services/Mail/Smtp.php

@@ -13,7 +13,7 @@ final class Smtp extends Base
     private PHPMailer $mail;
 
     /**
-     * @throws \PHPMailer\PHPMailer\Exception
+     * @throws Exception
      */
     public function __construct()
     {
@@ -43,7 +43,6 @@ final class Smtp extends Base
     }
 
     /**
-     * @throws \PHPMailer\PHPMailer\Exception
      * @throws Exception
      */
     public function send($to, $subject, $text, $files): void