[php] Good Looking PHP Code Example

Viewer

copydownloadembedprintName: Good Looking PHP Code Example
  1. <?php
  2.  
  3. namespace Email;
  4.  
  5. class Client
  6. {
  7.     public function send(string $emailAddress, Message $message): bool
  8.     {
  9.         if (!$this->validateParameters($emailAddress, $message)) {
  10.             return false;
  11.         }
  12.  
  13.         return $this->sendToMailServer([
  14.             'to' => $emailAddress,
  15.             'text' => $message->text(),
  16.         ]);
  17.     }
  18.  
  19.     private function validateParameters(string $emailAddress, Message $message): bool
  20.     {
  21.         $validateEmail = filter_var($emailAddress, FILTER_VALIDATE_EMAIL);
  22.         $validateMessage = strlen($message->text()) < 1000;
  23.         if (!$validateEmail || !$validateMessage) {
  24.             return false;
  25.         }
  26.  
  27.         return true;
  28.     }
  29.  
  30.     private function sendToMailServer(array $params): bool
  31.     {
  32.         $curl = curl_init();
  33.         curl_setopt($curl, CURLOPT_URL, 'https://mail.example.com');
  34.         curl_setopt($curl, CURLOPT_POST, true);
  35.         curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  36.  
  37.         $result = curl_exec($curl);
  38.         curl_close($curl);
  39.  
  40.         return $result;
  41.     }
  42. }

Editor

You can edit this paste and save as new:


File Description
  • Good Looking PHP Code Example
  • Paste Code
  • 23 May-2020
  • 1.08 Kb
You can Share it: