1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Email\Protocols;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Email\Address;
|
19
|
|
|
use O2System\Email\Message;
|
20
|
|
|
use O2System\Email\Spool;
|
21
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* Class SmtpProtocol
|
25
|
|
|
*
|
26
|
|
|
* @package O2System\Email\Protocols
|
27
|
|
|
*/
|
28
|
|
|
class SmtpProtocol extends Abstracts\AbstractProtocol
|
29
|
|
|
{
|
30
|
|
|
/**
|
31
|
|
|
* SmtpProtocol::$config
|
32
|
|
|
*
|
33
|
|
|
* @var \O2System\Email\DataStructures\Config
|
34
|
|
|
*/
|
35
|
|
|
protected $config;
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* SmtpProtocol::__construct
|
39
|
|
|
*
|
40
|
|
|
* @param \O2System\Email\Spool $spool
|
41
|
|
|
*/
|
42
|
|
|
public function __construct(Spool $spool)
|
43
|
|
|
{
|
44
|
|
|
parent::__construct($spool);
|
45
|
|
|
|
46
|
|
|
$this->config = $this->spool->getConfig();
|
47
|
|
|
|
48
|
|
|
if ( ! $this->config->offsetExists('debug')) {
|
49
|
|
|
/**
|
50
|
|
|
* Debug output level.
|
51
|
|
|
* Options:
|
52
|
|
|
* `0` No output
|
53
|
|
|
* `1` Commands
|
54
|
|
|
* `2` Data and commands
|
55
|
|
|
* `3` As 2 plus connection status
|
56
|
|
|
* `4` Low-level data output.
|
57
|
|
|
*/
|
58
|
|
|
$this->config[ 'debug' ] = 0;
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
if ( ! $this->config->offsetExists('auth')) {
|
62
|
|
|
$this->config[ 'auth' ] = false;
|
63
|
|
|
if ( ! empty($this->config[ 'username' ])) {
|
64
|
|
|
$this->config[ 'auth' ] = true;
|
65
|
|
|
}
|
66
|
|
|
}
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
// ------------------------------------------------------------------------
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* MailProtocol::sending
|
73
|
|
|
*
|
74
|
|
|
* Protocol message sending process.
|
75
|
|
|
*
|
76
|
|
|
* @param Message $message
|
77
|
|
|
*
|
78
|
|
|
* @return bool
|
79
|
|
|
* @throws \PHPMailer\PHPMailer\Exception
|
80
|
|
|
*/
|
81
|
|
|
protected function sending(Message $message)
|
82
|
|
|
{
|
83
|
|
|
$phpMailer = new PHPMailer();
|
84
|
|
|
$phpMailer->isSMTP();
|
85
|
|
|
$phpMailer->SMTPDebug = $this->config[ 'debug' ];
|
86
|
|
|
|
87
|
|
|
$host = $this->config[ 'host' ];
|
88
|
|
|
|
89
|
|
|
if (is_array($this->config[ 'host' ])) {
|
90
|
|
|
$host = implode(';', $this->config[ 'host' ]);
|
91
|
|
|
}
|
92
|
|
|
|
93
|
|
|
$phpMailer->Host = $host;
|
94
|
|
|
$phpMailer->SMTPAuth = $this->config[ 'auth' ];
|
95
|
|
|
$phpMailer->Username = $this->config[ 'username' ];
|
96
|
|
|
$phpMailer->Password = $this->config[ 'password' ];
|
97
|
|
|
$phpMailer->SMTPSecure = $this->config[ 'encryption' ];
|
98
|
|
|
$phpMailer->Port = $this->config[ 'port' ];
|
99
|
|
|
|
100
|
|
|
// Set from
|
101
|
|
|
if (false !== ($from = $message->getFrom())) {
|
|
|
|
|
102
|
|
|
$phpMailer->setFrom($from->getEmail(), $from->getName());
|
|
|
|
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
// Set recipient
|
106
|
|
|
if (false !== ($to = $message->getTo())) {
|
107
|
|
|
foreach ($to as $address) {
|
108
|
|
|
if ($address instanceof Address) {
|
109
|
|
|
$phpMailer->addAddress($address->getEmail(), $address->getName());
|
|
|
|
|
110
|
|
|
}
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
|
114
|
|
|
// Set reply-to
|
115
|
|
|
if (false !== ($replyTo = $message->getReplyTo())) {
|
|
|
|
|
116
|
|
|
$phpMailer->addReplyTo($replyTo->getEmail(), $replyTo->getName());
|
|
|
|
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
// Set content-type
|
120
|
|
|
if ($message->getContentType() === 'html') {
|
121
|
|
|
$phpMailer->isHTML(true);
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
// Set subject, body & alt-body
|
125
|
|
|
$phpMailer->Subject = $message->getSubject();
|
126
|
|
|
$phpMailer->Body = $message->getBody();
|
127
|
|
|
$phpMailer->AltBody = $message->getAltBody();
|
128
|
|
|
|
129
|
|
|
if (false !== ($attachments = $message->getAttachments())) {
|
130
|
|
|
foreach ($attachments as $filename => $attachment) {
|
131
|
|
|
$phpMailer->addAttachment($attachment, $filename);
|
132
|
|
|
}
|
133
|
|
|
}
|
134
|
|
|
|
135
|
|
|
if ( ! $phpMailer->send()) {
|
136
|
|
|
$this->addError(100, $phpMailer->ErrorInfo);
|
137
|
|
|
|
138
|
|
|
return false;
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
return true;
|
142
|
|
|
}
|
143
|
|
|
} |