summaryrefslogtreecommitdiff
path: root/htdocs/src/Email.inc.php
blob: b8287808d2d7dd8c70deeb18e7fc2864c02348d2 (plain)
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
<?php

/* Foursquare Community Site
 * 
 * Copyright (C) 2011 Foursquare Church.
 * 
 * Developers: Jesse Morgan <jmorgan@foursquarestaff.com>
 *
 */

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);
    }
}

?>