Smtp::setTimeOut()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Sendmail package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2010, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT MIT
8
 */
9
10
namespace Sendmail\Sender;
11
12
use Sendmail\Message;
13
use Sendmail\Sender\Smtp\Dialogue;
14
15
/**
16
 * SMTP/ESMTP sender RFC 5321.
17
 *
18
 * @author  Peter Gribanov <[email protected]>
19
 */
20
class Smtp implements SenderInterface
21
{
22
    /**
23
     * The connection timeout.
24
     *
25
     * @var int
26
     */
27
    protected $timeout = -1;
28
29
    /**
30
     * Use a secure connection.
31
     *
32
     * @var bool
33
     */
34
    protected $secure = false;
35
36
    /**
37
     * SMTP server.
38
     *
39
     * @var string
40
     */
41
    protected $server = '';
42
43
    /**
44
     * Connection port.
45
     *
46
     * @var int
47
     */
48
    protected $port = 25;
49
50
    /**
51
     * Username for authorization.
52
     *
53
     * @var string
54
     */
55
    protected $auth_username = '';
56
57
    /**
58
     * Password for authorization.
59
     *
60
     * @var string
61
     */
62
    protected $auth_password = '';
63
64
    /**
65
     * @param string $server
66
     * @param int $port
67
     * @param string $username
68
     * @param string $password
69
     */
70
    public function __construct($server, $port = 25, $username = '', $password = '')
71
    {
72
        $this->server = $server;
73
        $this->port = $port;
74
        $this->auth_username = $username;
75
        $this->auth_password = $password;
76
    }
77
78
    /**
79
     * Send E-mail message.
80
     *
81
     * @param Message $message
82
     *
83
     * @return bool
84
     */
85
    public function send(Message $message)
86
    {
87
        $dialogue = new Dialogue($this->server, $this->port, $this->timeout);
88
89
        // SMTP-session is established, can send requests
90
91
        // is ESMTP?
92
        if ($dialogue->call('EHLO '.$_SERVER['HTTP_HOST'])) {
93
            // open the TLS connection if need
94
            if ($this->secure) {
95
                $dialogue->call('STARTTLS');
96
                // after starting TLS need to say again EHLO
97
                $dialogue->call('EHLO '.$_SERVER['HTTP_HOST'], true);
98
            }
99
        } else {
100
            $dialogue->call('HELO '.$_SERVER['HTTP_HOST'], true);
101
        }
102
103
        // authorizing
104
        if ($this->auth_username && $this->auth_password) {
105
            $dialogue->call('AUTH LOGIN');
106
            $dialogue->call(base64_encode($this->auth_username));
107
            $dialogue->call(base64_encode($this->auth_password), true);
108
        }
109
110
        $dialogue->call('MAIL FROM: '.$message->getFrom(), true);
111
        $dialogue->call('RCPT TO: '.$message->getTo(), true);
112
        $dialogue->call('DATA');
113
114
        // point at the end means the end of the message
115
        $dialogue->call(
116
            $message->getHeaders()."\r\n\r\n".
117
            $message->getText()."\r\n.",
118
            true
119
        );
120
121
        // completes data transmission and close SMTP connect
122
        $result = $dialogue->call('QUIT');
123
        $dialogue->end();
124
125
        return $result;
126
    }
127
128
    /**
129
     * Set timeout connecting to the server.
130
     *
131
     * @param int $timeout
132
     *
133
     * @return self
134
     */
135
    public function setTimeOut($timeout)
136
    {
137
        if ($timeout > 0) {
138
            $this->timeout = $timeout;
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * Start secure connection.
146
     *
147
     * @param bool $secure
148
     *
149
     * @return self
150
     */
151
    public function setSecure($secure = true)
152
    {
153
        $this->secure = $secure;
154
155
        return $this;
156
    }
157
}
158