Merge branches 'ILIKE', 'brokensession', 'autologout', 'oop' and 'slave-table'
[infodrom/hallinta] / class / mail.class.php
diff --git a/class/mail.class.php b/class/mail.class.php
new file mode 100644 (file)
index 0000000..855bf18
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+class Mail {
+  protected $header = array();
+  protected $env_from = false;
+
+  public function set($name, $value)
+  {
+    $this->header[$name][] = $value;
+  }
+
+  public function env_from($value)
+  {
+    $this->env_from = $value;
+  }
+
+  public function send($body)
+  {
+    if (!array_key_exists('From', $this->header))
+      throw new Exception('No sender given.');
+    if (!array_key_exists('To', $this->header))
+      throw new Exception('No recipient given.');
+    if (!array_key_exists('Subject', $this->header))
+      throw new Exception('No subject given.');
+    if (empty($body))
+      throw new Exception('Mail body empty.');
+
+    if (!array_key_exists('Content-Type', $this->header))
+      $this->set('Content-Type', 'text/plain; charset=UTF-8');
+    if (!array_key_exists('Content-Disposition', $this->header))
+      $this->set('Content-Disposition', 'inline');
+    if (!array_key_exists('Content-Transfer-Encoding', $this->header))
+      $this->set('Content-Transfer-Encoding', '8bit');
+
+    $header = '';
+    foreach ($this->header as $name => $values) {
+      if ($name == 'To' || $name == 'Subject')
+       continue;
+      else
+       $header .= $name . ': ' . implode(', ', $values) . "\r\n";
+    }
+
+    $opts = '-t';
+    $opts .= strlen($this->env_from) ? ' -f '.$this->env_from : '';
+    $result = mail(implode(',',$this->header['To']), $this->header['Subject'][0], $body, $header, $opts);
+
+    return $result;
+  }
+}
+
+?>
\ No newline at end of file