|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/mail package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Mail\Transport; |
|
11
|
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
|
13
|
|
|
use Slick\Mail\Header\HeaderInterface; |
|
14
|
|
|
use Slick\Mail\MessageInterface; |
|
15
|
|
|
use Zend\Mail\Message as ZendMessage; |
|
16
|
|
|
use Zend\Mail\Transport\Smtp; |
|
17
|
|
|
use Zend\Mail\Transport\SmtpOptions; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* SMTP Transport |
|
21
|
|
|
* |
|
22
|
|
|
* @package Slick\Mail\Transport |
|
23
|
|
|
* @author Filipe Silva <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class SmtpTransport extends Base implements MailTransportInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Smtp |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $smtpServer; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @readwrite |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $options; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Gets the Zend SMTP service |
|
41
|
|
|
* |
|
42
|
|
|
* @return Smtp |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function getSmtpServer() |
|
45
|
|
|
{ |
|
46
|
4 |
|
if (null == $this->smtpServer) { |
|
47
|
2 |
|
$smtpServer = new Smtp(); |
|
48
|
2 |
|
$settings = new SmtpOptions($this->options); |
|
49
|
2 |
|
$smtpServer->setOptions($settings); |
|
50
|
2 |
|
$this->setSmtpServer($smtpServer); |
|
51
|
1 |
|
} |
|
52
|
4 |
|
return $this->smtpServer; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sets the Zend SMTP service |
|
57
|
|
|
* |
|
58
|
|
|
* @param Smtp $smtpServer |
|
59
|
|
|
* |
|
60
|
|
|
* @return SmtpTransport|$this|self |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public function setSmtpServer(Smtp $smtpServer) |
|
63
|
|
|
{ |
|
64
|
4 |
|
$this->smtpServer = $smtpServer; |
|
65
|
4 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Send a e-mail message |
|
70
|
|
|
* |
|
71
|
|
|
* @param MessageInterface $message |
|
72
|
|
|
* |
|
73
|
|
|
* @return MailTransportInterface |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function send(MessageInterface $message) |
|
76
|
|
|
{ |
|
77
|
2 |
|
$message = $this->convert($message); |
|
78
|
2 |
|
$this->getSmtpServer()->send($message); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param MessageInterface $message |
|
83
|
|
|
* |
|
84
|
|
|
* @return ZendMessage |
|
85
|
|
|
*/ |
|
86
|
2 |
|
protected function convert(MessageInterface $message) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$text = ''; |
|
89
|
2 |
|
foreach ($message->getHeaders() as $header) { |
|
90
|
2 |
|
$text .= "{$header}".HeaderInterface::EOL; |
|
91
|
1 |
|
} |
|
92
|
|
|
$text .= 'X-Mailer: Zend-SMPT-Transport; PHP/' . |
|
93
|
2 |
|
phpversion() . |
|
94
|
2 |
|
HeaderInterface::EOL; |
|
95
|
2 |
|
$text .= HeaderInterface::EOL.$message->getBodyText(); |
|
96
|
2 |
|
return ZendMessage::fromString($text); |
|
97
|
|
|
} |
|
98
|
|
|
} |