blob: ec639846f6ae10f6714b479d6345e1bdb64f3a30 [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
Derek Jonesd386f252013-07-24 17:35:45 -0700257 .. method:: set_header($header, $value)
258
259 :param string $header: header name
260 :param string $value: header value
261 :returns: void
262
263 Appends additional headers to the e-mail::
264
265 $this->email->set_header('Header1', 'Value1');
266 $this->email->set_header('Header2', 'Value2');
267
268
269 .. method:: clear([$clear_attachments = FALSE])
270
271 :param bool $clear_attachments: whether or not to clear attachments
272
273 Initializes all the email variables to an empty state. This method
274 is intended for use if you run the email sending method in a loop,
275 permitting the data to be reset between cycles.
276
277 ::
278
279 foreach ($list as $name => $address)
280 {
281 $this->email->clear();
282
283 $this->email->to($address);
284 $this->email->from('your@example.com');
285 $this->email->subject('Here is your info '.$name);
286 $this->email->message('Hi '.$name.' Here is the info you requested.');
287 $this->email->send();
288 }
289
290 If you set the parameter to TRUE any attachments will be cleared as
291 well::
292
293 $this->email->clear(TRUE);
294
295
296 .. method:: send([$auto_clear = TRUE])
297
298 :param bool $auto_clear: Whether to :meth:clear automatically
299 :returns: bool
300
301 The Email sending method. Returns boolean TRUE or FALSE based on
302 success or failure, enabling it to be used conditionally::
303
304 if ( ! $this->email->send())
305 {
306 // Generate error
307 }
308
309 This method will automatically clear all parameters if the request was
310 successful. To stop this behaviour pass FALSE::
311
312 if ($this->email->send(FALSE))
313 {
314 // Parameters won't be cleared
315 }
316
317 .. note:: In order to use the ``print_debugger()`` method, you need
318 to avoid clearing the email parameters.
319
320
321 .. method:: attach($filename[, $disposition = ''[, $newname = NULL[, $mime = '']]])
322
323 :param string $filename: name of the file
324 :param string $disposition: 'disposition' of the attachment. Most
325 email clients make their own decision regardless of the MIME
326 specification used here. https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
327 :param string $newname: custom name to use for the file in the email
328 :param string $mime: MIME type to use (useful for buffered data)
329 :returns: CI_Email object for method chaining
330
331 Enables you to send an attachment. Put the file path/name in the first
Andrey Andreevea801ab2014-01-20 15:03:43 +0200332 parameter. For multiple attachments use the method multiple times.
333 For example::
Derek Jonesd386f252013-07-24 17:35:45 -0700334
335 $this->email->attach('/path/to/photo1.jpg');
336 $this->email->attach('/path/to/photo2.jpg');
337 $this->email->attach('/path/to/photo3.jpg');
338
339 To use the default disposition (attachment), leave the second parameter blank,
340 otherwise use a custom disposition::
341
342 $this->email->attach('image.jpg', 'inline');
343
Andrey Andreevea801ab2014-01-20 15:03:43 +0200344 You can also use a URL::
345
346 $this->email->attach('http://example.com/filename.pdf');
347
Derek Jonesd386f252013-07-24 17:35:45 -0700348 If you'd like to use a custom file name, you can use the third paramater::
349
350 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
351
352 If you need to use a buffer string instead of a real - physical - file you can
353 use the first parameter as buffer, the third parameter as file name and the fourth
354 parameter as mime-type::
355
356 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
357
Andrey Andreevea801ab2014-01-20 15:03:43 +0200358 .. method:: attachment_cid($filename)
359
360 :param string $filename: Existing attachment filename
361 :returns: string
362
363 Sets and returns an attachment's Content-ID, which enables your to embed an inline
364 (picture) attachment into HTML. First parameter must be the already attached file name.
365 ::
366
367 $filename = '/img/photo1.jpg';
368 $this->email->attach($filename);
369 foreach ($list as $address)
370 {
371 $this->email->to($address);
372 $cid = $this->email->attach_cid($filename);
373 $this->email->message('<img src='cid:". $cid ."' alt="photo1" />');
374 $this->email->send();
375 }
376
377 .. note:: Content-ID for each e-mail must be re-created for it to be unique.
Derek Jonesd386f252013-07-24 17:35:45 -0700378
379 .. method:: print_debugger([$include = array('headers', 'subject', 'body')])
380
381 :param array $include: Which parts of the message to print out
382 :returns: string
383
384 Returns a string containing any server messages, the email headers, and
385 the email messsage. Useful for debugging.
386
387 You can optionally specify which parts of the message should be printed.
388 Valid options are: **headers**, **subject**, **body**.
389
390 Example::
391
392 // You need to pass FALSE while sending in order for the email data
393 // to not be cleared - if that happens, print_debugger() would have
394 // nothing to output.
395 $this->email->send(FALSE);
396
397 // Will only print the email headers, excluding the message subject and body
398 $this->email->print_debugger(array('headers'));
399
400 .. note:: By default, all of the raw data will be printed.