Skip to content

Commit fb53446

Browse files
committed
refactor
1 parent 56757a1 commit fb53446

File tree

5 files changed

+55
-105
lines changed

5 files changed

+55
-105
lines changed

src/Mailer.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Mailer
1515

1616
const MAILER_VERSION = '3.0.0-beta';
1717

18-
const DEFAULT_CHANNEL = 'default';
18+
const CHANNEL_DEFAULT = 'default';
1919

2020
const TRANSPORT_SENDMAIL = 'sendmail';
2121
const TRANSPORT_SMTP = 'smtp';
@@ -34,7 +34,7 @@ final class Mailer
3434
*/
3535
public function __construct($transport = self::TRANSPORT_SENDMAIL, $options = array())
3636
{
37-
$this->setChannel(self::DEFAULT_CHANNEL, $transport, $options);
37+
$this->setChannel(self::CHANNEL_DEFAULT, $transport, $options);
3838
}
3939

4040
/**
@@ -65,7 +65,8 @@ public function setChannel($name, $transport, $options = array())
6565
break;
6666
default:
6767
$opt = array_key_exists('options', $options)?(string)$options['options']:'';
68-
$connect = new Sendmail($opt);
68+
$sender = array_key_exists('sender', $options)?(string)$options['sender']:'';
69+
$connect = new Sendmail($sender, $opt);
6970
}
7071
$this->channels[$name] = $connect;
7172
}
@@ -77,34 +78,49 @@ public function setChannel($name, $transport, $options = array())
7778
public function removeChannel($name)
7879
{
7980
$name = (string)$name;
80-
if ($name == self::DEFAULT_CHANNEL) {
81+
if ($name == self::CHANNEL_DEFAULT) {
8182
throw new ChannelCantBeRemovedException($name);
8283
}
8384
unset($this->channels[$name]);
8485
}
8586

8687
/**
8788
* @param Message $message
88-
* @param Book $addresses
89-
* @param bool $personal
89+
* @param string[] $to
9090
* @param string $channel
9191
* @throws ChannelNotExistsException
9292
*/
93-
public function send(Message $message, Book $addresses, $personal = false, $channel = self::DEFAULT_CHANNEL)
93+
public function send(Message $message, $to, $channel = self::CHANNEL_DEFAULT)
94+
{
95+
foreach ($to as $address) {
96+
$message
97+
->removeHeader('To')
98+
->removeHeader('CC')
99+
->removeHeader('BCC')
100+
;
101+
$this->mass($message, array($address), array(), array(), $channel);
102+
}
103+
}
104+
105+
/**
106+
* @param Message $message
107+
* @param string[] $to
108+
* @param string[] $cc
109+
* @param string[] $bcc
110+
* @param string $channel
111+
* @throws ChannelNotExistsException
112+
*/
113+
public function mass(Message $message, $to, $cc = array(), $bcc = array(), $channel = self::CHANNEL_DEFAULT)
94114
{
95-
$message->setHeader('X-Mailer', 'ddrv/mailer-'.self::MAILER_VERSION.' (https://github.com/ddrv/mailer)');
96115
$channel = (string)$channel;
97116
if (!array_key_exists($channel, $this->channels)) {
98117
throw new ChannelNotExistsException($channel);
99118
}
100-
if ($personal) {
101-
foreach ($addresses as $address) {
102-
$recipient = new Book();
103-
$recipient->add($address);
104-
$this->channels[$channel]->send($message, $recipient);
105-
}
106-
} else {
107-
$this->channels[$channel]->send($message, $addresses);
108-
}
119+
$message->setHeader('X-Mailer', 'ddrv/mailer-'.self::MAILER_VERSION.' (https://github.com/ddrv/mailer)');
120+
$message->setHeader('To', implode(',', $to));
121+
$message->setHeader('CC', implode(',', $cc));
122+
$message->setHeader('BCC', implode(',', $bcc));
123+
$addresses = array_unique(array_merge($to, $cc, $bcc));
124+
$this->channels[$channel]->send($message, $addresses);
109125
}
110126
}

src/Message.php

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,31 @@ final class Message
2525
*/
2626
private $attachments = array();
2727

28-
/**
29-
* @var Book
30-
*/
31-
private $cc;
32-
33-
/**
34-
* @var Book
35-
*/
36-
private $bcc;
37-
3828
/**
3929
* @var string
4030
*/
4131
private $boundary;
4232

43-
public function __construct(Address $sender, $subject, $message, $isHtml = true)
33+
public function __construct($subject, $message, $isHtml = true)
4434
{
45-
$this->cc = new Book();
46-
$this->bcc = new Book();
4735
$this->subject = (string)$subject;
4836
$this->message = array(
4937
'content' => base64_encode($message),
5038
'mime' => $isHtml ? 'text/html' : 'text/plain',
5139
);
5240
$this->boundary = md5(time());
5341
$this->setHeader('MIME-Version','1.0');
54-
$this->setHeader('From', $sender->getContact());
5542
}
5643

5744
public function setHeader($header, $value)
5845
{
5946
$header = (string)$header;
60-
$this->headers[mb_strtolower($header)] = $header . ': ' . (string)$value;
47+
$value = (string)$value;
48+
if ($value) {
49+
$this->headers[mb_strtolower($header)] = $header . ': ' . $value;
50+
} else {
51+
$this->removeHeader($header);
52+
}
6153
return $this;
6254
}
6355

@@ -135,16 +127,7 @@ public function getHeadersLine()
135127
$this->setHeader('Content-Type', 'multipart/mixed; boundary="'.$this->boundary.'"');
136128
$this->setHeader('Content-Transfer-Encoding', '7bit');
137129
}
138-
$this->removeHeader('to');
139-
$this->removeHeader('cc');
140-
$this->removeHeader('bcc');
141130
$headers = implode("\r\n", $this->headers);
142-
if (!$this->cc->isEmpty()) {
143-
$headers .= "\r\nCC: {$this->cc->getContacts()}";
144-
}
145-
if (!$this->bcc->isEmpty()) {
146-
$headers .= "\r\nBCC: {$this->bcc->getContacts()}";
147-
}
148131
return $headers;
149132
}
150133

@@ -172,37 +155,6 @@ public function getBody()
172155
return $body;
173156
}
174157

175-
public function addCC($email, $name = '')
176-
{
177-
$this->cc->add(new Address($email, $name));
178-
}
179-
180-
public function removeCC($email)
181-
{
182-
$this->cc->remove(new Address($email));
183-
}
184-
185-
public function getCC()
186-
{
187-
return $this->cc;
188-
}
189-
190-
public function addBCC($email, $name = '')
191-
{
192-
$this->bcc->add(new Address($email, $name));
193-
}
194-
195-
public function removeBCC($email)
196-
{
197-
$this->bcc->remove(new Address($email));
198-
}
199-
200-
public function getBCC()
201-
{
202-
return $this->bcc;
203-
}
204-
205-
206158
/**
207159
* Return correct attachment name.
208160
*

src/Transport/Sendmail.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ final class Sendmail implements TransportInterface
1010

1111
private $options;
1212

13-
public function __construct($options = '')
13+
private $sender;
14+
15+
public function __construct($sender, $options = '')
1416
{
17+
$this->sender = $sender;
1518
$this->options = (string)$options;
1619
}
1720

18-
public function send(Message $message, Book $addresses)
21+
public function send(Message $message, $recipients)
1922
{
23+
$message->setHeader('From', $this->sender);
2024
$subject = $message->getSubject();
2125
$body = $message->getBody();
22-
$headers = $headers = "{$message->getHeadersLine()}\r\nTo: {$addresses->getContacts()}";
23-
$rcpt = array();
24-
$rcpt[] = $addresses->getContacts();
25-
if (!$message->getCC()->isEmpty()) {
26-
$rcpt[] = $message->getCC()->getContacts();
27-
}
28-
if (!$message->getBCC()->isEmpty()) {
29-
$rcpt[] = $message->getBCC()->getContacts();
30-
}
31-
$to = implode(', ', $rcpt);
26+
$headers = $message->getHeadersLine();
27+
$to = implode(', ', $recipients);
3228
return mail($to, $subject, $body, $headers, $this->options);
3329
}
3430
}

src/Transport/Smtp.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Ddrv\Mailer\Transport;
44

5-
use Ddrv\Mailer\Book;
65
use Ddrv\Mailer\Message;
76

87
final class Smtp implements TransportInterface
@@ -55,27 +54,15 @@ public function __construct($host, $port, $user, $password, $sender, $encryption
5554
}
5655
}
5756

58-
public function send(Message $message, Book $addresses)
57+
public function send(Message $message, $recipients)
5958
{
59+
$message->setHeader('From', $this->sender);
6060
$this->smtpCommand("MAIL FROM: <{$this->sender}>");
61-
$headers = "{$message->getHeadersLine()}\r\nTo: {$addresses->getContacts()}";
62-
foreach ($addresses as $address) {
63-
$this->smtpCommand("RCPT TO: <{$address->getEmail()}>");
64-
}
65-
$cc = $message->getCC();
66-
if (!$cc->isEmpty()) {
67-
foreach ($cc as $address) {
68-
$this->smtpCommand("RCPT TO: <{$address->getEmail()}>");
69-
}
70-
}
71-
$bcc = $message->getBCC();
72-
if (!$bcc->isEmpty()) {
73-
foreach ($bcc as $address) {
74-
$this->smtpCommand("RCPT TO: <{$address->getEmail()}>");
75-
}
61+
$headers = $message->getHeadersLine();
62+
foreach ($recipients as $address) {
63+
$this->smtpCommand("RCPT TO: <$address>");
7664
}
7765
$this->smtpCommand("DATA");
78-
7966
$this->smtpCommand("$headers\r\n\r\n{$message->getBody()}\r\n.");
8067
return true;
8168
}

src/Transport/TransportInterface.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Ddrv\Mailer\Transport;
44

5-
use Ddrv\Mailer\Book;
65
use Ddrv\Mailer\Message;
76

87
interface TransportInterface
@@ -11,9 +10,9 @@ interface TransportInterface
1110
* Send mail
1211
*
1312
* @param Message $message
14-
* @param Book $addresses
13+
* @param string[] $recipients
1514
* @return bool
1615
*/
17-
public function send(Message $message, Book $addresses);
16+
public function send(Message $message, $recipients);
1817
}
1918

0 commit comments

Comments
 (0)