Completed
Push — master ( 653203...665d50 )
by
unknown
03:07
created

Smtp::cleanAddr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Email\SMTP
5
 *
6
 * Email\SMTP PHP driver.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
namespace Email;
14
15
class Smtp implements Driver {
16
17
  protected
18
    $socket,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $socket.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
19
    $host,
20
    $secure,
21
    $port,
22
    $lastCode,
23
    $lastMessage,
24
    $username,
25
    $password;
26
27
  public function onInit($options) {
28
    $options        = (object)$options;
29
    $this->host     = isset($options->host)     ? $options->host     : 'localhost';
30
    $this->username = isset($options->username) ? $options->username : false;
31
    $this->secure   = isset($options->secure)   ? $options->secure   : ($this->username ? true : false);
32
    $this->port     = isset($options->port)     ? $options->port     : ($this->secure ? 465 : 25);
33
    $this->password = isset($options->password) ? $options->password : false;
34
  }
35
36
  protected function connect(){
37
    if ($this->socket) $this->close();
38
    $url = ($this->secure ? 'tls' : 'tcp') ."://{$this->host}";
39
    $this->socket = fsockopen( $url, $this->port, $errno, $errstr, 30 );
40
    if ( ! $this->socket ) throw new \Exception("Unable to connect to $url on port {$this->port}.");
41
    $this->lastMessage = '';
42
    $this->lastCode = 0;
43
  }
44
45
  public function close(){
46
    $this->socket && @fclose($this->socket);
47
  }
48
49
  protected function write($data, $nl = 1){
50
    $payload = $data . str_repeat("\r\n",$nl);
51
    fwrite($this->socket, $payload);
52
  }
53
54
  protected function expectCode($code){
55
56
    $this->lastMessage = '';
57
    while (substr($this->lastMessage, 3, 1) != ' '){
58
      $this->lastMessage = fgets($this->socket, 256);
59
    }
60
61
    $this->lastCode = 1 * substr($this->lastMessage, 0, 3);
62
    return $code == $this->lastCode;
63
  }
64
65
  protected function cleanAddr($email){
66
    return preg_replace('((.*?)<([\w.@-]+)>(.*?))','$2',$email);
67
  }
68
69
  protected function SMTPmail($from,$to,$body){
70
    $this->connect();
71
    $this->expectCode(220);
72
73
    $this->write("EHLO {$this->host}");
74
    $this->expectCode(250);
75
76
    if ($this->username){
77
      $this->write("AUTH LOGIN");
78
      $this->expectCode(334);
79
      $this->write(base64_encode($this->username));
80
      $this->expectCode(334);
81
      $this->write(base64_encode($this->password));
82
      $this->expectCode(334);
83
    }
84
85
    $from = $this->cleanAddr($from);
86
87
    $this->write("MAIL FROM: <{$from}>");
88
    $this->expectCode(250);
89
90
    $to = $this->cleanAddr($to);
91
92
    $this->write("RCPT TO: <{$to}>");
93
    $this->expectCode(250);
94
95
    $this->write("DATA");
96
    $this->expectCode(354);
97
98
    $this->write($body);
99
100
    $this->write(".");
101
    $success = $this->expectCode(250);
102
103
    $this->write("QUIT");
104
105
    $this->close();
106
    return $success;
107
  }
108
109
  public function onSend(Envelope $envelope){
110
    $results = [];
111
    foreach ($envelope->to() as $to) {
112
      $results[$to] = $this->SMTPmail($envelope->from(), $to, $envelope->build());
113
    }
114
    return $results;
115
  }
116
117
}
118
119