@@ -11,7 +11,7 @@ PHP library for sending email.
11
11
## With [ Composer] ( https://getcomposer.org/ )
12
12
1 . Run in console:
13
13
``` text
14
- php composer.phar require ddrv/mailer:~3
14
+ php composer.phar require ddrv/mailer:~4.0
15
15
```
16
16
1. Include autoload file
17
17
```php
@@ -24,6 +24,14 @@ PHP library for sending email.
24
24
```php
25
25
<?php
26
26
27
+ use \Ddrv\Mailer\Transport\SendmailTransport;
28
+ use \Ddrv\Mailer\Transport\SmtpTransport;
29
+ use \Ddrv\Mailer\Transport\FakeTransport;
30
+ use \Ddrv\Mailer\Spool\MemorySpool;
31
+ use \Ddrv\Mailer\Spool\FileSpool;
32
+ use \Ddrv\Mailer\Mailer;
33
+ use \Ddrv\Mailer\Message;
34
+
27
35
/*
28
36
* Step 1. Initialization transport
29
37
* --------------------------------
@@ -32,15 +40,14 @@ PHP library for sending email.
32
40
/*
33
41
* a. Sendmail
34
42
*/
35
- $transport = new \Ddrv\Mailer\Transport\Sendmail(
36
-
43
+ $transport = new SendmailTransport(
37
44
'-f' // sendmail options
38
45
);
39
46
40
47
/*
41
48
* b. SMTP
42
49
*/
43
- $transport = new \Ddrv\Mailer\Transport\Smtp (
50
+ $transport = new SmtpTransport (
44
51
'smtp.fight.club', // host
45
52
25, // port
46
53
'joe', // login
@@ -54,37 +61,72 @@ $transport = new \Ddrv\Mailer\Transport\Smtp(
54
61
* c. Fake (emulation send emails)
55
62
*/
56
63
57
- $transport = new \Ddrv\Mailer\Transport\Fake ();
64
+ $transport = new FakeTransport ();
58
65
59
66
/*
60
67
* d. Other. You can implement Ddrv\Mailer\Transport\TransportInterface interface
61
68
*/
62
69
63
70
/*
64
- * Step 2. Initialization Mailer
71
+ * Step 2. Initialization spool
65
72
* -----------------------------
66
73
*/
67
- $mailer = new \Ddrv\Mailer\Mailer($transport);
68
74
69
75
/*
70
- * Step 3. Create message
76
+ * a. File spool.
77
+ */
78
+ $spool = new FileSpool($transport, sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mail');
79
+
80
+ /*
81
+ * b. Memory spool.
82
+ */
83
+ $spool = new MemorySpool($transport);
84
+
85
+ /*
86
+ * c. Other. You can implement Ddrv\Mailer\SpoolInterface interface
87
+ */
88
+
89
+ /*
90
+ * Step 3. Initialization Mailer
91
+ * -----------------------------
92
+ */
93
+ $mailer = new Mailer($spool);
94
+
95
+ // If you need a replace header "From" in all messages, set your sender in second parameter
96
+ $mailer = new Mailer($spool, "Fight Club Informer <[email protected] >");
97
+
98
+ /*
99
+ * Step 4. Create message
71
100
* ----------------------
72
101
*/
102
+
73
103
74
- $text = <<<HTML
104
+ $message1 = new Message(
105
+ 'Test message', // subject of message
106
+ '<h1>Hello, world!</h1>', // html body
107
+ 'Hello, world!' // plain body
108
+ );
109
+ $message1->addTo('[email protected] ', 'My Friend');
110
+
111
+ $html = <<<HTML
75
112
<h1>Welcome to Fight Club</h1>
76
113
<p>Please, read our rules in attachments</p>
77
114
HTML;
78
-
79
115
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
- );
116
+ $text = <<<TEXT
117
+ Welcome to Fight Club
118
+ Please, read our rules in attachments
119
+ TEXT;
120
+
121
+ $message2 = new Message();
122
+ $message2
123
+ ->setSubject('Fight Club')
124
+ ->setHtmlBody($html)
125
+ ->setPlainBody($text)
126
+ ;
85
127
86
128
/*
87
- * Step 4 . Attachments
129
+ * Step 5 . Attachments
88
130
* -------------------
89
131
*/
90
132
@@ -102,7 +144,7 @@ $rules = <<<TEXT
102
144
8. If this is your first night at fight club, you have to fight.
103
145
TEXT;
104
146
105
- $message ->attachFromString(
147
+ $message2 ->attachFromString(
106
148
'fight-club.txt', // attachment name
107
149
$rules, // content
108
150
'text/plain' // content-type
@@ -111,178 +153,52 @@ $message->attachFromString(
111
153
/*
112
154
* b. Creating attachments from file
113
155
*/
114
-
115
156
$path = '/home/tyler/docs/projects/mayhem/rules.txt';
116
157
117
- $message ->attachFromFile(
158
+ $message2 ->attachFromFile(
118
159
'project-mayhem.txt', // attachment name
119
160
$path // path to attached file
120
161
);
121
162
122
163
/*
123
- * Step 5 . Add contacts names (OPTIONAL)
164
+ * Step 6 . Add recipients
124
165
*/
125
-
126
- $mailer->addContact('[email protected] ', 'Tyler Durden');
127
- $mailer->addContact('[email protected] ', 'Angel Face');
128
- $mailer->addContact('[email protected] ', 'Robert Paulson');
166
+ $message2->addTo('[email protected] ', 'Tyler Durden');
167
+ $message2->addCc('[email protected] ', 'Robert Paulson');
168
+ $message2->addBcc('[email protected] ', 'Angel Face');
129
169
130
170
/*
131
- * Step 6 . Send mail
171
+ * Step 7 . Send mail
132
172
* -----------------
133
173
*/
134
174
135
175
/*
136
- * a. Personal mailing (one mail per address)
176
+ * a. Simple send to all recipients
137
177
*/
138
-
139
- $mailer->send(
140
- $message,
141
- array(
142
-
143
-
144
-
145
- )
146
- );
147
-
148
- /*
149
- * b. Mass mailing (one mail to all addresses)
150
- */
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
- );
178
+ $mailer->send($message1);
223
179
224
180
/*
225
- * Send to channel
226
- * -----------------------
181
+ * b. Spooling
182
+ * For add message to spool, you need set second parameter as positive integer
227
183
*/
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
- );
184
+ $mailer->send($message1, 2);
185
+ $mailer->send($message2, 1);
239
186
240
- /*
241
- * Send to some channels
242
- */
243
- $mailer->send(
244
- $msg2, // message
245
- $recipients2, // recipients
246
- array('support', 'noreply') // channels
247
- );
187
+ // Send from spool.
188
+ $mailer->flush();
248
189
249
190
/*
250
- * Send to all channels
191
+ * You can set limit for send messages from spool
251
192
*/
252
- $mailer->send($msg2, $recipients2, \Ddrv\Mailer\Mailer::CHANNEL_ALL);
193
+ $mailer->flush(5);
253
194
254
195
/*
255
- * CAUTION!
256
- * If the channel does not exists, the call well be skipped
196
+ * Step 8. Personal mailing
197
+ * You can send one message with many recipient as many mails per recipient
198
+ * (messages will be without CC fnd BCC headers and include only one email on To header).
257
199
*/
200
+ $mailer->personal($message2); // without spool
201
+ // or
202
+ $mailer->personal($message2, 1); // with spool
203
+ $mailer->flush();
258
204
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
- ```
0 commit comments