blob: 1d9d2c1715c3343315eaf8480eba56f6d94f7422 [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
Derek Jonesd386f252013-07-24 17:35:45 -070019.. contents::
20 :local:
21
22.. raw:: html
23
24 <div class="custom-index container"></div>
25
26***********************
27Using the Email Library
28***********************
29
Derek Jones8ede1a22011-10-05 13:34:52 -050030Sending Email
31=============
32
33Sending email is not only simple, but you can configure it on the fly or
34set your preferences in a config file.
35
36Here is a basic example demonstrating how you might send email. Note:
37This example assumes you are sending the email from one of your
38:doc:`controllers <../general/controllers>`.
39
40::
41
Derek Jones3c356842011-10-05 16:14:23 -050042 $this->load->library('email');
43
44 $this->email->from('your@example.com', 'Your Name');
Derek Jonesd386f252013-07-24 17:35:45 -070045 $this->email->to('someone@example.com');
46 $this->email->cc('another@another-example.com');
47 $this->email->bcc('them@their-example.com');
Derek Jones3c356842011-10-05 16:14:23 -050048
49 $this->email->subject('Email Test');
Derek Jonesd386f252013-07-24 17:35:45 -070050 $this->email->message('Testing the email class.');
Derek Jones3c356842011-10-05 16:14:23 -050051
52 $this->email->send();
53
Derek Jones8ede1a22011-10-05 13:34:52 -050054Setting Email Preferences
55=========================
56
Darren Benney77130812013-03-26 02:22:35 +000057There are 21 different preferences available to tailor how your email
Derek Jones8ede1a22011-10-05 13:34:52 -050058messages are sent. You can either set them manually as described here,
59or automatically via preferences stored in your config file, described
60below:
61
62Preferences are set by passing an array of preference values to the
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020063email initialize method. Here is an example of how you might set some
Derek Jones8ede1a22011-10-05 13:34:52 -050064preferences::
65
Derek Jones3c356842011-10-05 16:14:23 -050066 $config['protocol'] = 'sendmail';
67 $config['mailpath'] = '/usr/sbin/sendmail';
68 $config['charset'] = 'iso-8859-1';
69 $config['wordwrap'] = TRUE;
70
71 $this->email->initialize($config);
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73.. note:: Most of the preferences have default values that will be used
74 if you do not set them.
75
76Setting Email Preferences in a Config File
77------------------------------------------
78
79If you prefer not to set preferences using the above method, you can
80instead put them into a config file. Simply create a new file called the
81email.php, add the $config array in that file. Then save the file at
82config/email.php and it will be used automatically. You will NOT need to
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020083use the ``$this->email->initialize()`` method if you save your
84preferences in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86Email Preferences
87=================
88
89The following is a list of all the preferences that can be set when
90sending email.
91
Joseph Wensleyd14717f2011-10-05 23:57:14 -040092=================== ====================== ============================ =======================================================================
93Preference Default Value Options Description
94=================== ====================== ============================ =======================================================================
95**useragent** CodeIgniter None The "user agent".
96**protocol** mail mail, sendmail, or smtp The mail sending protocol.
Derek Jonesd386f252013-07-24 17:35:45 -070097**mailpath** /usr/sbin/sendmail None The server path to Sendmail.
Joseph Wensleyd14717f2011-10-05 23:57:14 -040098**smtp_host** No Default None SMTP Server Address.
99**smtp_user** No Default None SMTP Username.
100**smtp_pass** No Default None SMTP Password.
101**smtp_port** 25 None SMTP Port.
102**smtp_timeout** 5 None SMTP Timeout (in seconds).
nisheeth-barthwal9ecde432013-02-18 17:06:12 +0530103**smtp_keepalive** FALSE TRUE or FALSE (boolean) Enable persistent SMTP connections.
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400104**smtp_crypto** No Default tls or ssl SMTP Encryption
105**wordwrap** TRUE TRUE or FALSE (boolean) Enable word-wrap.
106**wrapchars** 76 Character count to wrap at.
107**mailtype** text text or html Type of mail. If you send HTML email you must send it as a complete web
108 page. Make sure you don't have any relative links or relative image
109 paths otherwise they will not work.
Andrey Andreev6d9915a2012-10-10 16:18:33 +0300110**charset** ``$config['charset']`` Character set (utf-8, iso-8859-1, etc.).
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400111**validate** FALSE TRUE or FALSE (boolean) Whether to validate the email address.
112**priority** 3 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal.
113**crlf** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
114**newline** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
115**bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode.
116**bcc_batch_size** 200 None Number of emails in each BCC batch.
leandronfbe07c922012-03-22 19:49:23 -0300117**dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400118=================== ====================== ============================ =======================================================================
119
Derek Jones8ede1a22011-10-05 13:34:52 -0500120Overriding Word Wrapping
121========================
122
123If you have word wrapping enabled (recommended to comply with RFC 822)
124and you have a very long link in your email it can get wrapped too,
125causing it to become un-clickable by the person receiving it.
126CodeIgniter lets you manually override word wrapping within part of your
127message like this::
128
Derek Jones3c356842011-10-05 16:14:23 -0500129 The text of your email that
130 gets wrapped normally.
131
132 {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
133
134 More text that will be
135 wrapped normally.
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Derek Jonesd386f252013-07-24 17:35:45 -0700137
138Place the item you do not want word-wrapped between: {unwrap} {/unwrap}
139
140***************
141Class Reference
142***************
143
144.. class:: CI_Email
145
146 .. method:: from($from[, $name = ''[, $return_path = NULL]])
147
148 :param string $from: "From" email address
149 :param string $name: "From" display name
150 :param string $return_path: optional email address to redirect undelivered email
151 :returns: CI_Email object for method chaining
152
153 Sets the email address and name of the person sending the email::
154
155 $this->email->from('you@example.com', 'Your Name');
156
157 You can also set a Return-Path, to help redirect undelivered mail::
158
159 $this->email->from('you@example.com', 'Your Name', 'returned_emails@example.com');
160
161 .. note:: Return-Path can't be used if you've configured 'smtp' as
162 your protocol.
163
164
165 .. method:: reply_to($replyto[, $name = ''])
166
167 :param string $replyto: email address for replies
168 :param string $name: display name for reply email address
169 :returns: CI_Email object for method chaining
170
171 Sets the reply-to address. If the information is not provided the
172 information in the :meth:from method is used. Example::
173
174 $this->email->reply_to('you@example.com', 'Your Name');
175
176
177 .. method:: to($to)
178
179 :param mixed $to: comma delimited string or array of email addresses
180 :returns: CI_Email object for method chaining
181
182 Sets the email address(s) of the recipient(s). Can be a single email
183 , a comma-delimited list or an array::
184
185 $this->email->to('someone@example.com');
186
187 ::
188
189 $this->email->to('one@example.com, two@example.com, three@example.com');
190
191 ::
192
193 $list = array('one@example.com', 'two@example.com', 'three@example.com');
194
195 $this->email->to($list);
196
197
198 .. method:: cc($cc)
199
200 :param mixed $cc: comma delimited string or array of email addresses
201 :returns: CI_Email object for method chaining
202
203 Sets the CC email address(s). Just like the "to", can be a single
204 email, a comma-delimited list or an array.
205
206
207 .. method:: bcc($bcc, $limit = '')
208
209 :param mixed $bcc: comma delimited string or array of email addresses
210 :param int $limit: Maximum number of emails to send per batch
211 :returns: CI_Email object for method chaining
212
213 Sets the BCC email address(s). Just like the "to", can be a single
214 email, a comma-delimited list or an array.
215
216 If ``$limit`` is set, "batch mode" will be enabled, which will send
217 the emails to batches, with each batch not exceeding the specified
218 ``$limit``.
219
220
221 .. method:: subject($subject)
222
223 :param string $subject: email subject line
224 :returns: CI_Email object for method chaining
225
226 Sets the email subject::
227
228 $this->email->subject('This is my subject');
229
230
231 .. method:: message($body)
232
233 :param string $body: email body
234 :returns: CI_Email object for method chaining
235
236 Sets the email message body::
237
238 $this->email->message('This is my message');
239
240
241 .. method:: set_alt_message([$str = ''])
242
243 :param string $str: alternate email body
244 :returns: CI_Email object for method chaining
245
246 Sets the alternative email message body::
247
248 $this->email->set_alt_message('This is the alternative message');
249
250 This is an optional message string which can be used if you send
251 HTML formatted email. It lets you specify an alternative message
252 with no HTML formatting which is added to the header string for
253 people who do not accept HTML email. If you do not set your own
254 message CodeIgniter will extract the message from your HTML email
255 and strip the tags.
256
257
258 .. method:: set_header($header, $value)
259
260 :param string $header: header name
261 :param string $value: header value
262 :returns: void
263
264 Appends additional headers to the e-mail::
265
266 $this->email->set_header('Header1', 'Value1');
267 $this->email->set_header('Header2', 'Value2');
268
269
270 .. method:: clear([$clear_attachments = FALSE])
271
272 :param bool $clear_attachments: whether or not to clear attachments
273
274 Initializes all the email variables to an empty state. This method
275 is intended for use if you run the email sending method in a loop,
276 permitting the data to be reset between cycles.
277
278 ::
279
280 foreach ($list as $name => $address)
281 {
282 $this->email->clear();
283
284 $this->email->to($address);
285 $this->email->from('your@example.com');
286 $this->email->subject('Here is your info '.$name);
287 $this->email->message('Hi '.$name.' Here is the info you requested.');
288 $this->email->send();
289 }
290
291 If you set the parameter to TRUE any attachments will be cleared as
292 well::
293
294 $this->email->clear(TRUE);
295
296
297 .. method:: send([$auto_clear = TRUE])
298
299 :param bool $auto_clear: Whether to :meth:clear automatically
300 :returns: bool
301
302 The Email sending method. Returns boolean TRUE or FALSE based on
303 success or failure, enabling it to be used conditionally::
304
305 if ( ! $this->email->send())
306 {
307 // Generate error
308 }
309
310 This method will automatically clear all parameters if the request was
311 successful. To stop this behaviour pass FALSE::
312
313 if ($this->email->send(FALSE))
314 {
315 // Parameters won't be cleared
316 }
317
318 .. note:: In order to use the ``print_debugger()`` method, you need
319 to avoid clearing the email parameters.
320
321
322 .. method:: attach($filename[, $disposition = ''[, $newname = NULL[, $mime = '']]])
323
324 :param string $filename: name of the file
325 :param string $disposition: 'disposition' of the attachment. Most
326 email clients make their own decision regardless of the MIME
327 specification used here. https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
328 :param string $newname: custom name to use for the file in the email
329 :param string $mime: MIME type to use (useful for buffered data)
330 :returns: CI_Email object for method chaining
331
332 Enables you to send an attachment. Put the file path/name in the first
333 parameter. Note: Use a file path, not a URL. For multiple attachments
334 use the method multiple times. For example::
335
336 $this->email->attach('/path/to/photo1.jpg');
337 $this->email->attach('/path/to/photo2.jpg');
338 $this->email->attach('/path/to/photo3.jpg');
339
340 To use the default disposition (attachment), leave the second parameter blank,
341 otherwise use a custom disposition::
342
343 $this->email->attach('image.jpg', 'inline');
344
345 If you'd like to use a custom file name, you can use the third paramater::
346
347 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
348
349 If you need to use a buffer string instead of a real - physical - file you can
350 use the first parameter as buffer, the third parameter as file name and the fourth
351 parameter as mime-type::
352
353 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
354
355
356 .. method:: print_debugger([$include = array('headers', 'subject', 'body')])
357
358 :param array $include: Which parts of the message to print out
359 :returns: string
360
361 Returns a string containing any server messages, the email headers, and
362 the email messsage. Useful for debugging.
363
364 You can optionally specify which parts of the message should be printed.
365 Valid options are: **headers**, **subject**, **body**.
366
367 Example::
368
369 // You need to pass FALSE while sending in order for the email data
370 // to not be cleared - if that happens, print_debugger() would have
371 // nothing to output.
372 $this->email->send(FALSE);
373
374 // Will only print the email headers, excluding the message subject and body
375 $this->email->print_debugger(array('headers'));
376
377 .. note:: By default, all of the raw data will be printed.