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+ 'joe@fight.club', // sender
37+ '-f' // sendmail options
38+ );
39+
40+ /*
41+ * b. SMTP
3342 */
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
43+ $transport = new \Ddrv\Mailer\Transport\Smtp (
44+ 'smtp.fight.club', // host
45+ 25, // port
46+ 'joe', // login
47+ 'IAmJoesLiver', // password
48+ 'joe@fight.club', // 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('tyler@fight.club', 'Tyler Durden');
127+ $mailer->addContact('angel@fight.club', 'Angel Face');
128+ $mailer->addContact('bob@fight.club', '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('from@host.name', 'subject', '<p>Simple text</p>', true);
138+
139+ $mailer->send(
140+ $message,
141+ array(
142+ 'tyler@fight.club',
143+ 'angel@fight.club',
144+ 'bob@fight.club',
145+ )
146+ );
53147
54148/*
55- * You can set named sender from@host.name as Site Administrator
149+ * b. Mass mailing (one mail to all addresses)
56150 */
57- $message->setSender('from@host.name', 'Site Administrator');
151+
152+ $mailer->mass(
153+ $message,
154+ array('tyler@fight.club'), // recipients
155+ array('angel@fight.club'), // CC (carbon copy)
156+ array('bob@fight.club') // 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('user@host.name');
168+
169+ $noreply = new \Ddrv\Mailer\Transport\Smtp(
170+ 'smtp.host.name',
171+ 25,
172+ 'no-reply@host.name',
173+ 'password',
174+ 'no-reply@host.name',
175+ 'tls',
176+ 'http://host.name'
177+ );
178+
179+ $support = new \Ddrv\Mailer\Transport\Smtp(
180+ 'smtp.host.name',
181+ 25,
182+ 'support@host.name',
183+ 'password',
184+ 'support@host.name',
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('no-reply@host.name', 'Informer');
197+ $mailer->addContact('support@host.name', '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('recipient1@host.name', 'Recipient First');
212+ $mailer->addContact('recipient2@host.name', 'Recipient Second');
213+ $mailer->addContact('recipient3@host.name', 'Other Recipient');
214+
215+ $recipients1 = array(
216+ 'recipient1@host.name',
217+ 'recipient2@host.name'
218+ );
219+ $recipients2 = array(
220+ 'recipient2@host.name',
221+ 'recipient3@host.name'
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('email1@host.name', 'email2@host.name'));
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('email1@host.name', 'email2@host.name', 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('support@host.name');
271+ $noreply = new \Ddrv\Mailer\Transport\Sendmail('noreply@host.name');
272+ $default = new \Ddrv\Mailer\Transport\Sendmail('default@host.name');
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