From d69c19b7a138659962d3c20c2487f4f13da997c5 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 24 May 2011 15:01:09 -0700 Subject: Working on email validation --- htdocs/src/Email.inc.php | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 htdocs/src/Email.inc.php (limited to 'htdocs/src/Email.inc.php') diff --git a/htdocs/src/Email.inc.php b/htdocs/src/Email.inc.php new file mode 100644 index 0000000..b828780 --- /dev/null +++ b/htdocs/src/Email.inc.php @@ -0,0 +1,72 @@ + + * + */ + +class Email { + private $subject; + private $to; + private $from; + private $fromname; + private $message; + private $headers; + + public function __construct($to) { + $this->to = $to; + $this->from = $GLOBALS['CONFIG']['email_from']; + $this->message = ""; + $this->headers = array(); + } + + public function setFrom($from) { + $this->from = $from; + + if (strstr($from, "<")) + $this->fromname = preg_replace("/([^<>]+) <([^<>]+)>/", "$1", $from); + } + + public function setSubject($subject) { + $this->subject = $subject; + } + + public function addHeader($header, $value) { + $this->headers[] = "$header: $value"; + } + + public function appendMessage($message) { + $this->message .= $message; + } + + public function send($logprefix="") { + // Headers + if ($this->fromname) { + $headers = "From: ". $this->fromname ." <". $this->from .">\n"; + } else { + $headers = "From: ". $this->from ."\n"; + } + $headers .= "Reply-To: ". $this->from ."\n"; + $headers .= "Date: ". date("r") ."\n"; + $headers .= join("\n", $this->headers); + + if ($GLOBALS['CONFIG']['production']) { + $ret = mail($this->to, $this->subject, $this->message, $headers); + + } else { + // If we're not in production, save to file instead of emailing. + $fh = fopen($GLOBALS['CONFIG']['root'].'/emails.log', 'a'); + fwrite($fh, sprintf("To: %s\n%s\nSubject: %s\n\n%s\n\n", + $this->to, $headers, $this->subject, $this->message)); + fclose($fh); + } + + // TODO: Add logger + //$GLOBALS['logger']->log_email($ret, $this->to, $this->subject, $logprefix); + } +} + +?> -- cgit v1.2.3