Skip to content

Commit 56757a1

Browse files
committed
refactor
1 parent 7bd60a4 commit 56757a1

15 files changed

+591
-118
lines changed

README.md

Lines changed: 151 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
# Mailer
8-
PHP Class for sending email.
8+
PHP library for sending email.
99

1010
# Install
1111
## With [Composer](https://getcomposer.org/)
@@ -18,61 +18,184 @@ PHP Class for sending email.
1818
require_once('vendor/autoload.php');
1919
```
2020
21+
2122
# Usage
2223
2324
```php
2425
<?php
2526
2627
/*
27-
* Initialization Mailer class.
28+
* Initialization Mailer class with SMTP transport
2829
*/
29-
$mailer = new \Ddrv\Mailer\Mailer();
30+
$mailer = new \Ddrv\Mailer\Mailer(
31+
'smtp',
32+
array(
33+
'host' => 'smtp.fight.club', // host
34+
'port' => 25, // port
35+
'username' => 'joe', // login
36+
'password' => 'IAmJoesLiver', // password
37+
'sender' => 'joe@fight.club', // sender
38+
'encrypt' => null, // encryption: 'tls', 'ssl' or null
39+
'domain' => 'http://fight.club' // domain
40+
)
41+
);
3042
3143
/*
32-
* If need use SMTP server, setting it
44+
* Initialization Mailer class with sendmail transport
3345
*/
34-
$mailer->smtp(
35-
'smtp.host.name', // host
36-
25, // port
37-
'from@host.name', // login
38-
'password for from', // password
39-
'from@host.name', // sender
40-
null, // encryption: 'tls', 'ssl' or null
41-
'http://host.name' // domain
46+
$mailer = new \Ddrv\Mailer\Mailer(
47+
'sendmail',
48+
array(
49+
'options' => '-f', // sendmail options
50+
)
4251
);
4352
44-
/*
45-
* If need switch provider back to mail() function, use
46-
*/
47-
$mailer->legacy('-f');
4853
4954
/*
5055
* Create message
5156
*/
52-
$message = new \Ddrv\Mailer\Message('from@host.name', 'subject', '<p>Simple text</p>', true);
57+
$sender = new \Ddrv\Mailer\Address('joe@fight.club', 'Incognito');
5358
54-
/*
55-
* You can set named sender from@host.name as Site Administrator
56-
*/
57-
$message->setSender('from@host.name', 'Site Administrator');
59+
$message = new \Ddrv\Mailer\Message(
60+
$sender, // sender email
61+
'Fight Club', // subject of message
62+
'<p>Welcome to the Fight Club</p>', // text of message
63+
true // true for html, false for plain text
64+
);
5865
5966
/*
6067
* If need adding attachment from string, run
6168
*/
62-
$message->attachFromString('attach1.txt', 'content', 'text/plain');
69+
$rules = <<<TEXT
70+
1. You don't talk about fight club.
71+
2. You don't talk about fight club.
72+
3. When someone says stop, or goes limp, the fight is over.
73+
4. Only two guys to a fight.
74+
5. One fight at a time.
75+
6. They fight without shirts or shoes.
76+
7. The fights go on as long as they have to.
77+
8. If this is your first night at fight club, you have to fight.
78+
TEXT;
79+
80+
$message->attachFromString(
81+
'fight-club-rules.txt', // attachment name
82+
$rules, // content
83+
'text/plain' // content-type
84+
);
6385
6486
/*
6587
* If need adding attachment from file, run
6688
*/
67-
$message->attachFromFile('attach2.txt', '/path/to/file');
89+
$message->attachFromFile(
90+
'project-mayhem-rules.txt', // attachment name
91+
'/home/tyler/docs/projects/mayhem/rules.txt' // path to attached file
92+
);
6893
6994
/*
70-
* Send email to addresses (one mail for all addresses)
95+
* Create recipients
7196
*/
72-
$mailer->send($message, array('email1@host.name', 'email2@host.name'));
97+
$recipients = new \Ddrv\Mailer\Book();
98+
$recipients->add(new \Ddrv\Mailer\Address('tyler@fight.club', 'Tyler Durden'));
99+
$recipients->add(new \Ddrv\Mailer\Address('angel@fight.club', 'Angel Face'));
100+
$recipients->add(new \Ddrv\Mailer\Address('bob@fight.club', 'Robert Paulson'));
101+
73102
74103
/*
75-
* or send personal mailing one mail per addresses
104+
* Send email to addresses
76105
*/
77-
$mailer->send($message, array('email1@host.name', 'email2@host.name', true));
78-
```
106+
$mailer->send(
107+
$message, // message
108+
$recipients, // recipients
109+
false // false for group mailing (one mail for all addresses), true for personal mailing (one mail per address)
110+
);
111+
```
112+
113+
# Channels
114+
115+
You can add some channels for sending.
116+
117+
```php
118+
<?php
119+
120+
// create default channel
121+
$mailer = new \Ddrv\Mailer\Mailer(
122+
'smtp',
123+
array(
124+
'host' => 'smtp.host.name',
125+
'port' => 25,
126+
'username' => 'no-reply@host.name',
127+
'password' => 'password',
128+
'sender' => 'no-reply@host.name',
129+
'encrypt' => 'tls',
130+
'domain' => 'http://host.name'
131+
)
132+
);
133+
134+
// create support channel
135+
$mailer->setChannel(
136+
'support',
137+
'smtp',
138+
array(
139+
'host' => 'smtp.host.name',
140+
'port' => 25,
141+
'username' => 'support@host.name',
142+
'password' => 'password',
143+
'sender' => 'support@host.name',
144+
'encrypt' => null,
145+
'domain' => 'http://host.name'
146+
)
147+
);
148+
149+
$sender1 = new \Ddrv\Mailer\Address('no-reply@host.name', 'Informer');
150+
$msg1 = new \Ddrv\Mailer\Message(
151+
$sender1,
152+
'host.name: your account registered',
153+
'Your account registered! Please do not reply to this email',
154+
false
155+
);
156+
157+
$sender2 = new \Ddrv\Mailer\Address('support@host.name', 'Support Agent');
158+
$msg2 = new \Ddrv\Mailer\Message(
159+
$sender2,
160+
'host.name: ticket #4221 closed',
161+
'<p>Ticket #4221 closed</p>',
162+
true
163+
);
164+
165+
$rcpt1 = new \Ddrv\Mailer\Book();
166+
$rcpt1->add(new \Ddrv\Mailer\Address('recipient1@host.name', 'Recipient First'));
167+
$rcpt1->add(new \Ddrv\Mailer\Address('recipient2@host.name'));
168+
169+
$rcpt2 = new \Ddrv\Mailer\Book();
170+
$rcpt2->add(new \Ddrv\Mailer\Address('recipient3@host.name', 'Other Recipient'));
171+
172+
$mailer->send($msg1, $rcpt1, true); // send to default channel
173+
$mailer->send($msg2, $rcpt2, true, 'support'); // send to support channel
174+
```
175+
176+
# CC and BCC
177+
178+
```php
179+
180+
<?php
181+
182+
$msg = new \Ddrv\Mailer\Message(
183+
new \Ddrv\Mailer\Address('no-reply@host.name', 'Informer'),
184+
'host.name: your account registered',
185+
'Your account registered! Please do not reply to this email',
186+
false
187+
);
188+
$msg->addCC('cc1@host.name', 'User Name');
189+
$msg->addCC('cc2@host.name');
190+
191+
$msg->addBCC('bcc1@host.name', 'User Name');
192+
$msg->addBCC('bcc2@host.name');
193+
194+
$rcpt = new \Ddrv\Mailer\Book();
195+
$rcpt->add(new \Ddrv\Mailer\Address('recipient@host.name', 'Recipient'));
196+
197+
$mailer->send($msg, $rcpt);
198+
199+
```
200+
201+

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"ext-fileinfo": "Mime-Type detecting"
1010
},
1111
"type": "library",
12-
"description": "PHP Class for sending email",
13-
"keywords": ["email", "mail", "send", "attachments", "smtp"],
12+
"description": "PHP library for sending email",
13+
"keywords": ["email", "mail", "send", "attachments", "smtp", "sendmail"],
1414
"license": "MIT",
1515
"authors": [
1616
{

src/Address.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer;
4+
5+
final class Address
6+
{
7+
8+
private $email;
9+
10+
private $name;
11+
12+
public function __construct($email, $name = '')
13+
{
14+
$this->email = (string)$email;
15+
$this->name = (string)$name;
16+
if (preg_match('/[^\pL\s]/', $this->name)) $this->name = "\"{$this->name}\"";
17+
}
18+
19+
public function getContact()
20+
{
21+
if (!$this->isValid()) return null;
22+
if (!$this->name) return "<{$this->email}>";
23+
return "{$this->name} <{$this->email}>";
24+
}
25+
26+
public function getEmail()
27+
{
28+
if (!$this->isValid()) return null;
29+
return $this->email;
30+
}
31+
32+
public function isValid()
33+
{
34+
$arr = explode('@', $this->email);
35+
return (count($arr) == 2);
36+
}
37+
}

src/Book.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer;
4+
5+
use Iterator;
6+
7+
final class Book implements Iterator
8+
{
9+
10+
/**
11+
* @var Address[]
12+
*/
13+
private $book = array();
14+
15+
/**
16+
* @var string[]
17+
*/
18+
private $exists = array();
19+
20+
/**
21+
* @var int
22+
*/
23+
private $cursor = 0;
24+
25+
/**
26+
* @param Address $address
27+
* @return bool
28+
*/
29+
public function add(Address $address)
30+
{
31+
if (!$address->isValid()) return false;
32+
$this->book[] = $address;
33+
$this->exists = $address->getEmail();
34+
return true;
35+
}
36+
37+
/**
38+
* @param Address $address
39+
* @return bool|int
40+
*/
41+
public function remove(Address $address)
42+
{
43+
if (!$address->isValid()) return false;
44+
$removed = 0;
45+
foreach ($this->book as $key => $item) {
46+
if ($address->getEmail() == $item->getEmail()) {
47+
$removed++;
48+
unset($this->book[$key]);
49+
}
50+
}
51+
$this->book = array_values($this->book);
52+
return $removed;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getContacts()
59+
{
60+
$list = array();
61+
foreach ($this->book as $address) {
62+
$list[] = $address->getContact();
63+
}
64+
return implode(', ', $list);
65+
}
66+
67+
/**
68+
* @return string
69+
*/
70+
public function getEmails()
71+
{
72+
$list = array();
73+
foreach ($this->book as $address) {
74+
$list[] = $address->getEmail();
75+
}
76+
return implode(', ', $list);
77+
}
78+
79+
public function isEmpty()
80+
{
81+
return empty($this->book);
82+
}
83+
84+
/**
85+
* @return Address|null
86+
*/
87+
public function current()
88+
{
89+
if (!array_key_exists($this->cursor, $this->book)) return null;
90+
return $this->book[$this->cursor];
91+
}
92+
93+
/**
94+
* @void
95+
*/
96+
public function next()
97+
{
98+
$this->cursor++;
99+
}
100+
101+
/**
102+
* @return int
103+
*/
104+
public function key()
105+
{
106+
return $this->cursor;
107+
}
108+
109+
/**
110+
* @return bool
111+
*/
112+
public function valid()
113+
{
114+
return array_key_exists($this->cursor, $this->book);
115+
}
116+
117+
/**
118+
* @void
119+
*/
120+
public function rewind()
121+
{
122+
$this->cursor = 0;
123+
}
124+
}

0 commit comments

Comments
 (0)