php Id Mail class php 372 2007-05-26 08 00 51Z darkk require_once arra

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
<?php
// $Id: Mail.class.php 372 2007-05-26 08:00:51Z darkk $
require_once('array.php');
require_once('Debug.class.php');
class Mail {
var $head = array('From' => null);
var $body;
function Mail() {
if (isset($_SERVER["REMOTE_ADDR"]))
$this->head['Received'] =
"from [{$_SERVER["REMOTE_ADDR"]}] by komus-gifts via HTTP; " .
date("r");
if (isset($_SERVER["HTTP_USER_AGENT"]))
$this->head['X-Mailer'] = $_SERVER["HTTP_USER_AGENT"];
$this->head['Content-type'] =
'text/plain; charset=utf-8';
}
function send() {
$lines = array();
foreach ($this->head as $key => $val)
$lines[] = "{$key}: {$val}"; // TODO: wrap long lines for RFC
$head = join("\r\n", $lines);
$fd = array();
$metafd = array(
0 => array("pipe", "r"), // stdin
1 => array("file", "/dev/null", "w"), // stdout
2 => array("file", "/dev/null", "w") // stderr
);
$proc = proc_open("sendmail -t", $metafd, $fd);
if (is_resource($proc)) {
fwrite($fd[0], "$head\r\n\r\n{$this->body}");
// if sendmail is insane, there may be deadlock here
fclose($fd[0]);
$retval = proc_close($proc);
return ($retval == 0);
}
else
return false;
}
function subject($subj) {
$this->head['Subject'] = $this->_header_encode($subj);
}
function from($name, $email) {
$this->_appendFromLikeHeader('From', $name, $email);
}
function to($name, $email) {
$this->_appendFromLikeHeader('To', $name, $email);
}
function bcc($name, $email) {
$this->_appendFromLikeHeader('BCC', $name, $email);
}
function body($body) {
$this->body = $body;
}
function _appendFromLikeHeader($key, $name, $email) {
if ($name)
$value = $this->_header_encode($name) . " <{$email}>";
else
$value = $email;
if (isset($this->head[$key]))
$this->head[$key] .= ", " . $value;
else
$this->head[$key] = $value;
}
function _header_encode($in_str, $charset = 'utf-8') {
// XXX: taken from php.net
$out_str = $in_str;
if ($out_str && $charset) {
$end = "?=";
$start = "=?" . $charset . "?B?";
$spacer = $end . "\r\n " . $start;
// determine length of encoded text within chunks
// and ensure length is even
$length = 75 - strlen($start) - strlen($end);
$length = $length - ($length % 4);
// encode the string and split it into chunks
// with spacers after each chunk
$out_str = base64_encode($out_str);
$out_str = chunk_split($out_str, $length, $spacer);
// remove trailing spacer and add start and end delimiters
$spacer = preg_quote($spacer);
$out_str = preg_replace("/" . $spacer . "$/", "", $out_str);
$out_str = $start . $out_str . $end;
}
return $out_str;
}
}
// vim:set tabstop=4 softtabstop=4 shiftwidth=4:
// vim:set foldmethod=marker foldlevel=32 foldmarker={,}:
?>