5
5
6
6
7
7
# Mailer
8
- PHP Class for sending email.
8
+ PHP library for sending email.
9
9
10
10
# Install
11
11
## With [ Composer] ( https://getcomposer.org/ )
@@ -18,61 +18,271 @@ PHP Class for sending email.
18
18
require_once('vendor/autoload.php');
19
19
```
20
20
21
+
21
22
# Usage
22
23
23
24
```php
24
25
<?php
25
26
26
27
/*
27
- * Initialization Mailer class.
28
+ * Step 1. Initialization transport
29
+ * --------------------------------
28
30
*/
29
- $mailer = new \Ddrv\Mailer\Mailer();
30
31
31
32
/*
32
- * If need use SMTP server, setting it
33
+ * a. Sendmail
34
+ */
35
+ $transport = new \Ddrv\Mailer\Transport\Sendmail(
36
+
37
+ '-f' // sendmail options
38
+ );
39
+
40
+ /*
41
+ * b. SMTP
33
42
*/
34
- $mailer->smtp (
35
- 'smtp.host.name', // host
36
- 25, // port
37
-
38
- 'password for from', // password
39
-
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
+
49
+ null, // encryption: 'tls', 'ssl' or null
50
+ 'http://fight.club' // domain
42
51
);
43
52
44
53
/*
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
+ * -----------------
46
133
*/
47
- $mailer->legacy('-f');
48
134
49
135
/*
50
- * Create message
136
+ * a. Personal mailing (one mail per address)
51
137
*/
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
+ );
53
147
54
148
/*
55
- * You can set named sender [email protected] as Site Administrator
149
+ * b. Mass mailing (one mail to all addresses)
56
150
*/
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
+ );
58
223
59
224
/*
60
- * If need adding attachment from string, run
225
+ * Send to channel
226
+ * -----------------------
61
227
*/
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
+ );
63
239
64
240
/*
65
- * If need adding attachment from file, run
241
+ * Send to some channels
66
242
*/
67
- $message->attachFromFile('attach2.txt', '/path/to/file');
243
+ $mailer->send(
244
+ $msg2, // message
245
+ $recipients2, // recipients
246
+ array('support', 'noreply') // channels
247
+ );
68
248
69
249
/*
70
- * Send email to addresses (one mail for all addresses)
250
+ * Send to all channels
71
251
*/
72
- $mailer->send($message, array('[email protected] ', '[email protected] '));
252
+ $mailer->send($msg2, $recipients2, \Ddrv\Mailer\Mailer::CHANNEL_ALL);
73
253
74
254
/*
75
- * or send personal mailing one mail per addresses
255
+ * CAUTION!
256
+ * If the channel does not exists, the call well be skipped
76
257
*/
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
+ ```
0 commit comments