Skip to content

Commit 576c857

Browse files
committed
Merge branch 'dev3x'
2 parents 7bd60a4 + 8c34fb6 commit 576c857

15 files changed

+639
-147
lines changed

.github/ISSUE_TEMPLATE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
| Q | A
1010
| ---------------- | ---
11-
| Mailer version | 2.?
11+
| Mailer version | 3.0.?
12+
| Mailer transport | `sendmail`, `smtp`, etc
1213
| PHP version | 5.?
1314
| Operating system |
1415

.github/PULL_REQUEST_TEMPLATE.md

-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22
| ------------- | ---
33
| Is bugfix? | yes/no
44
| New feature? | yes/no
5-
| Breaks BC? | yes/no
6-
| Tests pass? | yes/no
75
| Fixed issues | comma-separated list of tickets # fixed by the PR, if any

README.md

+237-27
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,271 @@ 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+
* Step 1. Initialization transport
29+
* --------------------------------
2830
*/
29-
$mailer = new \Ddrv\Mailer\Mailer();
3031
3132
/*
32-
* If need use SMTP server, setting it
33+
* a. Sendmail
34+
*/
35+
$transport = new \Ddrv\Mailer\Transport\Sendmail(
36+
'[email protected]', // sender
37+
'-f' // sendmail options
38+
);
39+
40+
/*
41+
* b. SMTP
3342
*/
34-
$mailer->smtp(
35-
'smtp.host.name', // host
36-
25, // port
37-
'[email protected]', // login
38-
'password for from', // password
39-
'[email protected]', // sender
40-
null, // encryption: 'tls', 'ssl' or null
41-
'http://host.name' // domain
43+
$transport = new \Ddrv\Mailer\Transport\Smtp(
44+
'smtp.fight.club', // host
45+
25, // port
46+
'joe', // login
47+
'IAmJoesLiver', // password
48+
'[email protected]', // sender
49+
null, // encryption: 'tls', 'ssl' or null
50+
'http://fight.club' // domain
4251
);
4352
4453
/*
45-
* If need switch provider back to mail() function, use
54+
* c. Fake (emulation send emails)
55+
*/
56+
57+
$transport = new \Ddrv\Mailer\Transport\Fake();
58+
59+
/*
60+
* d. Other. You can implement Ddrv\Mailer\Transport\TransportInterface interface
61+
*/
62+
63+
/*
64+
* Step 2. Initialization Mailer
65+
* -----------------------------
66+
*/
67+
$mailer = new \Ddrv\Mailer\Mailer($transport);
68+
69+
/*
70+
* Step 3. Create message
71+
* ----------------------
72+
*/
73+
74+
$text = <<<HTML
75+
<h1>Welcome to Fight Club</h1>
76+
<p>Please, read our rules in attachments</p>
77+
HTML;
78+
79+
80+
$message = new \Ddrv\Mailer\Message(
81+
'Fight Club', // subject of message
82+
$text, // text of message
83+
true // true for html, false for plain text
84+
);
85+
86+
/*
87+
* Step 4. Attachments
88+
* -------------------
89+
*/
90+
91+
/*
92+
* a. Creating attachment from string
93+
*/
94+
$rules = <<<TEXT
95+
1. You don't talk about fight club.
96+
2. You don't talk about fight club.
97+
3. When someone says stop, or goes limp, the fight is over.
98+
4. Only two guys to a fight.
99+
5. One fight at a time.
100+
6. They fight without shirts or shoes.
101+
7. The fights go on as long as they have to.
102+
8. If this is your first night at fight club, you have to fight.
103+
TEXT;
104+
105+
$message->attachFromString(
106+
'fight-club.txt', // attachment name
107+
$rules, // content
108+
'text/plain' // content-type
109+
);
110+
111+
/*
112+
* b. Creating attachments from file
113+
*/
114+
115+
$path = '/home/tyler/docs/projects/mayhem/rules.txt';
116+
117+
$message->attachFromFile(
118+
'project-mayhem.txt', // attachment name
119+
$path // path to attached file
120+
);
121+
122+
/*
123+
* Step 5. Add contacts names (OPTIONAL)
124+
*/
125+
126+
$mailer->addContact('[email protected]', 'Tyler Durden');
127+
$mailer->addContact('[email protected]', 'Angel Face');
128+
$mailer->addContact('[email protected]', 'Robert Paulson');
129+
130+
/*
131+
* Step 6. Send mail
132+
* -----------------
46133
*/
47-
$mailer->legacy('-f');
48134
49135
/*
50-
* Create message
136+
* a. Personal mailing (one mail per address)
51137
*/
52-
$message = new \Ddrv\Mailer\Message('[email protected]', 'subject', '<p>Simple text</p>', true);
138+
139+
$mailer->send(
140+
$message,
141+
array(
142+
143+
144+
145+
)
146+
);
53147
54148
/*
55-
* You can set named sender [email protected] as Site Administrator
149+
* b. Mass mailing (one mail to all addresses)
56150
*/
57-
$message->setSender('[email protected]', 'Site Administrator');
151+
152+
$mailer->mass(
153+
$message,
154+
array('[email protected]'), // recipients
155+
array('[email protected]'), // CC (carbon copy)
156+
array('[email protected]') // BCC (blind carbon copy)
157+
);
158+
```
159+
160+
# Channels
161+
162+
You can add some channels for sending.
163+
164+
```php
165+
<?php
166+
167+
$default = new \Ddrv\Mailer\Transport\Sendmail('[email protected]');
168+
169+
$noreply = new \Ddrv\Mailer\Transport\Smtp(
170+
'smtp.host.name',
171+
25,
172+
173+
'password',
174+
175+
'tls',
176+
'http://host.name'
177+
);
178+
179+
$support = new \Ddrv\Mailer\Transport\Smtp(
180+
'smtp.host.name',
181+
25,
182+
183+
'password',
184+
185+
null,
186+
'http://host.name'
187+
);
188+
189+
// channel name is \Ddrv\Mailer\Mailer::CHANNEL_DEFAULT.
190+
// You can define your channel name in second parameter
191+
// for example: $mailer = new \Ddrv\Mailer\Mailer($default, 'channel');
192+
$mailer = new \Ddrv\Mailer\Mailer($default);
193+
$mailer->setChannel($noreply, 'noreply');
194+
$mailer->setChannel($support, 'support');
195+
196+
$mailer->addContact('[email protected]', 'Informer');
197+
$mailer->addContact('[email protected]', 'Support Agent');
198+
199+
$msg1 = new \Ddrv\Mailer\Message(
200+
'host.name: your account registered',
201+
'Your account registered! Please do not reply to this email',
202+
false
203+
);
204+
205+
$msg2 = new \Ddrv\Mailer\Message(
206+
'host.name: ticket #4221 closed',
207+
'<p>Ticket #4221 closed</p>',
208+
true
209+
);
210+
211+
$mailer->addContact('[email protected]', 'Recipient First');
212+
$mailer->addContact('[email protected]', 'Recipient Second');
213+
$mailer->addContact('[email protected]', 'Other Recipient');
214+
215+
$recipients1 = array(
216+
217+
218+
);
219+
$recipients2 = array(
220+
221+
222+
);
58223

59224
/*
60-
* If need adding attachment from string, run
225+
* Send to channel
226+
* -----------------------
61227
*/
62-
$message->attachFromString('attach1.txt', 'content', 'text/plain');
228+
$mailer->send(
229+
$msg1, // message
230+
$recipients1, // recipients
231+
'noreply' // channel name
232+
);
233+
234+
$mailer->send(
235+
$msg2, // message
236+
$recipients2, // recipients
237+
'support' // channel name
238+
);
63239

64240
/*
65-
* If need adding attachment from file, run
241+
* Send to some channels
66242
*/
67-
$message->attachFromFile('attach2.txt', '/path/to/file');
243+
$mailer->send(
244+
$msg2, // message
245+
$recipients2, // recipients
246+
array('support', 'noreply') // channels
247+
);
68248

69249
/*
70-
* Send email to addresses (one mail for all addresses)
250+
* Send to all channels
71251
*/
72-
$mailer->send($message, array('[email protected]', '[email protected]'));
252+
$mailer->send($msg2, $recipients2, \Ddrv\Mailer\Mailer::CHANNEL_ALL);
73253

74254
/*
75-
* or send personal mailing one mail per addresses
255+
* CAUTION!
256+
* If the channel does not exists, the call well be skipped
76257
*/
77-
$mailer->send($message, array('[email protected]', '[email protected]', true));
78-
```
258+
259+
// If you need clear memory, you may clear contacts
260+
261+
$mailer->clearContacts();
262+
263+
```
264+
265+
# Logging
266+
267+
```php
268+
<?php
269+
270+
$support = new \Ddrv\Mailer\Transport\Sendmail('[email protected]');
271+
$noreply = new \Ddrv\Mailer\Transport\Sendmail('[email protected]');
272+
$default = new \Ddrv\Mailer\Transport\Sendmail('[email protected]');
273+
$mailer = new \Ddrv\Mailer\Mailer($support, 'support');
274+
$mailer->setChannel($noreply, 'noreply');
275+
$mailer->setChannel($default, 'default');
276+
277+
/**
278+
* @var Psr\Log\LoggerInterface $logger
279+
*/
280+
281+
$mailer->setLogger(
282+
function ($log) use ($logger) {
283+
$logger->info($log);
284+
},
285+
array('noreply', 'support') // channels
286+
);
287+
288+
```

composer.json

+2-2
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
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Exception;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class ChannelCantBeRemovedException extends Exception
9+
{
10+
11+
public function __construct($name, Throwable $previous = null)
12+
{
13+
parent::__construct("channel $name can't be removed", 2, $previous);
14+
}
15+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Exception;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class ChannelIsExistsException extends Exception
9+
{
10+
11+
public function __construct($name, Throwable $previous = null)
12+
{
13+
parent::__construct("channel $name already exists", 1, $previous);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer\Exception;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class RecipientsListEmptyException extends Exception
9+
{
10+
11+
public function __construct(Throwable $previous = null)
12+
{
13+
parent::__construct("recipients list is empty", 4, $previous);
14+
}
15+
}

0 commit comments

Comments
 (0)