@@ -11,7 +11,7 @@ PHP library for sending email.
1111## With [ Composer] ( https://getcomposer.org/ )
12121 . Run in console:
1313 ``` text
14- php composer.phar require ddrv/mailer:~3
14+ php composer.phar require ddrv/mailer:~4.0
1515 ```
16161. Include autoload file
1717 ```php
@@ -24,6 +24,14 @@ PHP library for sending email.
2424```php
2525<?php
2626
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+
2735/*
2836 * Step 1. Initialization transport
2937 * --------------------------------
@@ -32,15 +40,14 @@ PHP library for sending email.
3240/*
3341 * a. Sendmail
3442 */
35- $transport = new \Ddrv\Mailer\Transport\Sendmail(
36- 'joe@fight.club', // sender
43+ $transport = new SendmailTransport(
3744 '-f' // sendmail options
3845);
3946
4047/*
4148 * b. SMTP
4249 */
43- $transport = new \Ddrv\Mailer\Transport\Smtp (
50+ $transport = new SmtpTransport (
4451 'smtp.fight.club', // host
4552 25, // port
4653 'joe', // login
@@ -54,37 +61,72 @@ $transport = new \Ddrv\Mailer\Transport\Smtp(
5461 * c. Fake (emulation send emails)
5562 */
5663
57- $transport = new \Ddrv\Mailer\Transport\Fake ();
64+ $transport = new FakeTransport ();
5865
5966/*
6067 * d. Other. You can implement Ddrv\Mailer\Transport\TransportInterface interface
6168 */
6269
6370/*
64- * Step 2. Initialization Mailer
71+ * Step 2. Initialization spool
6572 * -----------------------------
6673 */
67- $mailer = new \Ddrv\Mailer\Mailer($transport);
6874
6975/*
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 <info@fight.club>");
97+
98+ /*
99+ * Step 4. Create message
71100 * ----------------------
72101 */
102+
73103
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('recipient@example.com', 'My Friend');
110+
111+ $html = <<<HTML
75112<h1>Welcome to Fight Club</h1>
76113<p>Please, read our rules in attachments</p>
77114HTML;
78-
79115
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+ ;
85127
86128/*
87- * Step 4 . Attachments
129+ * Step 5 . Attachments
88130 * -------------------
89131 */
90132
@@ -102,7 +144,7 @@ $rules = <<<TEXT
1021448. If this is your first night at fight club, you have to fight.
103145TEXT;
104146
105- $message ->attachFromString(
147+ $message2 ->attachFromString(
106148 'fight-club.txt', // attachment name
107149 $rules, // content
108150 'text/plain' // content-type
@@ -111,178 +153,52 @@ $message->attachFromString(
111153/*
112154 * b. Creating attachments from file
113155 */
114-
115156$path = '/home/tyler/docs/projects/mayhem/rules.txt';
116157
117- $message ->attachFromFile(
158+ $message2 ->attachFromFile(
118159 'project-mayhem.txt', // attachment name
119160 $path // path to attached file
120161);
121162
122163/*
123- * Step 5 . Add contacts names (OPTIONAL)
164+ * Step 6 . Add recipients
124165 */
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');
166+ $message2->addTo('tyler@fight.club', 'Tyler Durden');
167+ $message2->addCc('bob@fight.club', 'Robert Paulson');
168+ $message2->addBcc('angel@fight.club', 'Angel Face');
129169
130170/*
131- * Step 6 . Send mail
171+ * Step 7 . Send mail
132172 * -----------------
133173 */
134174
135175/*
136- * a. Personal mailing (one mail per address)
176+ * a. Simple send to all recipients
137177 */
138-
139- $mailer->send(
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)
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- );
178+ $mailer->send($message1);
223179
224180/*
225- * Send to channel
226- * -----------------------
181+ * b. Spooling
182+ * For add message to spool, you need set second parameter as positive integer
227183 */
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);
239186
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();
248189
249190/*
250- * Send to all channels
191+ * You can set limit for send messages from spool
251192 */
252- $mailer->send($msg2, $recipients2, \Ddrv\Mailer\Mailer::CHANNEL_ALL);
193+ $mailer->flush(5);
253194
254195/*
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).
257199 */
200+ $mailer->personal($message2); // without spool
201+ // or
202+ $mailer->personal($message2, 1); // with spool
203+ $mailer->flush();
258204
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