EmailSettings::getNotifyUser()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 1
1
<?php
2
3
class EmailSettings extends CiiSettingsModel
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected $SMTPHost = NULL;
6
7
    protected $SMTPPort = NULL;
8
9
    protected $SMTPUser = NULL;
10
11
    protected $SMTPPass = NULL;
12
13
    protected $notifyName = NULL;
14
15
    protected $notifyEmail = NULL;
16
17
    protected $useTLS = 0;
18
19
    protected $useSSL = 0;
20
21
    public function rules()
22
    {
23
        return array(
24
            array('notifyEmail', 'email'),
25
            array('useTLS, useSSL', 'boolean'),
26
            array('SMTPPort', 'numerical', 'integerOnly' => true, 'min' => 0),
27
            array('SMTPPass', 'password'),
28
            array('notifyName, SMTPPass, SMTPUser', 'length', 'max' => 255)
29
        );
30
    }
31
32
    public function attributeLabels()
33
    {
34
        return array(
35
            'SMTPHost'      => Yii::t('ciims.models.email', 'SMTP Hostname'),
36
            'SMTPPort'      => Yii::t('ciims.models.email', 'SMTP Port Number'),
37
            'SMTPUser'      => Yii::t('ciims.models.email', 'SMTP Username'),
38
            'SMTPPass'      => Yii::t('ciims.models.email', 'SMTP Password'),
39
            'useTLS'        => Yii::t('ciims.models.email', 'Use TLS Connection'),
40
            'useSSL'        => Yii::t('ciims.models.email', 'Use SSL Connection'),
41
            'notifyName'    => Yii::t('ciims.models.email', 'System From Name'),
42
            'notifyEmail'   => Yii::t('ciims.models.email', 'System Email Address')
43
        );
44
    }
45
46
    /**
47
     * Generic method for sending an email. Instead of having to call a bunch of code all over over the place
48
     * This method can be called which should be able to handle almost anything.
49
     *
50
     * By calling this method, the SMTP details will automatically be setup as well the notify email and user
51
     *
52
     * @param  Users   $user          The User we are sending the email to
53
     * @param  string  $subject       The email Subject
54
     * @param  string  $viewFile      The view file we want to render. Generally this should be in the form //email/<file>
55
     *                                And should correspond to a viewfile in /themes/<theme>/views/email/<file>
56
     * @param  array   $content       The content to pass to renderPartial()
57
     * @param  boolean $return        Whether the output should be returned. The default is TRUE since this output will be passed to MsgHTML
58
     * @param  boolean $processOutput Whether the output should be processed. The default is TRUE since this output will be passed to MsgHTML
59
     * @return boolean                Whether or not the email sent sucessfully
60
     */
61
    public function send($user, $subject = "", $viewFile, $content = array(), $return = true, $processOutput = true, $debug=false)
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
62
    {
63
        $mail = new PHPMailer($debug);
64
        $mail->IsSMTP();
65
        $mail->SMTPAuth = false;
66
67
        $notifyUser = $this->getNotifyUser(isset($content['origin_from']) ? $content['origin_from'] : array());
68
69
        if (empty($this->SMTPHost))
70
            $mail->Host = $this->SMTPHost;
71
72
        if (empty($this->SMTPPort))
73
            $mail->Port = $this->SMTPPort;
74
75
        if (empty($this->SMTPUser))
76
        {
77
            $mail->Username = $this->SMTPUser;
78
            $mail->SMTPAuth = true;
79
        }
80
81
        if ($this->useTLS == 1)
82
            $mail->SMTPSecure = 'tls';
83
        else if ($this->useSSL == 1)
84
            $mail->SMTPSecure = 'ssl';
85
86
        if (empty($this->SMTPPass))
87
        {
88
            $mail->Password   = Cii::decrypt($this->SMTPPass);
89
            $mail->SMTPAuth = true;
90
        } 
91
92
        $mail->SetFrom($notifyUser->email, $notifyUser->username);
93
        $mail->Subject = $subject;
94
        $mail->MsgHTML($this->renderFile(Yii::getPathOfAlias($viewFile).'.php', $content, $return, $processOutput));
95
        $mail->AddAddress($user->email, $user->username);
96
97
        try
98
        {
99
            return $mail->Send();
100
        }
101
        catch (phpmailerException $e)
102
        {
103
            Yii::log($e->getMessage(), 'info', 'ciims.models.EmailSettings');
104
            return false;
105
        }
106
        catch (Exception $e)
107
        {
108
            Yii::log($e->getMessage(), 'info', 'ciims.models.EmailSettings');
109
            return false;
110
        }
111
    }
112
113
    /**
114
     * Generates a user object
115
     * @param array $userData
116
     */
117
    private function getNotifyUser($userData=array())
118
    {
119
        $user = new stdClass;
120
        if (!empty($userData))
121
        {
122
            $user->email    = $userData['email'];
123
            $user->username = $userData['name'];
124
        }
125
        else
126
        {
127
            if ($this->notifyName === NULL || $this->notifyEmail === NULL)
128
                $user = Users::model()->findByPk(1);
129
            else
130
            {
131
                $user->email    = $this->notifyEmail;
132
                $user->username = $this->notifyName;
133
            }
134
        }
135
136
        return $user;
137
    }
138
139
    /**
140
     * Renders a view file.
141
     *
142
     * @param string $viewFile view file path
143
     * @param array $data data to be extracted and made available to the view
144
     * @param boolean $return whether the rendering result should be returned instead of being echoed
145
     * @return string the rendering result. Null if the rendering result is not required.
146
     * @throws CException if the view file does not exist
147
     */
148
    private function renderFile($viewFile,$data=null,$return=false)
149
    {
150
        if(($renderer=Yii::app()->getViewRenderer())!==null && $renderer->fileExtension==='.'.CFileHelper::getExtension($viewFile))
151
            $content=$renderer->renderFile($this,$viewFile,$data,$return);
152
        else
153
            $content=$this->renderInternal($viewFile,$data,$return);
154
        
155
        return $content;
156
    }
157
158
    /**
159
     * Renders a view file.
160
     * This method includes the view file as a PHP script
161
     * and captures the display result if required.
162
     * @param string $_viewFile_ view file
163
     * @param array $_data_ data to be extracted and made available to the view file
164
     * @param boolean $_return_ whether the rendering result should be returned as a string
165
     * @return string the rendering result. Null if the rendering result is not required.
166
     */
167
    private function renderInternal($_viewFile_,$_data_=null,$_return_=false)
168
    {
169
        // we use special variable names here to avoid conflict when extracting data
170
        if(is_array($_data_))
171
            extract($_data_,EXTR_PREFIX_SAME,'data');
172
        else
173
            $data=$_data_;
174
        if($_return_)
175
        {
176
            ob_start();
177
            ob_implicit_flush(false);
178
            require($_viewFile_);
179
            return ob_get_clean();
180
        }
181
        else
182
            require($_viewFile_);
183
    }
184
}
185