Advertisement
AlexWebDevelop

MailQueue PHP class

Mar 20th, 2019
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. class MailQueue
  4. {
  5.     /* PDO object for database operations */
  6.     private $pdo;
  7.    
  8.    
  9.     public function __construct(PDO &$pdo)
  10.     {
  11.         /* Set the PDO object */
  12.         $this->pdo = $pdo;
  13.     }
  14.    
  15.    
  16.     /* Public function to add a new message to the queue. Returns the message ID */
  17.     public function add_message(array $message): int
  18.     {
  19.         /* Check if the message is valid, otherwise throws an exception. */
  20.         if ($this->validate_message($message))
  21.         {
  22.             /* Insert the base message to the queue */
  23.             $queue_id = $this->insert_message($message);
  24.            
  25.             /* Insert the CC(N) addresses */
  26.             $this->insert_cc($queue_id, $message['cc']);
  27.            
  28.             /* Insert the file attachments */
  29.             $this->insert_attachments($queue_id, $message['attachments']);
  30.         }
  31.         else
  32.         {
  33.             /* Invalid message */
  34.             throw new Exception('Invalid message.');
  35.         }
  36.        
  37.         return $queue_id;
  38.     }
  39.    
  40.    
  41.     private function insert_message(array $message): int
  42.     {
  43.         /* New message ID */
  44.         $queue_id = NULL;
  45.        
  46.         /* Insert query */
  47.         $query = 'INSERT INTO mailqueue.queue (queue_app_id, queue_from, queue_recipient, queue_subject, queue_text_body, queue_html_body, queue_priority, queue_added, queue_schedule) VALUES (:app_id, :from, :recipient, :subject, :text_body, :html_body, :priority, NOW(), :schedule)';
  48.        
  49.         /* Query values */
  50.         $params = array(
  51.        
  52.         ':app_id' => $message[app_id],
  53.         ':from' => $message['from'],
  54.         ':recipient' => $message['recipient'],
  55.         ':subject' => $message['subject'],
  56.         ':text_body' => $message['text_body'],
  57.         ':html_body' => $message['html_body'],
  58.         ':priority' => $message['priority'],
  59.         ':schedule' => $message['schedule']);
  60.        
  61.         /* Check for PDO Exceptions (query errors) */
  62.         try
  63.         {
  64.             /* Execute the query */
  65.             $res = $this->pdo->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  66.             $res->execute($params);
  67.            
  68.             /* Retrieve the new message id */
  69.             $queue_id = $this->pdo->lastInsertId();
  70.         }
  71.         catch (PDOException $e)
  72.         {
  73.             /* Throw a standard exception */
  74.             throw new Exception('Database error: ' . $e->getMessage());
  75.         }
  76.        
  77.         return $queue_id;
  78.     }
  79.    
  80.    
  81.     private function validate_message(array $message): bool
  82.     {
  83.         /* TODO */
  84.         return TRUE;
  85.     }
  86.    
  87.    
  88.     private function insert_cc(int $queue_id, array $message)
  89.     {
  90.         /* TODO */
  91.         return TRUE;
  92.     }
  93.    
  94.    
  95.     private function insert_attachments(int $queue_id, array $message)
  96.     {
  97.         /* TODO */
  98.         return TRUE;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement