source: branches/1.2/libs/EmailFunctions.php @ 1354

Revision 1354, 4.8 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.2] Custom iconv function, plus more SMTP email fixes

Line 
1<?php
2/**
3 * Functions for sending emails
4 *
5 * PHP version 5
6 *
7 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
18 *
19 * @category  Content Management System
20 * @package   HotaruCMS
21 * @author    Nick Ramsay <admin@hotarucms.org>
22 * @copyright Copyright (c) 2009, Hotaru CMS
23 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @link      http://www.hotarucms.org/
25 */
26   
27class EmailFunctions
28{
29    protected $to           = '';
30    protected $from         = '';
31    protected $subject      = 'No Subject';
32    protected $body         = '';
33    protected $headers      = '';
34    protected $type         = 'email';
35    private $smtp   = NULL;
36   
37    /**
38     * Access modifier to set protected properties
39     */
40    public function __set($var, $val)
41    {
42        $this->$var = $val;
43    }
44   
45   
46    /**
47     * Access modifier to get protected properties
48     * The & is necessary (http://bugs.php.net/bug.php?id=39449)
49     */
50    public function &__get($var)
51    {
52        return $this->$var;
53    }
54
55   
56    /**
57     * Send emails - Note: properties must be set before calling this function
58     */
59    public function doEmail()
60    {       
61        if (!$this->body) { return false; }
62       
63        if (!$this->to) { $this->to = SITE_NAME . ' <' . SITE_EMAIL . '>'; }
64        if (!$this->from) { $this->from = SITE_NAME . ' <' . SITE_EMAIL . '>'; }
65       
66        if (SMTP == 'true') {
67            // note: this overwrites headers passed to this function:
68            if (is_array($this->to)) { $to = $this->to['To']; } else { $to = $this->to; }
69            $this->headers = array ('From' => $this->from, 'To' => $to, 'Subject' => $this->subject);
70        } else {
71            // if not using SMTP and no headers passed to this function, use default
72            if (!$this->headers) {
73                $this->headers = "From: " . $this->from . "\r\nReply-To: " . SITE_EMAIL . "\r\nX-Priority: 3\r\n";
74            }
75        }
76       
77        switch ($this->type)
78        {
79            case 'log':
80                require_once(LIBS . 'Debug.php');
81                $debug = new Debug();
82                $debug->openLog('email_log', 'a+');
83                $content = $this->headers . "\r\n" . $this->to . "\r\n" . $this->subject . "\r\n" . $this->body . "\r\n\r\n";
84                $content .= "**************************************************************\r\n\r\n";
85                $debug->writeLog('email_log', $content);
86                $debug->closeLog('email_log');
87                break;
88            case 'screen':
89                echo "Headers: " . $this->headers . "<br /><br />";
90                echo "To: " . $this->to . "<br /><br />";
91                echo "Subject: " . $this->subject . "<br /><br />";
92                $this->body = nl2br($this->body);
93                echo "Body: " . $this->body . "<br /><br />";
94                break;
95            case 'return':
96                return array('headers' => $this->headers, 'to' => $this->to, 'subject' => $this->subject, 'body' => $this->body, 'type' => $this->type);
97                break;
98            default:
99                if (SMTP == 'true') {
100                    $this->doSmtpEmail();
101                } else {
102                    $return_path = "-f " . SITE_EMAIL;
103                    mail($this->to, $this->subject, $this->body, $this->headers, $return_path);
104                }
105        }
106    }
107   
108   
109    /**
110     * Send email using SMTP authentication and SSL Encryption
111     */
112    public function doSmtpEmail()
113    {
114        //  Only create a new smtp object if we don't already have one:
115        if (!is_object($this->smtp))
116        {
117            $smtp_array = array (
118                'host' => SMTP_HOST,
119                'port' => SMTP_PORT,
120                'auth' => true,
121                'username' => SMTP_USERNAME,
122                'password' => SMTP_PASSWORD
123            );
124
125            require_once "Mail.php";
126            $this->smtp = Mail::factory('smtp', $smtp_array);
127        }
128
129        $mail = $this->smtp->send($this->to, $this->headers, $this->body);
130       
131        if (PEAR::isError($mail)) {
132            echo("<p>" . $mail->getMessage() . "</p>");
133            exit;
134        }
135    }
136}
137
138?>
Note: See TracBrowser for help on using the repository browser.