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,184 @@ 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+ * Initialization Mailer class with SMTP transport
2829 */
29- $mailer = new \Ddrv\Mailer\Mailer();
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+ )
41+ );
3042
3143/*
32- * If need use SMTP server, setting it
44+ * Initialization Mailer class with sendmail transport
3345 */
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
46+ $mailer = new \Ddrv\Mailer\Mailer(
47+ 'sendmail',
48+ array(
49+ 'options' => '-f', // sendmail options
50+ )
4251);
4352
44- /*
45- * If need switch provider back to mail() function, use
46- */
47- $mailer->legacy('-f');
4853
4954/*
5055 * Create message
5156 */
52- $message = new \Ddrv\Mailer\Message('from@host.name ', 'subject', '<p>Simple text</p>', true );
57+ $sender = new \Ddrv\Mailer\Address('joe@fight.club ', 'Incognito' );
5358
54- /*
55- * You can set named sender from@host.name as Site Administrator
56- */
57- $message->setSender('from@host.name', 'Site Administrator');
59+ $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
64+ );
5865
5966/*
6067 * If need adding attachment from string, run
6168 */
62- $message->attachFromString('attach1.txt', 'content', 'text/plain');
69+ $rules = <<<TEXT
70+ 1. You don't talk about fight club.
71+ 2. You don't talk about fight club.
72+ 3. When someone says stop, or goes limp, the fight is over.
73+ 4. Only two guys to a fight.
74+ 5. One fight at a time.
75+ 6. They fight without shirts or shoes.
76+ 7. The fights go on as long as they have to.
77+ 8. If this is your first night at fight club, you have to fight.
78+ TEXT;
79+
80+ $message->attachFromString(
81+ 'fight-club-rules.txt', // attachment name
82+ $rules, // content
83+ 'text/plain' // content-type
84+ );
6385
6486/*
6587 * If need adding attachment from file, run
6688 */
67- $message->attachFromFile('attach2.txt', '/path/to/file');
89+ $message->attachFromFile(
90+ 'project-mayhem-rules.txt', // attachment name
91+ '/home/tyler/docs/projects/mayhem/rules.txt' // path to attached file
92+ );
6893
6994/*
70- * Send email to addresses (one mail for all addresses)
95+ * Create recipients
7196 */
72- $mailer->send($message, array('email1@host.name', 'email2@host.name'));
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'));
101+
73102
74103/*
75- * or send personal mailing one mail per addresses
104+ * Send email to addresses
76105 */
77- $mailer->send($message, array('email1@host.name', 'email2@host.name', true));
78- ```
106+ $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)
110+ );
111+ ```
112+
113+ # Channels
114+
115+ You can add some channels for sending.
116+
117+ ``` php
118+ <?php
119+
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+ )
132+ );
133+
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+ )
147+ );
148+
149+ $sender1 = new \Ddrv\Mailer\Address('no-reply@host.name', 'Informer');
150+ $msg1 = new \Ddrv\Mailer\Message(
151+ $sender1,
152+ 'host.name: your account registered',
153+ 'Your account registered! Please do not reply to this email',
154+ false
155+ );
156+
157+ $sender2 = new \Ddrv\Mailer\Address('support@host.name', 'Support Agent');
158+ $msg2 = new \Ddrv\Mailer\Message(
159+ $sender2,
160+ 'host.name: ticket #4221 closed',
161+ '<p >Ticket #4221 closed</p >',
162+ true
163+ );
164+
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'));
171+
172+ $mailer->send($msg1, $rcpt1, true); // send to default channel
173+ $mailer->send($msg2, $rcpt2, true, 'support'); // send to support channel
174+ ```
175+
176+ # CC and BCC
177+
178+ ``` php
179+
180+ <?php
181+
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');
190+
191+ $msg->addBCC('bcc1@host.name', 'User Name');
192+ $msg->addBCC('bcc2@host.name');
193+
194+ $rcpt = new \Ddrv\Mailer\Book();
195+ $rcpt->add(new \Ddrv\Mailer\Address('recipient@host.name', 'Recipient'));
196+
197+ $mailer->send($msg, $rcpt);
198+
199+ ```
200+
201+
0 commit comments