@@ -25,46 +25,71 @@ PHP library for sending email.
2525<?php
2626
2727/*
28- * Initialization Mailer class with SMTP transport
28+ * Step 1. Initialization transport
29+ * --------------------------------
2930 */
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- )
31+
32+ /*
33+ * a. Sendmail
34+ */
35+ $transport = new \Ddrv\Mailer\Transport\Sendmail(
36+ 'joe@fight.club', // sender
37+ '-f' // sendmail options
4138);
4239
4340/*
44- * Initialization Mailer class with sendmail transport
41+ * b. SMTP
4542 */
46- $mailer = new \Ddrv\Mailer\Mailer(
47- 'sendmail',
48- array(
49- 'options' => '-f', // sendmail options
50- )
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
5151);
5252
53+ /*
54+ * c. Fake (emulation send emails)
55+ */
56+
57+ $transport = new \Ddrv\Mailer\Transport\Fake();
5358
5459/*
55- * Create message
60+ * d. Other. You can implement Ddrv\Mailer\Transport\TransportInterface interface
5661 */
57- $sender = new \Ddrv\Mailer\Address('joe@fight.club', 'Incognito');
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+
5879
5980$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
81+ 'Fight Club', // subject of message
82+ $text, // text of message
83+ true // true for html, false for plain text
6484);
6585
6686/*
67- * If need adding attachment from string, run
87+ * Step 4. Attachments
88+ * -------------------
89+ */
90+
91+ /*
92+ * a. Creating attachment from string
6893 */
6994$rules = <<<TEXT
70951. You don't talk about fight club.
@@ -78,35 +103,57 @@ $rules = <<<TEXT
78103TEXT;
79104
80105$message->attachFromString(
81- 'fight-club-rules .txt', // attachment name
82- $rules, // content
83- 'text/plain' // content-type
106+ 'fight-club.txt', // attachment name
107+ $rules, // content
108+ 'text/plain' // content-type
84109);
85110
86111/*
87- * If need adding attachment from file, run
112+ * b. Creating attachments from file
88113 */
114+
115+ $path = '/home/tyler/docs/projects/mayhem/rules.txt';
116+
89117$message->attachFromFile(
90- 'project-mayhem-rules .txt', // attachment name
91- '/home/tyler/docs/projects/mayhem/rules.txt' // path to attached file
118+ 'project-mayhem.txt', // attachment name
119+ $path // path to attached file
92120);
93121
94122/*
95- * Create recipients
123+ * Step 5. Add contacts names (OPTIONAL)
96124 */
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'));
101125
126+ $mailer->addContact('tyler@fight.club', 'Tyler Durden');
127+ $mailer->addContact('angel@fight.club', 'Angel Face');
128+ $mailer->addContact('bob@fight.club', 'Robert Paulson');
102129
103130/*
104- * Send email to addresses
131+ * Step 6. Send mail
132+ * -----------------
105133 */
134+
135+ /*
136+ * a. Personal mailing (one mail per address)
137+ */
138+
106139$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)
140+ $message,
141+ array(
142+ 'tyler@fight.club',
143+ 'angel@fight.club',
144+ 'bob@fight.club',
145+ )
146+ );
147+
148+ /*
149+ * b. Mass mailing (one mail to all addresses)
150+ */
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)
110157);
111158```
112159
@@ -117,85 +164,125 @@ You can add some channels for sending.
117164``` php
118165<?php
119166
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- )
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'
132177);
133178
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- )
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'
147187);
148188
149- $sender1 = new \Ddrv\Mailer\Address('no-reply@host.name', 'Informer');
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+
150199$msg1 = new \Ddrv\Mailer\Message(
151- $sender1,
152200 'host.name: your account registered',
153201 'Your account registered! Please do not reply to this email',
154202 false
155203);
156204
157- $sender2 = new \Ddrv\Mailer\Address('support@host.name', 'Support Agent');
158205$msg2 = new \Ddrv\Mailer\Message(
159- $sender2,
160206 'host.name: ticket #4221 closed',
161207 '<p >Ticket #4221 closed</p >',
162208 true
163209);
164210
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'));
211+ $mailer->addContact('recipient1@host.name', 'Recipient First');
212+ $mailer->addContact('recipient2@host.name', 'Recipient Second');
213+ $mailer->addContact('recipient3@host.name', 'Other Recipient');
171214
172- $mailer->send($msg1, $rcpt1, true); // send to default channel
173- $mailer->send($msg2, $rcpt2, true, 'support'); // send to support channel
174- ```
215+ $recipients1 = array(
216+ 'recipient1@host.name',
217+ 'recipient2@host.name'
218+ );
219+ $recipients2 = array(
220+ 'recipient2@host.name',
221+ 'recipient3@host.name'
222+ );
175223
176- # CC and BCC
224+ /*
225+ * Send to channel
226+ * -----------------------
227+ */
228+ $mailer->send(
229+ $msg1, // message
230+ $recipients1, // recipients
231+ 'noreply' // channel name
232+ );
177233
178- ``` php
234+ $mailer->send(
235+ $msg2, // message
236+ $recipients2, // recipients
237+ 'support' // channel name
238+ );
179239
180- <?php
240+ /*
241+ * Send to some channels
242+ */
243+ $mailer->send(
244+ $msg2, // message
245+ $recipients2, // recipients
246+ array('support', 'noreply') // channels
247+ );
181248
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');
249+ /*
250+ * Send to all channels
251+ */
252+ $mailer->send($msg2, $recipients2, \Ddrv\Mailer\Mailer::CHANNEL_ALL);
190253
191- $msg->addBCC('bcc1@host.name', 'User Name');
192- $msg->addBCC('bcc2@host.name');
254+ /*
255+ * CAUTION!
256+ * If the channel does not exists, the call well be skipped
257+ */
193258
194- $rcpt = new \Ddrv\Mailer\Book();
195- $rcpt->add(new \Ddrv\Mailer\Address('recipient@host.name', 'Recipient'));
259+ // If you need clear memory, you may clear contacts
196260
197- $mailer->send($msg, $rcpt );
261+ $mailer->clearContacts( );
198262
199263```
200264
265+ # Logging
201266
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