Project

General

Profile

1 2 Manuela
<?php
2
/**
3
 *
4
 * @category        framework
5
 * @package         frontend
6
 * @subpackage      wbmailer
7
 * @author          Ryan Djurovich, WebsiteBaker Project
8
 * @copyright       WebsiteBaker Org. e.V.
9
 * @link            http://websitebaker.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.3
12
 * @requirements    PHP 5.3.6 and higher
13
 * @version         $Id$
14
 * @filesource      $HeadURL$
15
 * @lastmodified    $Date$
16
 * @examples        http://phpmailer.worxware.com/index.php?pg=examples
17
 *
18
 */
19
/* -------------------------------------------------------- */
20
// Must include code to stop this file being accessed directly
21
if(!defined('WB_PATH')) {
22
    require_once(dirname(__FILE__).'/globalExceptionHandler.php');
23
    throw new IllegalFileException();
24
}
25
/* -------------------------------------------------------- */
26
//SMTP needs accurate times, and the PHP time zone MUST be set
27
//This should be done in your php.ini, but this is how to do it if you don't have access to that
28
date_default_timezone_set('Etc/UTC');
29
30
// Include PHPMailer autoloader in initialize
31
32
class wbmailer
33
{
34
    // new websitebaker mailer class (subset of PHPMailer class)
35
    // setting default values
36
    protected $aErrorMessage = [];
37
    protected $cfg = [];
38
39
    private $oMailer = null;
40
41
    public function __construct(PHPMailer $oMailer)
42
    {
43
        $this->execute();
44
    }
45
/* ---------------------------------------------------------------------- */
46
47
    public function execute($exceptions = false) {//
48
        $database = $GLOBALS['database'];
49
        $db_server_email = ''; // required
50
        // set mailer defaults (PHP mail function)
51
        $db_wbmailer_routine = "phpmail";
52
        $db_wbmailer_smtp_host = ""; // required if smtp
53
        $db_wbmailer_smtp_port = 25; // required
54
        $db_wbmailer_smtp_secure = ''; // required if smtp
55
        $db_wbmailer_default_sendername = 'WB Mailer'; // required
56
57
        // get mailer settings from database
58
        $sql = 'SELECT * FROM `' .TABLE_PREFIX. 'settings` '
59
              . 'WHERE `name` LIKE (\'wbmailer\_%\') '
60
              . 'OR `name`=\'server_email\'';
61
        $oRes = $database->query($sql);
62
        while($aSettings = $oRes->fetchRow( MYSQLI_ASSOC )) {
63
            switch ($aSettings['name']):
64
                case 'wbmailer_routine':
65
                case 'server_email':
66
                    ${'db_'.$aSettings['name']} = $aSettings['value'];
67
                    if ($db_wbmailer_routine == "smtp"){
68
                        switch ($aSettings['name']):
69
                            case 'wbmailer_smtp_host':
70
                            case 'wbmailer_smtp_port':
71
                            case 'wbmailer_smtp_secure':
72
                            case 'wbmailer_smtp_username':
73
                            case 'wbmailer_smtp_password':
74
                            case 'wbmailer_default_sendername':
75
                            case 'wbmailer_smtp_auth':
76
                                ${'db_'.$aSettings['name']} =  ( ($aSettings['value']=='true') || (intval($aSettings['value'])==1) ?true:false );
77
                                break;
78
                        endswitch;
79
                    }
80
                    break;
81
                default:
82
                break;
83
            endswitch;
84
        }
85
86
/**
87
     * `echo` Output plain-text as-is, appropriate for CLI
88
     * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output
89
     * `error_log` Output to error log as configured in php.ini
90
     *
91
     * Alternatively, you can provide a callable expecting two params: a message string and the debug level:
92
     * <code>
93
     * $this->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
94
     * </code>
95
     * oMailer->
96
 */
97
98
        $this->set('SMTPDebug', ((defined('DEBUG') && DEBUG)?2:0));    // Enable verbose debug output
99
        $this->set('Debugoutput', 'error_log');
100
101
        // set method to send out emails
102
        if ($db_wbmailer_routine == "smtp" && mb_strlen($db_wbmailer_smtp_host) > 5 ) {
103
            // use SMTP for all outgoing mails send by Website Baker
104
            $this->oMailer->isSMTP();                                               // telling the class to use SMTP
105
            $this->oMailer->set('SMTPAuth', false);                                 // enable SMTP authentication
106
            $this->oMailer->set('Host', $db_wbmailer_smtp_host);                    // Set the hostname of the mail server
107
            $this->oMailer->set('Port', intval($db_wbmailer_smtp_port));            // Set the SMTP port number - likely to be 25, 465 or 587
108
            $this->oMailer->set('SMTPSecure', strtolower($db_wbmailer_smtp_secure));// Set the encryption system to use - ssl (deprecated) or tls
109
            $this->oMailer->set('SMTPKeepAlive', false);                            // SMTP connection will be close after each email sent
110
            // check if SMTP authentification is required
111
            if ($db_wbmailer_smtp_auth  && (mb_strlen($db_wbmailer_smtp_username) > 1) && (mb_strlen($db_wbmailer_smtp_password) > 1) ) {
112
                // use SMTP authentification
113
                $this->oMailer->set('SMTPAuth', true);                                                 // enable SMTP authentication
114
                $this->oMailer->set('Username',   $db_wbmailer_smtp_username);                         // set SMTP username
115
                $this->oMailer->set('Password',   $db_wbmailer_smtp_password);                         // set SMTP password
116
            }
117
        } else if ($db_wbmailer_routine == "phpmail") {
118
            // use PHP mail() function for outgoing mails send by Website Baker
119
            $this->oMailer->IsMail();
120
        } else {
121
            $this->oMailer->isSendmail();   // telling the class to use SendMail transport
122
        }
123
124
        // set language file for PHPMailer error messages
125
        if(defined("LANGUAGE")) {
126
            $this->SetLanguage(strtolower(LANGUAGE),"language");    // english default (also used if file is missing)
127
        }
128
129
        // set default charset
130
        if(defined('DEFAULT_CHARSET')) {
131
            $this->oMailer->set('CharSet', DEFAULT_CHARSET);
132
        } else {
133
            $this->set('CharSet', 'utf-8');
134
        }
135
136
        // set default sender name
137
        if($this->oMailer->FromName == 'Root User') {
138
            if(isset($_SESSION['DISPLAY_NAME'])) {
139
                $this->oMailer->set('FromName', $_SESSION['DISPLAY_NAME']);                  // FROM NAME: display name of user logged in
140
            } else {
141
                $this->oMailer->set('FromName', $db_wbmailer_default_sendername);            // FROM NAME: set default name
142
            }
143
        }
144
145
        /*
146
            some mail provider (lets say mail.com) reject mails send out by foreign mail
147
            relays but using the providers domain in the from mail address (e.g. myname@mail.com)
148
        $this->setFrom($db_server_email);                       // FROM MAIL: (server mail)
149
        */
150
151
        // set default mail formats
152
        $this->oMailer->IsHTML();                                        // Sets message type to HTML or plain.
153
        $this->oMailer->set('WordWrap', 80);
154
        $this->oMailer->set('Timeout', 30);
155
    }
156
157
/* ---------------------------------------------------------------------- */
158
159
    public function __isset($name)
160
    {
161
        return isset($this->aConfig[$name]);
162
    }
163
164
     public function __set($name, $value)
165
     {
166
//         throw new Exception('Tried to set a readonly or nonexisting property ['.$name.']!!');
167
         return $this->aConfig[$name] = $value;
168
     }
169
170
    public function __get($name)
171
    {
172
        $retval = null;
173
        if (!$this->__isset($name)) {
174
            throw new Exception('Tried to get nonexisting property ['.$name.']');
175
        }
176
            $retval = $this->aConfig[$name];
177
        return $retval;
178
    }
179
/* ---------------------------------------------------------------------- */
180
181
    /**
182
     * Send messages using $Sendmail.
183
     * @return void
184
     * @description  overrides isSendmail() in parent
185
     */
186
    public function isSendmail()
187
    {
188
        $ini_sendmail_path = ini_get('sendmail_path');
189
        if (!preg_match('/sendmail$/i', $ini_sendmail_path)) {
190
            if ($this->exceptions) {
191
                throw new phpmailerException('no sendmail available');
192
            }
193
        } else {
194
            $this->oMailer->Sendmail = $ini_sendmail_path;
195
            $this->oMailer->Mailer = 'sendmail';
196
        }
197
    }
198
199
} // end of class
200