4 protected $header = array();
5 protected $env_from = false;
7 public function set($name, $value)
9 $this->header[$name][] = $value;
12 public function env_from($value)
14 $this->env_from = $value;
17 private function content_type($file)
19 if (($fh = popen('/usr/bin/file -i -b ' . escapeshellarg($file), 'r')) !== false) {
20 $type = fread($fh, 1024);
22 $parts = explode(';', $type);
23 return trim($parts[0]);
25 return mime_content_type($file);
28 public function attach($filename,$basename=false,$content_type=false)
32 public function send($body)
34 if (!array_key_exists('From', $this->header))
35 throw new Exception('No sender given.');
36 if (!array_key_exists('To', $this->header))
37 throw new Exception('No recipient given.');
38 if (!array_key_exists('Subject', $this->header))
39 throw new Exception('No subject given.');
41 throw new Exception('Mail body empty.');
43 if (!array_key_exists('Content-Type', $this->header))
44 $this->set('Content-Type', 'text/plain; charset=UTF-8');
45 if (!array_key_exists('Content-Disposition', $this->header))
46 $this->set('Content-Disposition', 'inline');
47 if (!array_key_exists('Content-Transfer-Encoding', $this->header))
48 $this->set('Content-Transfer-Encoding', '8bit');
51 foreach ($this->header as $name => $values) {
52 if ($name == 'To' || $name == 'Subject')
55 $header .= $name . ': ' . implode(', ', $values) . "\r\n";
59 $opts .= strlen($this->env_from) ? ' -f '.$this->env_from : '';
60 $result = mail(implode(',',$this->header['To']), $this->header['Subject'][0], $body, $header, $opts);