Completed
Push — feat_email_envelope ( e5445d )
by Stefano
02:37
created

Smtp::SMTPmail()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 35
rs 8.8571
cc 2
eloc 24
nc 2
nop 3
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,
19
    $host,
20
    $secure,
21
    $port,
22
    $lastCode,
23
    $lastMessage,
24
    $username,
25
    $password;
26
27
  public function __construct($options = null) {
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 SMTPmail($from,$to,$body){
66
    $this->connect();
67
    $this->expectCode(220);
68
69
    $this->write("EHLO {$this->host}");
70
    $this->expectCode(250);
71
72
    if ($this->username){
73
      $this->write("AUTH LOGIN");
74
      $this->expectCode(334);
75
      $this->write(base64_encode($this->username));
76
      $this->expectCode(334);
77
      $this->write(base64_encode($this->password));
78
      $this->expectCode(334);
79
    }
80
81
    $this->write("MAIL FROM: <{$from}>");
82
    $this->expectCode(250);
83
84
    $this->write("RCPT TO: <{$to}>");
85
    $this->expectCode(250);
86
87
    $this->write("DATA");
88
    $this->expectCode(354);
89
90
    $this->write($body);
91
92
    $this->write(".");
93
    $success = $this->expectCode(250);
94
95
    $this->write("QUIT");
96
97
    $this->close();
98
    return $success;
99
  }
100
101 View Code Duplication
  public function send(Envelope $envelope){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    // PHP requires direct handling of To and Subject Headers.
103
    $success     = true;
104
    $recipients  = $envelope->to();
105
    $from        = $envelope->from();
106
    $envelope->to(false);
107
    $envelope->from(false);
108
    foreach ($recipients as $to) {
109
      $current_success = $this->SMTPmail($from, $to, $envelope->build());
110
      \Event::trigger('core.email.send',$to,$envelope,'native');
111
      $success = $success && $current_success;
112
    }
113
    return $success;
114
  }
115
116
}
117
118