Project

General

Profile

wb-2_10_x / branches / main / framework / class.wbmailer.php @ 10

1
<?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: class.wbmailer.php 2 2017-07-02 15:14:29Z Manuela $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/framework/class.wbmailer.php $
15
 * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
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 extends PHPMailer
33
{
34
    // new websitebaker mailer class (subset of PHPMailer class)
35
    // setting default values
36

    
37

    
38
    function __construct($exceptions = false) {//
39

    
40
        parent::__construct($exceptions);//
41

    
42
        $database = $GLOBALS['database'];
43
        $errorMessage = [];
44
        $db_server_email = ''; // required
45
        // set mailer defaults (PHP mail function)
46
        $db_wbmailer_routine = "phpmail";
47
        $db_wbmailer_smtp_host = ""; // required if smtp
48
        $db_wbmailer_smtp_port = 25; // required
49
        $db_wbmailer_smtp_secure = ''; // required if smtp
50
        $db_wbmailer_default_sendername = 'WB Mailer'; // required
51
// && mb_strlen($db_wbmailer_smtp_host) > 5
52
        // get mailer settings from database
53
        $sql = 'SELECT * FROM `' .TABLE_PREFIX. 'settings` '
54
              . 'WHERE `name` LIKE (\'wbmailer\_%\') '
55
              . 'OR `name`=\'server_email\'';
56
        $oRes = $database->query($sql);
57
        while($aSettings = $oRes->fetchRow( MYSQLI_ASSOC )) {
58
            ${'db_'.$aSettings['name']} = $aSettings['value'];
59
            switch ($aSettings['name']):
60
                case 'server_email':
61
                    if (filter_var($aSettings['value'], FILTER_VALIDATE_EMAIL) === false){
62
                      $this->setError('Server E-Mail is empty or not valide');
63
                    };
64
            endswitch;
65
// TODO sanitize smtp settings
66
           if ($db_wbmailer_routine == "smtp"){
67
              switch ($aSettings['name']):
68
                  case 'wbmailer_smtp_host':
69
                      $db_wbmailer_smtp_host = $aSettings['value'];
70
                      break;
71
                  case 'wbmailer_smtp_port':
72
                      $db_wbmailer_smtp_port = (int)$aSettings['value'];
73
                      break;
74
                  case 'wbmailer_smtp_secure':
75
                      $db_wbmailer_smtp_secure = $aSettings['value'];
76
                      break;
77
                  case 'wbmailer_smtp_username':
78
                      break;
79
                  case 'wbmailer_smtp_password':
80
                      break;
81
                  case 'wbmailer_default_sendername':
82
                      break;
83
                  default:
84
                      break;
85
              endswitch;
86
           }
87
        }
88

    
89
/**
90
     * `echo` Output plain-text as-is, appropriate for CLI
91
     * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output
92
     * `error_log` Output to error log as configured in php.ini
93
     *
94
     * Alternatively, you can provide a callable expecting two params: a message string and the debug level:
95
     * <code>
96
     * $this->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";};
97
     * </code>
98
 */
99

    
100
        $this->set('SMTPDebug', ((defined('DEBUG') && DEBUG)?2:0));    // Enable verbose debug output
101
        $this->set('Debugoutput', 'error_log');
102

    
103
        // set method to send out emails
104
        if ($db_wbmailer_routine == "smtp") {
105
            // use SMTP for all outgoing mails send by Website Baker
106
            $this->isSMTP();                                               // telling the class to use SMTP
107
            $this->set('SMTPAuth', false);                                 // enable SMTP authentication
108
            $this->set('Host', $db_wbmailer_smtp_host);                    // Set the hostname of the mail server
109
            $this->set('Port', intval($db_wbmailer_smtp_port));            // Set the SMTP port number - likely to be 25, 465 or 587
110
            $this->set('SMTPSecure', strtolower($db_wbmailer_smtp_secure));// Set the encryption system to use - ssl (deprecated) or tls
111
            $this->set('SMTPKeepAlive', false);                            // SMTP connection will be close after each email sent
112
            // check if SMTP authentification is required
113
            if ($db_wbmailer_smtp_auth  && (mb_strlen($db_wbmailer_smtp_username) > 1) && (mb_strlen($db_wbmailer_smtp_password) > 1) ) {
114
                // use SMTP authentification
115
                $this->set('SMTPAuth', true);                                                 // enable SMTP authentication
116
                $this->set('Username',   $db_wbmailer_smtp_username);                         // set SMTP username
117
                $this->set('Password',   $db_wbmailer_smtp_password);                         // set SMTP password
118
            }
119
        } else if ($db_wbmailer_routine == "phpmail") {
120
            // use PHP mail() function for outgoing mails send by Website Baker
121
            $this->IsMail();
122
        } else {
123
            $this->isSendmail();   // telling the class to use SendMail transport
124
        }
125

    
126
        // set language file for PHPMailer error messages
127
        if(defined("LANGUAGE")) {
128
            $this->SetLanguage(strtolower(LANGUAGE),"language");    // english default (also used if file is missing)
129
        }
130

    
131
        // set default charset
132
        if(defined('DEFAULT_CHARSET')) {
133
            $this->set('CharSet', DEFAULT_CHARSET);
134
        } else {
135
            $this->set('CharSet', 'utf-8');
136
        }
137

    
138
        // set default sender name
139
        if($this->FromName == 'Root User') {
140
            if(isset($_SESSION['DISPLAY_NAME'])) {
141
                $this->set('FromName', $_SESSION['DISPLAY_NAME']);                  // FROM NAME: display name of user logged in
142
            } else {
143
                $this->set('FromName', $db_wbmailer_default_sendername);            // FROM NAME: set default name
144
            }
145
        }
146

    
147
        /*
148
            some mail provider (lets say mail.com) reject mails send out by foreign mail
149
            relays but using the providers domain in the from mail address (e.g. myname@mail.com)
150
        $this->setFrom($db_server_email);                       // FROM MAIL: (server mail)
151
        */
152

    
153
        // set default mail formats
154
        $this->IsHTML();                                        // Sets message type to HTML or plain.
155
        $this->set('WordWrap', 80);
156
        $this->set('Timeout', 30);
157
    }
158

    
159
    /**
160
     * Send messages using $Sendmail.
161
     * @return void
162
     * @description  overrides isSendmail() in parent
163
     */
164
    public function isSendmail()
165
    {
166
        $ini_sendmail_path = ini_get('sendmail_path');
167
        if (!preg_match('/sendmail$/i', $ini_sendmail_path)) {
168
            if ($this->exceptions) {
169
                throw new phpmailerException('no sendmail available');
170
            }
171
        } else {
172
            $this->Sendmail = $ini_sendmail_path;
173
            $this->Mailer = 'sendmail';
174
        }
175
    }
176

    
177
}