blob: 86f440a74127a8fbdf9e4246733b75a62519e21e [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2Email Class
3###########
4
5CodeIgniter's robust Email Class supports the following features:
6
7- Multiple Protocols: Mail, Sendmail, and SMTP
8- TLS and SSL Encryption for SMTP
9- Multiple recipients
10- CC and BCCs
11- HTML or Plaintext email
12- Attachments
13- Word wrapping
14- Priorities
15- BCC Batch Mode, enabling large email lists to be broken into small
16 BCC batches.
17- Email Debugging tools
18
19Sending Email
20=============
21
22Sending email is not only simple, but you can configure it on the fly or
23set your preferences in a config file.
24
25Here is a basic example demonstrating how you might send email. Note:
26This example assumes you are sending the email from one of your
27:doc:`controllers <../general/controllers>`.
28
29::
30
Derek Jones3c356842011-10-05 16:14:23 -050031 $this->load->library('email');
32
33 $this->email->from('your@example.com', 'Your Name');
34 $this->email->to('someone@example.com');
35 $this->email->cc('another@another-example.com');
36 $this->email->bcc('them@their-example.com');
37
38 $this->email->subject('Email Test');
39 $this->email->message('Testing the email class.');
40
41 $this->email->send();
42
Derek Jones8ede1a22011-10-05 13:34:52 -050043Setting Email Preferences
44=========================
45
Darren Benney77130812013-03-26 02:22:35 +000046There are 21 different preferences available to tailor how your email
Derek Jones8ede1a22011-10-05 13:34:52 -050047messages are sent. You can either set them manually as described here,
48or automatically via preferences stored in your config file, described
49below:
50
51Preferences are set by passing an array of preference values to the
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020052email initialize method. Here is an example of how you might set some
Derek Jones8ede1a22011-10-05 13:34:52 -050053preferences::
54
Derek Jones3c356842011-10-05 16:14:23 -050055 $config['protocol'] = 'sendmail';
56 $config['mailpath'] = '/usr/sbin/sendmail';
57 $config['charset'] = 'iso-8859-1';
58 $config['wordwrap'] = TRUE;
59
60 $this->email->initialize($config);
Derek Jones8ede1a22011-10-05 13:34:52 -050061
62.. note:: Most of the preferences have default values that will be used
63 if you do not set them.
64
65Setting Email Preferences in a Config File
66------------------------------------------
67
68If you prefer not to set preferences using the above method, you can
69instead put them into a config file. Simply create a new file called the
70email.php, add the $config array in that file. Then save the file at
71config/email.php and it will be used automatically. You will NOT need to
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020072use the ``$this->email->initialize()`` method if you save your
73preferences in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75Email Preferences
76=================
77
78The following is a list of all the preferences that can be set when
79sending email.
80
Joseph Wensleyd14717f2011-10-05 23:57:14 -040081=================== ====================== ============================ =======================================================================
82Preference Default Value Options Description
83=================== ====================== ============================ =======================================================================
84**useragent** CodeIgniter None The "user agent".
85**protocol** mail mail, sendmail, or smtp The mail sending protocol.
86**mailpath** /usr/sbin/sendmail None The server path to Sendmail.
87**smtp_host** No Default None SMTP Server Address.
88**smtp_user** No Default None SMTP Username.
89**smtp_pass** No Default None SMTP Password.
90**smtp_port** 25 None SMTP Port.
91**smtp_timeout** 5 None SMTP Timeout (in seconds).
nisheeth-barthwal9ecde432013-02-18 17:06:12 +053092**smtp_keepalive** FALSE TRUE or FALSE (boolean) Enable persistent SMTP connections.
Joseph Wensleyd14717f2011-10-05 23:57:14 -040093**smtp_crypto** No Default tls or ssl SMTP Encryption
94**wordwrap** TRUE TRUE or FALSE (boolean) Enable word-wrap.
95**wrapchars** 76 Character count to wrap at.
96**mailtype** text text or html Type of mail. If you send HTML email you must send it as a complete web
97 page. Make sure you don't have any relative links or relative image
98 paths otherwise they will not work.
Andrey Andreev6d9915a2012-10-10 16:18:33 +030099**charset** ``$config['charset']`` Character set (utf-8, iso-8859-1, etc.).
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400100**validate** FALSE TRUE or FALSE (boolean) Whether to validate the email address.
101**priority** 3 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal.
102**crlf** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
103**newline** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
104**bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode.
105**bcc_batch_size** 200 None Number of emails in each BCC batch.
leandronfbe07c922012-03-22 19:49:23 -0300106**dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400107=================== ====================== ============================ =======================================================================
108
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200109Email Methods Reference
110=======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
112$this->email->from()
113--------------------
114
115Sets the email address and name of the person sending the email::
116
117 $this->email->from('you@example.com', 'Your Name');
118
Andrey Andreevccd01c72012-10-05 17:12:55 +0300119You can also set a Return-Path, to help redirect undelivered mail::
Melounek58dfc082012-06-29 08:43:47 +0200120
121 $this->email->from('you@example.com', 'Your Name', 'returned_emails@example.com');
122
Andrey Andreevccd01c72012-10-05 17:12:55 +0300123.. note:: Return-Path can't be used if you've configured
124 'smtp' as your protocol.
Melounek58dfc082012-06-29 08:43:47 +0200125
Derek Jones8ede1a22011-10-05 13:34:52 -0500126$this->email->reply_to()
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200127------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129Sets the reply-to address. If the information is not provided the
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200130information in the "from" method is used. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
132 $this->email->reply_to('you@example.com', 'Your Name');
133
134$this->email->to()
135------------------
136
137Sets the email address(s) of the recipient(s). Can be a single email, a
138comma-delimited list or an array::
139
140 $this->email->to('someone@example.com');
141
142::
143
144 $this->email->to('one@example.com, two@example.com, three@example.com');
145
146::
147
Derek Jones3c356842011-10-05 16:14:23 -0500148 $list = array('one@example.com', 'two@example.com', 'three@example.com');
149
150 $this->email->to($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
152$this->email->cc()
153------------------
154
155Sets the CC email address(s). Just like the "to", can be a single email,
156a comma-delimited list or an array.
157
158$this->email->bcc()
159-------------------
160
161Sets the BCC email address(s). Just like the "to", can be a single
162email, a comma-delimited list or an array.
163
164$this->email->subject()
165-----------------------
166
167Sets the email subject::
168
169 $this->email->subject('This is my subject');
170
171$this->email->message()
172-----------------------
173
174Sets the email message body::
175
176 $this->email->message('This is my message');
177
178$this->email->set_alt_message()
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200179-------------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
181Sets the alternative email message body::
182
183 $this->email->set_alt_message('This is the alternative message');
184
185This is an optional message string which can be used if you send HTML
186formatted email. It lets you specify an alternative message with no HTML
187formatting which is added to the header string for people who do not
188accept HTML email. If you do not set your own message CodeIgniter will
189extract the message from your HTML email and strip the tags.
190
Mickey Wubfc1cad2012-05-31 22:28:40 -0700191$this->email->set_header()
Derek Jonesce79be02012-06-25 23:23:46 -0700192--------------------------
Mickey Wubfc1cad2012-05-31 22:28:40 -0700193
194Appends additional headers to the e-mail::
195
196 $this->email->set_header('Header1', 'Value1');
197 $this->email->set_header('Header2', 'Value2');
198
Derek Jones8ede1a22011-10-05 13:34:52 -0500199$this->email->clear()
200---------------------
201
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200202Initializes all the email variables to an empty state. This method is
203intended for use if you run the email sending method in a loop,
Derek Jones8ede1a22011-10-05 13:34:52 -0500204permitting the data to be reset between cycles.
205
206::
207
Derek Jones3c356842011-10-05 16:14:23 -0500208 foreach ($list as $name => $address)
209 {
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200210 $this->email->clear();
Derek Jones3c356842011-10-05 16:14:23 -0500211
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200212 $this->email->to($address);
213 $this->email->from('your@example.com');
214 $this->email->subject('Here is your info '.$name);
215 $this->email->message('Hi '.$name.' Here is the info you requested.');
216 $this->email->send();
Derek Jones3c356842011-10-05 16:14:23 -0500217 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
219If you set the parameter to TRUE any attachments will be cleared as
220well::
221
222 $this->email->clear(TRUE);
223
224$this->email->send()
225--------------------
226
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200227The Email sending method. Returns boolean TRUE or FALSE based on
Derek Jones8ede1a22011-10-05 13:34:52 -0500228success or failure, enabling it to be used conditionally::
229
Derek Jones3c356842011-10-05 16:14:23 -0500230 if ( ! $this->email->send())
231 {
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200232 // Generate error
Derek Jones3c356842011-10-05 16:14:23 -0500233 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500234
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200235This method will automatically clear all parameters if the request was
Andrey Andreevbdb99992012-07-30 17:38:05 +0300236successful. To stop this behaviour pass FALSE::
Alex Bilbiea473d802012-07-30 09:51:04 +0100237
238 if ($this->email->send(FALSE))
239 {
240 // Parameters won't be cleared
241 }
242
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200243.. note:: In order to use the ``print_debugger()`` method, you need
244 to avoid clearing the email parameters.
245
Derek Jones8ede1a22011-10-05 13:34:52 -0500246$this->email->attach()
247----------------------
248
249Enables you to send an attachment. Put the file path/name in the first
Petr Heralecky300e3f02014-01-10 11:49:11 +0100250parameter. For multiple attachments use the method multiple times.
251For example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
Derek Jones3c356842011-10-05 16:14:23 -0500253 $this->email->attach('/path/to/photo1.jpg');
254 $this->email->attach('/path/to/photo2.jpg');
255 $this->email->attach('/path/to/photo3.jpg');
256
Matteo Matteic3b36f42012-03-26 10:27:17 +0200257To use the default disposition (attachment), leave the second parameter blank,
258otherwise use a custom disposition::
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100259
Matteo Matteic3b36f42012-03-26 10:27:17 +0200260 $this->email->attach('image.jpg', 'inline');
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100261
Petr Heralecky63f07cb2014-01-10 16:50:49 +0100262You can use URL::
Petr Heralecky232ea652014-01-10 15:21:18 +0100263
264 $this->email->attach('http://example.com/filename.pdf');
265
Matteo Matteic3b36f42012-03-26 10:27:17 +0200266If you'd like to use a custom file name, you can use the third paramater::
Matteo Matteidf59c682012-03-15 16:03:58 +0100267
Matteo Matteic3b36f42012-03-26 10:27:17 +0200268 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
269
270If you need to use a buffer string instead of a real - physical - file you can
271use the first parameter as buffer, the third parameter as file name and the fourth
272parameter as mime-type::
273
274 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
Andrey Andreevc8097262014-01-10 14:45:31 +0200276$this->email->attachment_cid()
277------------------------------
Petr Heralecky300e3f02014-01-10 11:49:11 +0100278
Andrey Andreevc8097262014-01-10 14:45:31 +0200279Sets and returns an attachment's Content-ID, which enables your to embed an inline
280(picture) attachment into HTML. First parameter must be attached file.
Petr Heralecky300e3f02014-01-10 11:49:11 +0100281
Andrey Andreevc8097262014-01-10 14:45:31 +0200282::
Petr Heralecky300e3f02014-01-10 11:49:11 +0100283
Petr Heraleckyde886152014-01-10 12:52:56 +0100284 $filename = '/img/photo1.jpg';
285 $this->email->attach($filename);
286 foreach ($list as $address)
287 {
288 $this->email->to($address);
289 $cid = $this->email->attach_cid($filename);
290 $this->email->message('<img src='cid:". $cid ."' alt="photo1" />');
291 $this->email->send();
292 }
Andrey Andreevc8097262014-01-10 14:45:31 +0200293
294CID for each Email have to be create again to be unique.
Petr Heralecky300e3f02014-01-10 11:49:11 +0100295
Derek Jones8ede1a22011-10-05 13:34:52 -0500296$this->email->print_debugger()
Andrey Andreev61797f62012-11-26 16:15:12 +0200297------------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500298
299Returns a string containing any server messages, the email headers, and
300the email messsage. Useful for debugging.
301
Andrey Andreev61797f62012-11-26 16:15:12 +0200302You can optionally specify which parts of the message should be printed.
303Valid options are: **headers**, **subject**, **body**.
304
305Example::
306
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200307 // You need to pass FALSE while sending in order for the email data
308 // to not be cleared - if that happens, print_debugger() would have
309 // nothing to output.
310 $this->email->send(FALSE);
311
Andrey Andreev61797f62012-11-26 16:15:12 +0200312 // Will only print the email headers, excluding the message subject and body
313 $this->email->print_debugger(array('headers'));
314
315.. note:: By default, all of the raw data will be printed.
316
Derek Jones8ede1a22011-10-05 13:34:52 -0500317Overriding Word Wrapping
318========================
319
320If you have word wrapping enabled (recommended to comply with RFC 822)
321and you have a very long link in your email it can get wrapped too,
322causing it to become un-clickable by the person receiving it.
323CodeIgniter lets you manually override word wrapping within part of your
324message like this::
325
Derek Jones3c356842011-10-05 16:14:23 -0500326 The text of your email that
327 gets wrapped normally.
328
329 {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
330
331 More text that will be
332 wrapped normally.
333
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200335Place the item you do not want word-wrapped between: {unwrap} {/unwrap}