Add mail class
authorJoey Schulze <joey@infodrom.org>
Fri, 4 Apr 2014 18:20:17 +0000 (18:20 +0000)
committerJoey Schulze <joey@infodrom.org>
Fri, 4 Apr 2014 18:20:17 +0000 (18:20 +0000)
class/mail.class.php [new file with mode: 0644]

diff --git a/class/mail.class.php b/class/mail.class.php
new file mode 100644 (file)
index 0000000..5cfb968
--- /dev/null
@@ -0,0 +1,66 @@
+<?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;
+  }
+
+  private function content_type($file)
+  {
+    if (($fh = popen('/usr/bin/file -i -b ' . escapeshellarg($file), 'r')) !== false) {
+      $type = fread($fh, 1024);
+      fclose($fh);
+      $parts = explode(';', $type);
+      return trim($parts[0]);
+    } else
+      return mime_content_type($file);
+  }
+
+  public function attach($filename,$basename=false,$content_type=false)
+  {
+  }
+
+  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