blob: 39629ece1663039f94a067143a0e898c34d1c71c [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
250parameter. Note: Use a file path, not a URL. For multiple attachments
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200251use the method multiple times. For 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
Matteo Matteic3b36f42012-03-26 10:27:17 +0200262If you'd like to use a custom file name, you can use the third paramater::
Matteo Matteidf59c682012-03-15 16:03:58 +0100263
Matteo Matteic3b36f42012-03-26 10:27:17 +0200264 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
265
266If you need to use a buffer string instead of a real - physical - file you can
267use the first parameter as buffer, the third parameter as file name and the fourth
268parameter as mime-type::
269
270 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
272$this->email->print_debugger()
Andrey Andreev61797f62012-11-26 16:15:12 +0200273------------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500274
275Returns a string containing any server messages, the email headers, and
276the email messsage. Useful for debugging.
277
Andrey Andreev61797f62012-11-26 16:15:12 +0200278You can optionally specify which parts of the message should be printed.
279Valid options are: **headers**, **subject**, **body**.
280
281Example::
282
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200283 // You need to pass FALSE while sending in order for the email data
284 // to not be cleared - if that happens, print_debugger() would have
285 // nothing to output.
286 $this->email->send(FALSE);
287
Andrey Andreev61797f62012-11-26 16:15:12 +0200288 // Will only print the email headers, excluding the message subject and body
289 $this->email->print_debugger(array('headers'));
290
291.. note:: By default, all of the raw data will be printed.
292
Derek Jones8ede1a22011-10-05 13:34:52 -0500293Overriding Word Wrapping
294========================
295
296If you have word wrapping enabled (recommended to comply with RFC 822)
297and you have a very long link in your email it can get wrapped too,
298causing it to become un-clickable by the person receiving it.
299CodeIgniter lets you manually override word wrapping within part of your
300message like this::
301
Derek Jones3c356842011-10-05 16:14:23 -0500302 The text of your email that
303 gets wrapped normally.
304
305 {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
306
307 More text that will be
308 wrapped normally.
309
Derek Jones8ede1a22011-10-05 13:34:52 -0500310
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200311Place the item you do not want word-wrapped between: {unwrap} {/unwrap}