Conditions | 28 |
Paths | 4064 |
Total Lines | 111 |
Code Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public function smtpConnect($options = array()) |
||
86 | { |
||
87 | if (is_null($this->smtp)) { |
||
88 | $this->smtp = $this->getSMTPInstance(); |
||
89 | } |
||
90 | |||
91 | if (is_null($this->oauth)) { |
||
92 | $this->oauth = $this->getOAUTHInstance(); |
||
93 | } |
||
94 | |||
95 | // Already connected? |
||
96 | if ($this->smtp->connected()) { |
||
97 | return true; |
||
98 | } |
||
99 | |||
100 | $this->smtp->setTimeout($this->Timeout); |
||
101 | $this->smtp->setDebugLevel($this->SMTPDebug); |
||
102 | $this->smtp->setDebugOutput($this->Debugoutput); |
||
103 | $this->smtp->setVerp($this->do_verp); |
||
104 | $hosts = explode(';', $this->Host); |
||
105 | $lastexception = null; |
||
106 | |||
107 | foreach ($hosts as $hostentry) { |
||
108 | $hostinfo = array(); |
||
109 | if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) { |
||
110 | // Not a valid host entry |
||
111 | continue; |
||
112 | } |
||
113 | // $hostinfo[2]: optional ssl or tls prefix |
||
114 | // $hostinfo[3]: the hostname |
||
115 | // $hostinfo[4]: optional port number |
||
116 | // The host string prefix can temporarily override the current setting for SMTPSecure |
||
117 | // If it's not specified, the default value is used |
||
118 | $prefix = ''; |
||
119 | $secure = $this->SMTPSecure; |
||
120 | $tls = ($this->SMTPSecure == 'tls'); |
||
121 | if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) { |
||
122 | $prefix = 'ssl://'; |
||
123 | $tls = false; // Can't have SSL and TLS at the same time |
||
124 | $secure = 'ssl'; |
||
125 | } elseif ($hostinfo[2] == 'tls') { |
||
126 | $tls = true; |
||
127 | // tls doesn't use a prefix |
||
128 | $secure = 'tls'; |
||
129 | } |
||
130 | //Do we need the OpenSSL extension? |
||
131 | $sslext = defined('OPENSSL_ALGO_SHA1'); |
||
132 | if ('tls' === $secure or 'ssl' === $secure) { |
||
133 | //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled |
||
134 | if (!$sslext) { |
||
135 | throw new phpmailerException($this->lang('extension_missing').'openssl', self::STOP_CRITICAL); |
||
136 | } |
||
137 | } |
||
138 | $host = $hostinfo[3]; |
||
139 | $port = $this->Port; |
||
140 | $tport = (integer)$hostinfo[4]; |
||
141 | if ($tport > 0 and $tport < 65536) { |
||
142 | $port = $tport; |
||
143 | } |
||
144 | if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { |
||
145 | try { |
||
146 | if ($this->Helo) { |
||
147 | $hello = $this->Helo; |
||
148 | } else { |
||
149 | $hello = $this->serverHostname(); |
||
150 | } |
||
151 | $this->smtp->hello($hello); |
||
152 | //Automatically enable TLS encryption if: |
||
153 | // * it's not disabled |
||
154 | // * we have openssl extension |
||
155 | // * we are not already using SSL |
||
156 | // * the server offers STARTTLS |
||
157 | if ($this->SMTPAutoTLS and $sslext and $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) { |
||
158 | $tls = true; |
||
159 | } |
||
160 | if ($tls) { |
||
161 | if (!$this->smtp->startTLS()) { |
||
162 | throw new phpmailerException($this->lang('connect_host')); |
||
163 | } |
||
164 | // We must resend HELO after tls negotiation |
||
165 | $this->smtp->hello($hello); |
||
166 | } |
||
167 | if ($this->SMTPAuth) { |
||
168 | if (!$this->smtp->authenticate( |
||
169 | $this->Username, |
||
170 | $this->Password, |
||
171 | $this->AuthType, |
||
172 | $this->Realm, |
||
173 | $this->Workstation, |
||
174 | $this->oauth |
||
|
|||
175 | ) |
||
176 | ) { |
||
177 | throw new phpmailerException($this->lang('authenticate')); |
||
178 | } |
||
179 | } |
||
180 | return true; |
||
181 | } catch (phpmailerException $exc) { |
||
182 | $lastexception = $exc; |
||
183 | $this->edebug($exc->getMessage()); |
||
184 | // We must have connected, but then failed TLS or Auth, so close connection nicely |
||
185 | $this->smtp->quit(); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | // If we get here, all connection attempts have failed, so close connection hard |
||
190 | $this->smtp->close(); |
||
191 | // As we've caught all exceptions, just report whatever the last one was |
||
192 | if ($this->exceptions and !is_null($lastexception)) { |
||
193 | throw $lastexception; |
||
194 | } |
||
195 | return false; |
||
196 | } |
||
198 |